succesfully done the function

This commit is contained in:
leeyunlong 2025-06-11 15:04:30 +08:00
parent bd8b0ef886
commit 490fa0495f
2 changed files with 174 additions and 19 deletions

View File

@ -23,33 +23,41 @@ def check():
sys.exit(1)
##############################################################################
#########Main logic
##No Copy Workbook Logic
##############################################################################
def process_OffsetAddress_and_RegName_col(file_path, sheet_name):
def no_copy_workbook(file_path, sheet_name):
"""
处理OffsetAddress and 列中的"charu"内容并写回Excel(.xls)
处理Excel文件中的工作表不复制工作表内容
使用纯xlwt库实现
"""
# 读取原始Excel文件
rb = xlrd.open_workbook(file_path)
sheet_index = rb.sheet_names().index(sheet_name)
sheet = rb.sheet_by_index(sheet_index)
# 获取OffsetAddress列的索引
offset_col = None
# 获取OffsetAddress and RegName列的索引
offset_col = None
regname_col = None
bits_col = None
offsetWidth_col = None
for col in range(sheet.ncols):
if sheet.cell(0, col).value == 'OffsetAddress':
offset_col = col
if sheet.cell(0, col).value == 'RegName':
regname_col = col
if offset_col is not None and regname_col is not None:
if sheet.cell(0, col).value == 'Bits':
bits_col = col
if sheet.cell(0, col).value == 'OffsetWidth':
offsetWidth_col = col
if offset_col is not None and regname_col is not None and bits_col is not None and offsetWidth_col is not None:
break
pattern_row = 0
if offset_col is None:
print("[Error]: 未找到OffsetAddress列")
return False
if offset_col is None or regname_col is None or bits_col is None:
print("[Error]: 未找到OffsetAddress列或RegName列或Bits列")
return
else:
for row in range(sheet.nrows):
if sheet.cell(row, offset_col).value == 'offset' or sheet.cell(row, regname_col).value == 'regname':
@ -60,37 +68,74 @@ def process_OffsetAddress_and_RegName_col(file_path, sheet_name):
# 创建新的Excel工作簿
wb = xlwt.Workbook()
ws = wb.add_sheet(sheet_name)
bits_seq = []
row_range = []
bits_seq,row_range = parse_bits_sequences(file_path, sheet.name)
is_in_sequence = False
# The cell Not copied logic
first_pattern_row = 0
for row in range(sheet.nrows):
for col in range(sheet.ncols):
for col in range(sheet.ncols):
for row in range(sheet.nrows):
# > OffsetAddress and RegName_col col not copied
if (row >= pattern_row and (col == offset_col or col == regname_col)) or (sheet.cell(row, col).value == 'offset' or sheet.cell(row, col).value == 'regname'):
continue
# The Bits col not copied logic
if col == bits_col:
# 处理Bits列No copy的逻辑
for (start, end) in row_range:
if row >= start and row <=end :
is_in_sequence = True
if is_in_sequence:
is_in_sequence = False
continue
ws.write(row, col, sheet.cell(row, col).value)
wb.save(file_path)
print("Has copied all data to new file: %s"%(file_path))
return offset_col, regname_col, bits_col, offsetWidth_col, pattern_row, row_range, wb, ws
##############################################################################
#########process_OffsetAddress_and_RegName_Bits_col
##############################################################################
def process_OffsetAddress_and_RegName_Bits_col(file_path, sheet_name):
"""
处理OffsetAddress and RegName列中的"charu"内容并写回Excel(.xls)
使用纯xlwt库实现
"""
# 读取原始Excel文件
#rb = xlrd.open_workbook(file_path)
#sheet_index = rb.sheet_names().index(sheet_name)
#sheet = rb.sheet_by_index(sheet_index)
offset_col = None
regname_col = None
bits_col = None
pattern_row = None
row_range = []
#No copy updated logic
offset_col, regname_col, bits_col, offsetWidth_col, pattern_row, row_range, wb, ws = no_copy_workbook(file_path, sheet_name)
last_valid_value = None
first_charu_found = False
modified = False
# 处理OffsetAddress列
for row in range(1, sheet.nrows): # 跳过表头
cell_value = sheet.cell(row, offset_col).value # offset_col is the offsetAddress column
if row >= pattern_row and cell_value and str(cell_value).strip():
if not first_charu_found and last_valid_value is not None:
first_charu_found = True
# 将第一个"charu"替换为上一个值加4
# 将第一个"offset"替换为上一个值加4
new_value = int(last_valid_value, 16) + 4
ws.write(row, offset_col, f"0x{new_value:02X}")
last_valid_value = f"0x{new_value:02X}"
modified = True
elif first_charu_found and last_valid_value is not None:
# 后续"charu"继续加4
# 后续"offset"继续加4
new_value = int(last_valid_value, 16) + 4
ws.write(row, offset_col, f"0x{new_value:02X}")
last_valid_value = f"0x{new_value:02X}"
@ -101,6 +146,7 @@ def process_OffsetAddress_and_RegName_col(file_path, sheet_name):
last_reg = None
# 处理RegName列
for row in range(1, sheet.nrows):
cell_value = sheet.cell(row, regname_col).value
@ -117,6 +163,45 @@ def process_OffsetAddress_and_RegName_col(file_path, sheet_name):
elif cell_value and str(cell_value).startswith('reg_'):
last_reg = cell_value # 记录最后一个有效的reg名称
# 处理Bits列
prev_right = None
for (start, end) in row_range:
for row in range(start, end + 1):
cell_value = int(sheet.cell(row, offsetWidth_col).value)
if row == start and cell_value and str(cell_value).strip():
left = 32 -1
right = left - cell_value + 1
content = f"[{left}:{right}]"
ws.write(row, bits_col, content)
prev_right = right
elif row == end and cell_value and str(cell_value).strip():
left = prev_right - 1
right = left - cell_value + 1
content = f"[{left}:{right}]"
ws.write(row, bits_col, content)
prev_right = None
else:
if prev_right is not None:
left = prev_right - 1
right = left - cell_value + 1
content = f"[{left}:{right}]"
ws.write(row, bits_col, content)
prev_right = right
else:
print(f"[Warning]: No previous right value for row {row} in Bits column")
#if row >= pattern_row and cell_value and str(cell_value).strip():
# if cell_value == 'BitsHead':
# ws.write(row, bits_col, 'BitsHead')
# modified = True
# elif cell_value == 'BitsTail':
# ws.write(row, bits_col, 'BitsTail')
# modified = True
# elif str(cell_value).startswith('Bits'):
# # 处理Bits列中的其他内容
# ws.write(row, bits_col, f"Processed_{cell_value}")
# modified = True
# Ensure whether the CurrentFile is modified
if modified:
wb.save(file_path)
@ -127,9 +212,77 @@ def process_OffsetAddress_and_RegName_col(file_path, sheet_name):
return modified
###################################################################################################
##
##Demand:
##1.Get the sub-sequences of BitsHead-->BitsTail of all
###################################################################################################
# ... existing code ...
def parse_bits_sequences(file_path, sheet_name):
"""
Parse all sequences between BitsHead and BitsTail in the Bits column
Returns a list where each element is a sublist [BitsHead, ..., BitsTail]
"""
# Open Excel file and get the specified sheet
rb = xlrd.open_workbook(file_path)
sheet = rb.sheet_by_name(sheet_name)
# Find the Bits column index
bits_col = None
for col in range(sheet.ncols):
if sheet.cell(0, col).value == 'Bits':
bits_col = col
break
if bits_col is None:
print("[Error]: Bits column not found")
return []
current_sequence = []
bits_seq = []
start_row = None
row_range = []
in_sequence = False
is_closed = True
# Iterate through rows (skip header)
for row in range(1, sheet.nrows):
cell_value = str(sheet.cell(row, bits_col).value).strip()
if cell_value == 'BitsHead':
# If already in a sequence when new BitsHead found, save current sequence
if in_sequence:
bits_seq.append(current_sequence)
in_sequence = True
is_closed = False
start_row = row
current_sequence = [cell_value]
elif cell_value == 'BitsTail':
if in_sequence:
in_sequence = False
is_closed = True
current_sequence.append(cell_value)
bits_seq.append(current_sequence)
current_sequence = []
# sequence tuple logic
row_range.append((start_row,row))
elif in_sequence:
current_sequence.append(cell_value)
# Handle last unclosed sequence
if is_closed is False:
print("[Warning]: Last sequence not closed with BitsTail, adding to bits_seq")
bits_seq.append(current_sequence)
print("Bits sequences found:")
for seq,(start,end) in zip(bits_seq,row_range):
print(f"Rows {start+1}-{end+1}: {seq}")
return bits_seq, row_range
####Main()
if __name__ == "__main__":
check()
file_path = sys.argv[1]
@ -137,7 +290,9 @@ if __name__ == "__main__":
sheets_num = len(book.sheet_names())
print("This is OK,sheets_num is : %d" % sheets_num)
for index in range(sheets_num):
#for index in range(sheets_num):
for index in range(1):
sheet = book.sheet_by_index(index)
print("Sheet Name: %s"%(sheet.name))
print("Rows: %d, Cols: %d"%(sheet.nrows, sheet.ncols))
@ -149,6 +304,6 @@ if __name__ == "__main__":
# print(f"Cell[{row}, {col}]: {cell_value}")
# process_OffsetAddress_and_RegName_col(file_path, sheet.name)
# no_copy_workbook(file_path, sheet.name)
process_OffsetAddress_and_RegName_Bits_col(file_path, sheet.name)
print("\n")

Binary file not shown.