Files
37duAutomation/plugins/XQ0001-0100/XQ0031_XQ0032 物料收发与采购订单导出/report/merge_summary.py
T
puddy 42d3ad48d0 feat(plugin): XQ0031_XQ0032 1.1.8 物料收发汇总列结构可自定义(界面可视化编辑)
- 新增配置项 material_columns(JSON,留空=内置标准),logic/material_summary.resolve_columns
  解析并校验,配置错误在取数前 fail-fast,不生成半截文件
- 导出列按 2026-11-16 平台样本对齐:新版 12 列、数量列 0 值留空、数字格式、
  按「物料编码→仓库编码」排序、末行只汇总期初/收入/发出/结存
- 插件界面新增「05 导出列」可视化编辑表(表头/字段/数值/格式 + 上移下移删除/重置)
- 同步 README 与开发文档;重建 ui 产物
2026-11-16 14:17:17 +08:00

95 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""report 层 - 「物料收发汇总表」各档报表合并汇总(xq0031_xq0032 插件内模块)。
与原影刀版 merge_summary.py 同源:把同一保存目录下各档「物料收发汇总表_标签.xlsx」
合并为一个汇总文件「物料收发汇总表_<日期>.xlsx」:
- 每个报表第 4 行是表头;合并时第 4 行表头第一列新增「时间区间」字段
- 数据行第一列填入报表标签(60D / 30D / 自定义日期区间 ...)
- 跳过每表最后一行汇总行
返回汇总文件路径;无可合并报表返回 None。
"""
import re
from datetime import datetime
from pathlib import Path
import openpyxl
from .. import config
TAG = '[物料收发汇总表]'
def merge_summary(save_dir):
m = re.search(r'(\d{4}-\d{2}-\d{2})', str(save_dir))
date_tag = m.group(1) if m else datetime.now().strftime('%Y-%m-%d')
out = Path(save_dir) / f'{config.FILE_PREFIX_31}_{date_tag}.xlsx'
def _label(name):
m = re.match(rf'^{config.FILE_PREFIX_31}_(.+)\.xlsx$', name)
return m.group(1) if m else None
files = sorted(p for p in Path(save_dir).glob(f'{config.FILE_PREFIX_31}_*.xlsx')
if _label(p.name) and p.name != out.name)
if not files:
config.log(f'{TAG} 目录下没有可合并的报表: {save_dir}')
return None
header, fmts = None, None
all_rows, labels = [], []
for f in files:
label = _label(f.name)
try:
wb = openpyxl.load_workbook(f, data_only=True)
except Exception as e:
config.log(f'{TAG} [跳过] 无法读取的报表: {f.name}({e})')
continue
ws = wb[wb.sheetnames[0]]
h = [ws.cell(4, c).value for c in range(1, ws.max_column + 1)]
if header is None:
header = h
# 各列数字格式沿用单表数据行(与平台「引出」的数显一致,如 2,941.61)
fmts = [ws.cell(5, c).number_format if ws.max_row > 5 else 'General'
for c in range(1, ws.max_column + 1)]
elif h != header:
config.log(f'{TAG} [跳过] 表头不一致: {f.name}')
continue
# 数据从第 5 行起,跳过最后一行汇总行
n = 0
for r in range(5, ws.max_row):
row = [ws.cell(r, c).value for c in range(1, ws.max_column + 1)]
if any(v is not None and str(v).strip() != '' for v in row):
all_rows.append([label] + row)
n += 1
labels.append(label)
config.log(f'{TAG} [{label}] 数据 {n} 行 <- {f.name}')
wb.close()
if not all_rows:
config.log(f'{TAG} 没有可合并的数据')
return None
if out.exists():
out.unlink()
wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'Sheet1'
# 前 3 行说明,保持「表头在第 4 行」的结构与单报表一致
ws.cell(1, 1, '物料收发汇总表(汇总)')
ws.cell(2, 1, f'生成时间: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}')
ws.cell(3, 1, f'合并报表: {", ".join(labels)}')
# 第 4 行表头:第一列插入「时间区间」
ws.cell(4, 1, '时间区间')
for c, v in enumerate(header, start=2):
ws.cell(4, c, v)
# 数据(第一列为报表标签;数值列沿用单表数字格式)
for r, row in enumerate(all_rows, start=5):
for c, v in enumerate(row, start=1):
cell = ws.cell(r, c)
cell.value = v
if c >= 2 and fmts and c - 2 < len(fmts) and fmts[c - 2] not in (None, 'General'):
cell.number_format = fmts[c - 2]
wb.save(out)
wb.close()
config.log(f'{TAG} 已生成汇总文件: {out}(共 {len(all_rows)} 条数据,{len(labels)} 个报表)')
return out