154 lines
5.5 KiB
Python
154 lines
5.5 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import sys
|
|
import xlrd
|
|
import xlwt
|
|
import math
|
|
|
|
def check():
|
|
if(len(sys.argv) < 2):
|
|
print("[Error]:Not have input file")
|
|
print("Usage : %s <filename>.xlsx"%(sys.argv[0]))
|
|
sys.exit(1)
|
|
|
|
if(sys.argv[1]=='-help'):
|
|
print("Usage : %s <filename>.xlsx"%(sys.argv[0]))
|
|
sys.exit(0)
|
|
|
|
if(os.path.exists(sys.argv[1])==False):
|
|
print(f"[Error]: 文件 {sys.argv[1]} 不存在")
|
|
print("[Error]:Not such file")
|
|
sys.exit(1)
|
|
|
|
##############################################################################
|
|
#########Main logic
|
|
##############################################################################
|
|
def process_OffsetAddress_and_RegName_col(file_path, sheet_name):
|
|
"""
|
|
处理OffsetAddress and 列中的"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)
|
|
|
|
# 获取OffsetAddress列的索引
|
|
offset_col = None
|
|
regname_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:
|
|
break
|
|
|
|
pattern_row = 0
|
|
if offset_col is None:
|
|
print("[Error]: 未找到OffsetAddress列")
|
|
return False
|
|
else:
|
|
for row in range(sheet.nrows):
|
|
if sheet.cell(row, offset_col).value == 'offset' or sheet.cell(row, regname_col).value == 'regname':
|
|
pattern_row = row
|
|
print("pattern_row is : %d"%(pattern_row))
|
|
break
|
|
|
|
# 创建新的Excel工作簿
|
|
wb = xlwt.Workbook()
|
|
ws = wb.add_sheet(sheet_name)
|
|
|
|
# The cell Not copied logic
|
|
first_pattern_row = 0
|
|
for row in range(sheet.nrows):
|
|
for col in range(sheet.ncols):
|
|
# > 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
|
|
ws.write(row, col, sheet.cell(row, col).value)
|
|
|
|
wb.save(file_path)
|
|
print("Has copied all data to new file: %s"%(file_path))
|
|
|
|
last_valid_value = None
|
|
first_charu_found = False
|
|
modified = False
|
|
|
|
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
|
|
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
|
|
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 cell_value and str(cell_value).strip():
|
|
# 记录最后一个有效值
|
|
last_valid_value = cell_value
|
|
|
|
last_reg = None
|
|
|
|
for row in range(1, sheet.nrows):
|
|
cell_value = sheet.cell(row, regname_col).value
|
|
|
|
if row >= pattern_row and cell_value and str(cell_value).strip():
|
|
if last_reg:
|
|
try:
|
|
num = int(last_reg.split("_")[1]) + 1
|
|
new_reg = f"reg_{num:02d}"
|
|
ws.write(row, regname_col, new_reg)
|
|
last_reg = new_reg
|
|
modified = True
|
|
except (IndexError, ValueError):
|
|
print(f"Invalid Reg Name: {last_reg}")
|
|
elif cell_value and str(cell_value).startswith('reg_'):
|
|
last_reg = cell_value # 记录最后一个有效的reg名称
|
|
|
|
# Ensure whether the CurrentFile is modified
|
|
if modified:
|
|
wb.save(file_path)
|
|
print(f"The updated file has been loaded into {file_path}")
|
|
else:
|
|
print("No modifications were made to the file.")
|
|
|
|
return modified
|
|
|
|
###################################################################################################
|
|
##
|
|
###################################################################################################
|
|
|
|
if __name__ == "__main__":
|
|
check()
|
|
file_path = sys.argv[1]
|
|
book = xlrd.open_workbook(file_path)
|
|
sheets_num = len(book.sheet_names())
|
|
print("This is OK,sheets_num is : %d" % sheets_num)
|
|
|
|
for index in range(sheets_num):
|
|
sheet = book.sheet_by_index(index)
|
|
print("Sheet Name: %s"%(sheet.name))
|
|
print("Rows: %d, Cols: %d"%(sheet.nrows, sheet.ncols))
|
|
#for row in range(sheet.nrows):
|
|
# for col in range(sheet.ncols):
|
|
# cell_value = sheet.cell_value(row, col)
|
|
# if isinstance(cell_value, str):
|
|
# cell_value = cell_value.strip()
|
|
# print(f"Cell[{row}, {col}]: {cell_value}")
|
|
|
|
|
|
# process_OffsetAddress_and_RegName_col(file_path, sheet.name)
|
|
|
|
print("\n") |