Refactor ExcelConverter to streamline logging and enhance summary reporting

This commit is contained in:
Misaka_Company
2026-01-16 15:59:25 +08:00
parent bb9348d12b
commit 92d4572985
2 changed files with 9 additions and 57 deletions

View File

@@ -46,10 +46,6 @@ class ExcelConverter:
if output_file:
output_file = self._handle_output_file(output_file)
self._print("=" * 80)
self._print("开始转换 Excel 数据")
self._print("=" * 80)
# 读取工作表
wb = openpyxl.load_workbook(input_file)
ws = wb.active
@@ -57,44 +53,22 @@ class ExcelConverter:
# 解析订单数据
orders = self._parse_sheet(ws)
self._print(f"\n\n共解析到 {len(orders)} 个订单")
# 打印每个订单的摘要
for i, order in enumerate(orders, 1):
order_info = order['order_info']
materials = order['materials']
self._print(f"\n订单 {i}:")
self._print(f" 备料计划单号: {order_info.get('备料计划单号', 'N/A')}")
self._print(f" 来源单号: {order_info.get('来源单号', 'N/A')}")
self._print(f" 产品编码: {order_info.get('产品编码', 'N/A')}")
self._print(f" 产品名称: {order_info.get('产品名称', 'N/A')}")
self._print(f" 产品计划数量: {order_info.get('产品计划数量', 'N/A')}")
self._print(f" 物料数量: {len(materials)}")
# 转换为 DataFrame
df = self._convert_to_dataframe(orders)
self._print(f"\n转换后的数据形状: {df.shape}")
if not df.empty:
self._print(f"列名: {list(df.columns)}")
# 保存文件
if output_file:
df.to_excel(output_file, index=False)
self._print(f"\n数据已保存到: {output_file}")
# 显示前几行数据
self._print("\n数据预览:")
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 200)
pd.set_option('display.max_colwidth', 30)
self._print(df.head(20))
pd.reset_option('display.max_columns')
pd.reset_option('display.width')
pd.reset_option('display.max_colwidth')
else:
self._print("警告: 没有数据可保存")
# 打印汇总报告
self._print("=" * 60)
self._print("转换完成")
self._print("=" * 60)
self._print(f"订单数: {len(orders)}")
self._print(f"数据行数: {len(df)}")
self._print(f"输出文件: {output_file if output_file else 'N/A'}")
self._print("=" * 60)
return df
@@ -135,13 +109,6 @@ class ExcelConverter:
orders = []
all_rows = list(ws.iter_rows(values_only=True))
# 查找所有空行,用于分割订单
empty_rows = [i for i, row in enumerate(all_rows)
if all(cell is None or str(cell).strip() == "" for cell in row)]
self._print(f"检测到空行索引: {empty_rows}")
self._print(f"总行数: {len(all_rows)}")
# 逐行扫描,按订单结构解析
i = 0
while i < len(all_rows):
@@ -149,16 +116,12 @@ class ExcelConverter:
# 检查是否是订单标题行
if row and '离散备料计划' in str(row[0]):
self._print(f"\n在行 {i + 1} 发现订单标题")
# 解析订单头信息接下来的4行
order_info = {}
for j in range(1, 5):
if i + j < len(all_rows) and all_rows[i + j]:
self._parse_header_row(all_rows[i + j], order_info)
self._print(f"订单头信息: {order_info}")
# 跳过空行,找到表格标题行
table_row = i + 5
while table_row < len(all_rows) and (not all_rows[table_row] or not all_rows[table_row][0]):
@@ -166,8 +129,6 @@ class ExcelConverter:
# 检查是否是表格标题行
if table_row < len(all_rows) and all_rows[table_row] and all_rows[table_row][0] == '序号':
self._print(f"在行 {table_row + 1} 发现表格标题")
# 检查表头下一行是否为空,判断是否存在数据
next_row = table_row + 1
is_empty_row = (next_row < len(all_rows) and
@@ -175,18 +136,15 @@ class ExcelConverter:
all(cell is None or str(cell).strip() == "" for cell in all_rows[next_row]))
if is_empty_row:
self._print(f"表头下没有数据")
# 没有数据,查找页脚信息
materials = []
footer_info = {}
data_row = next_row + 1
while data_row < len(all_rows) and all_rows[data_row]:
if all_rows[data_row][0] and ('制单人' in str(all_rows[data_row][0]) or '打印人' in str(all_rows[data_row][0])):
self._print(f"在行 {data_row + 1} 发现页脚信息")
self._parse_header_row(all_rows[data_row], footer_info)
if data_row + 1 < len(all_rows) and all_rows[data_row + 1]:
self._parse_header_row(all_rows[data_row + 1], footer_info)
self._print(f"页脚信息: {footer_info}")
break
data_row += 1
@@ -196,20 +154,17 @@ class ExcelConverter:
})
else:
# 有数据,开始提取物料
self._print(f"表头下有数据")
materials = []
footer_info = {} # 页脚信息
data_row = table_row + 1
while data_row < len(all_rows) and all_rows[data_row]:
# 检查是否是页脚信息(制单人、打印人)
if all_rows[data_row][0] and ('制单人' in str(all_rows[data_row][0]) or '打印人' in str(all_rows[data_row][0])):
self._print(f"在行 {data_row + 1} 发现页脚信息")
# 解析页脚信息
self._parse_header_row(all_rows[data_row], footer_info)
# 检查下一行是否也是页脚信息
if data_row + 1 < len(all_rows) and all_rows[data_row + 1]:
self._parse_header_row(all_rows[data_row + 1], footer_info)
self._print(f"页脚信息: {footer_info}")
break
# 提取物料数据
@@ -230,12 +185,9 @@ class ExcelConverter:
'累计出库数量': material_row[12],
}
materials.append(material)
self._print(f" 添加物料: {material['材料编码']} - {material['材料名称']}")
data_row += 1
self._print(f"共解析到 {len(materials)} 条物料数据")
orders.append({
'order_info': {**order_info, **footer_info},
'materials': materials

View File

@@ -202,7 +202,7 @@ class DiscreteMaterialPlanExtractor:
self._print("=" * 80)
# 登录成功后可以进行后续操作
# 点击打开"菜单"
# 点击打开"功能菜单"
main_frame.locator("i").first.click()
# 点击打开"离散备料计划维护"