废话不多说,本次分享自己最近写的代码,主要是用python解析定义好的结构体数据表格,并转为C++头文件:
'''
Author: solitary
Date: 2023-01-17 17:50:08
LastEditTime: 2023-01-30 11:25:51
LastEditors: your name
Description: 转换excel表格为struct文件
FilePath: /src/parse_excel/excelTocpp.py
'''
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.utils import column_index_from_string, get_column_letter
import os, sys
import string
def get_all_struct_mems(struct_dict_, wb:Workbook, sheet_title):
print(sheet_title)
ws = wb[sheet_title]
weidu_col = 0
bianliang_col = 0
leixing_col = 0
#找到"维度"字段所在列的index
for col in ws.iter_rows(min_row=1, max_row=1, min_col=1):
for cell in col:
weidu_col = weidu_col + 1
if(cell.value == "维度"):
break
#找到"变量类型"字段所在列的index
for col in ws.iter_rows(min_row=1, max_row=1, min_col=1):
for cell in col:
leixing_col = leixing_col + 1
if(cell.value == "变量类型"):
break
#找到"变量名"字段所在列的index
for col in ws.iter_rows(min_row=1, max_row=1, min_col=1):
for cell in col:
bianliang_col = bianliang_col + 1
if(cell.value == "变量名"):
break
weidu_col_index = column_index_from_string(get_column_letter(weidu_col))
bianliang_col_index = column_index_from_string(get_column_letter(bianliang_col))
leixing_col_index = column_index_from_string(get_column_letter(leixing_col))
#遍历所有的合并单元格
for merged_range in ws.merged_cell_ranges:
#只保存第一列的合并列表数据
if merged_range.min_col == 1:
#存储结构体成员名称和类型名称的字典
merged_struct_mems_dict = {}
#存储合并单元格的起止坐标
for rowx in range(merged_range.min_row, merged_range.max_row + 1):
if(int(ws.cell(row = rowx, column = weidu_col_index).value) > 1):
items = ws.cell(row = rowx, column