style: normalize code formatting across the codebase
- Standardize quote style (single to double quotes) - Improve code formatting consistency - Apply formatting to utilities, GUI components, and tools - Update imports and docstrings for consistency Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
Excel 报表数据转换工具组件
|
||||
将 Excel 报表数据转换为数据库记录形式
|
||||
"""
|
||||
|
||||
import pandas as pd
|
||||
import openpyxl
|
||||
from typing import List, Dict, Optional
|
||||
@@ -12,10 +13,7 @@ class ExcelConverter:
|
||||
"""Excel 报表数据转换器"""
|
||||
|
||||
# 字段名称映射(解决字段名冲突)
|
||||
FIELD_NAME_MAPPING = {
|
||||
'计划数量': '产品计划数量',
|
||||
'单位': '产品单位'
|
||||
}
|
||||
FIELD_NAME_MAPPING = {"计划数量": "产品计划数量", "单位": "产品单位"}
|
||||
|
||||
def __init__(self, verbose: bool = True):
|
||||
"""
|
||||
@@ -115,7 +113,7 @@ class ExcelConverter:
|
||||
row = all_rows[i]
|
||||
|
||||
# 检查是否是订单标题行
|
||||
if row and '离散备料计划' in str(row[0]):
|
||||
if row and "离散备料计划" in str(row[0]):
|
||||
# 解析订单头信息(接下来的4行)
|
||||
order_info = {}
|
||||
for j in range(1, 5):
|
||||
@@ -124,16 +122,27 @@ class ExcelConverter:
|
||||
|
||||
# 跳过空行,找到表格标题行
|
||||
table_row = i + 5
|
||||
while table_row < len(all_rows) and (not all_rows[table_row] or not all_rows[table_row][0]):
|
||||
while table_row < len(all_rows) and (
|
||||
not all_rows[table_row] or not all_rows[table_row][0]
|
||||
):
|
||||
table_row += 1
|
||||
|
||||
# 检查是否是表格标题行
|
||||
if table_row < len(all_rows) and all_rows[table_row] and all_rows[table_row][0] == '序号':
|
||||
if (
|
||||
table_row < len(all_rows)
|
||||
and all_rows[table_row]
|
||||
and all_rows[table_row][0] == "序号"
|
||||
):
|
||||
# 检查表头下一行是否为空,判断是否存在数据
|
||||
next_row = table_row + 1
|
||||
is_empty_row = (next_row < len(all_rows) and
|
||||
all_rows[next_row] and
|
||||
all(cell is None or str(cell).strip() == "" for cell in all_rows[next_row]))
|
||||
is_empty_row = (
|
||||
next_row < len(all_rows)
|
||||
and all_rows[next_row]
|
||||
and all(
|
||||
cell is None or str(cell).strip() == ""
|
||||
for cell in all_rows[next_row]
|
||||
)
|
||||
)
|
||||
|
||||
if is_empty_row:
|
||||
# 没有数据,查找页脚信息
|
||||
@@ -141,17 +150,27 @@ class ExcelConverter:
|
||||
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])):
|
||||
if all_rows[data_row][0] and (
|
||||
"制单人" in str(all_rows[data_row][0])
|
||||
or "打印人" in str(all_rows[data_row][0])
|
||||
):
|
||||
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)
|
||||
if (
|
||||
data_row + 1 < len(all_rows)
|
||||
and all_rows[data_row + 1]
|
||||
):
|
||||
self._parse_header_row(
|
||||
all_rows[data_row + 1], footer_info
|
||||
)
|
||||
break
|
||||
data_row += 1
|
||||
|
||||
orders.append({
|
||||
'order_info': {**order_info, **footer_info},
|
||||
'materials': materials
|
||||
})
|
||||
orders.append(
|
||||
{
|
||||
"order_info": {**order_info, **footer_info},
|
||||
"materials": materials,
|
||||
}
|
||||
)
|
||||
else:
|
||||
# 有数据,开始提取物料
|
||||
materials = []
|
||||
@@ -159,39 +178,48 @@ class ExcelConverter:
|
||||
data_row = table_row + 1
|
||||
while data_row < len(all_rows) and all_rows[data_row]:
|
||||
# 检查是否是页脚信息(制单人、打印人)
|
||||
if all_rows[data_row+1][0] and '制单人' in str(all_rows[data_row+1][0]) :
|
||||
if all_rows[data_row + 1][0] and "制单人" in str(
|
||||
all_rows[data_row + 1][0]
|
||||
):
|
||||
# 解析页脚信息
|
||||
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)
|
||||
if (
|
||||
data_row + 1 < len(all_rows)
|
||||
and all_rows[data_row + 1]
|
||||
):
|
||||
self._parse_header_row(
|
||||
all_rows[data_row + 1], footer_info
|
||||
)
|
||||
break
|
||||
|
||||
# 提取物料数据
|
||||
material_row = all_rows[data_row]
|
||||
material = {
|
||||
'序号': material_row[0],
|
||||
'材料编码': material_row[1],
|
||||
'材料名称': material_row[2],
|
||||
'规格': material_row[3],
|
||||
'型号': material_row[4],
|
||||
'图号': material_row[5],
|
||||
'物料材质': material_row[6],
|
||||
'计划数量': material_row[7],
|
||||
'单位': material_row[8],
|
||||
'需用日期': material_row[9],
|
||||
'发料仓库': material_row[10],
|
||||
'单位用量': material_row[11],
|
||||
'累计出库数量': material_row[12],
|
||||
"序号": material_row[0],
|
||||
"材料编码": material_row[1],
|
||||
"材料名称": material_row[2],
|
||||
"规格": material_row[3],
|
||||
"型号": material_row[4],
|
||||
"图号": material_row[5],
|
||||
"物料材质": material_row[6],
|
||||
"计划数量": material_row[7],
|
||||
"单位": material_row[8],
|
||||
"需用日期": material_row[9],
|
||||
"发料仓库": material_row[10],
|
||||
"单位用量": material_row[11],
|
||||
"累计出库数量": material_row[12],
|
||||
}
|
||||
materials.append(material)
|
||||
|
||||
data_row += 1
|
||||
|
||||
orders.append({
|
||||
'order_info': {**order_info, **footer_info},
|
||||
'materials': materials
|
||||
})
|
||||
orders.append(
|
||||
{
|
||||
"order_info": {**order_info, **footer_info},
|
||||
"materials": materials,
|
||||
}
|
||||
)
|
||||
|
||||
i += 1
|
||||
|
||||
@@ -208,9 +236,9 @@ class ExcelConverter:
|
||||
i = 0
|
||||
while i < len(row):
|
||||
cell = row[i]
|
||||
if cell and str(cell).strip() and ':' in str(cell):
|
||||
if cell and str(cell).strip() and ":" in str(cell):
|
||||
# 找到字段名
|
||||
field_name = str(cell).replace(':', '').strip()
|
||||
field_name = str(cell).replace(":", "").strip()
|
||||
|
||||
# 应用字段名映射
|
||||
if field_name in self.FIELD_NAME_MAPPING:
|
||||
@@ -218,9 +246,11 @@ class ExcelConverter:
|
||||
|
||||
# 跳过空单元格,找到第一个非字段名的值
|
||||
j = i + 1
|
||||
while j < len(row) and (not row[j] or not str(row[j]).strip() or ':' in str(row[j])):
|
||||
while j < len(row) and (
|
||||
not row[j] or not str(row[j]).strip() or ":" in str(row[j])
|
||||
):
|
||||
j += 1
|
||||
if j < len(row) and row[j] and not ':' in str(row[j]):
|
||||
if j < len(row) and row[j] and not ":" in str(row[j]):
|
||||
info[field_name] = str(row[j]).strip()
|
||||
# 跳过已处理的值,继续找下一个字段名
|
||||
i = j + 1
|
||||
@@ -240,14 +270,11 @@ class ExcelConverter:
|
||||
all_records = []
|
||||
|
||||
for order in orders:
|
||||
order_info = order['order_info']
|
||||
materials = order['materials']
|
||||
order_info = order["order_info"]
|
||||
materials = order["materials"]
|
||||
|
||||
for material in materials:
|
||||
record = {
|
||||
**order_info,
|
||||
**material
|
||||
}
|
||||
record = {**order_info, **material}
|
||||
all_records.append(record)
|
||||
|
||||
return pd.DataFrame(all_records)
|
||||
|
||||
Reference in New Issue
Block a user