feat(xq0085): 团购核对支持对比表与门店归属(v1.1.0)
This commit is contained in:
@@ -7,7 +7,8 @@
|
||||
抖音【报表】N~Z →【平台-抖音】A~M(数据第 5 行起)
|
||||
CRM【团购统计】A~I →【团购统计】A~I(同结构,数据第 2 行起)
|
||||
|
||||
**共享盘全程只读**,只写本地 ``_work/`` 副本;zip+XML 直接改值,不用 openpyxl save。
|
||||
**共享盘全程只读**,只写本地「过程表」里的马敏总表副本(两条分支共用一份);
|
||||
zip+XML 直接改值,不用 openpyxl save。
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
@@ -19,6 +20,7 @@ from xml.sax.saxutils import escape
|
||||
import openpyxl
|
||||
from openpyxl.utils import column_index_from_string as col
|
||||
from openpyxl.utils import get_column_letter as letter
|
||||
from openpyxl.utils.datetime import to_excel as _to_excel
|
||||
|
||||
from .. import config
|
||||
from . import share_copy
|
||||
@@ -70,12 +72,12 @@ def _sheet_xml_path(zf, sheet_name):
|
||||
|
||||
|
||||
def _master_path(day):
|
||||
return os.path.join(config.WORK_DIR, config.DAILY_WORK_DIR,
|
||||
return os.path.join(config.WORK_DIR,
|
||||
f'{config.LINK_BOOK}{day.month}.{day.day}.xlsx')
|
||||
|
||||
|
||||
def _local_path(day, keyword):
|
||||
return os.path.join(config.WORK_DIR, config.DAILY_WORK_DIR,
|
||||
return os.path.join(config.WORK_DIR,
|
||||
share_copy.table_name(keyword, day, config.SHARE_TABLE_EXT))
|
||||
|
||||
|
||||
@@ -90,11 +92,18 @@ def _read_values(path, sheet, c0, c1, start_row):
|
||||
if sheet not in wb.sheetnames:
|
||||
return []
|
||||
ws = wb[sheet]
|
||||
# 模板/导出的 dimension 可能没写(None / 1×1),read_only 下会读不到
|
||||
if (ws.max_row or 1) <= 1 or (ws.max_column or 1) <= 1:
|
||||
ws.reset_dimensions()
|
||||
rows = [list(row) for row in ws.iter_rows(min_row=start_row,
|
||||
min_col=col(c0), max_col=col(c1),
|
||||
values_only=True)]
|
||||
while rows and all(v is None for v in rows[-1]):
|
||||
rows.pop()
|
||||
# 报表 I~P 是固定行数的公式块,透视行变少时尾部会整段 #N/A,绝不能贴进总表
|
||||
rows = [r for r in rows
|
||||
if not (isinstance(r[0], str) and r[0].startswith('#'))
|
||||
and r[0] not in (None, '')]
|
||||
return rows
|
||||
finally:
|
||||
wb.close()
|
||||
@@ -105,6 +114,12 @@ def _make_cell(row_no, cnum, value, style=''):
|
||||
s_attr = f' s="{style}"' if style else ''
|
||||
if isinstance(value, bool):
|
||||
return f'<c r="{letters}{row_no}"{s_attr} t="b"><v>{int(value)}</v></c>'
|
||||
if isinstance(value, (datetime, date)):
|
||||
# 日期必须写 Excel 序列号并按原单元格日期格式显示;写成 inlineStr 会退化为文本,
|
||||
# 透视表按日期分组 / 日期匹配公式都会失效(19xx 序列号不影响,本表为 1900 日期系统)。
|
||||
serial = _to_excel(value)
|
||||
num = int(serial) if float(serial).is_integer() else serial
|
||||
return f'<c r="{letters}{row_no}"{s_attr}><v>{num}</v></c>'
|
||||
if isinstance(value, (int, float)):
|
||||
return f'<c r="{letters}{row_no}"{s_attr}><v>{value}</v></c>'
|
||||
return (f'<c r="{letters}{row_no}"{s_attr} t="inlineStr">'
|
||||
@@ -134,6 +149,18 @@ def _set_row_values(row_xml, row_no, values, lo, hi):
|
||||
return head + ''.join(cell for _, cell in keep) + '</row>'
|
||||
|
||||
|
||||
def _row_style_map(row_xml):
|
||||
"""解析单行 XML,返回 {列号: 样式 s 值}(用于新增行沿用上一行格式)。"""
|
||||
out = {}
|
||||
if not row_xml:
|
||||
return out
|
||||
for m in _CELL_RE.finditer(row_xml):
|
||||
sm = re.search(r'\bs="(\d+)"', m.group(3) or '')
|
||||
if sm:
|
||||
out[_col_num(m.group(1))] = sm.group(1)
|
||||
return out
|
||||
|
||||
|
||||
def _patch_sheet_xml(xml_text, rows, start_row, start_col):
|
||||
existing = {}
|
||||
for m in _ROW_RE.finditer(xml_text):
|
||||
@@ -159,9 +186,16 @@ def _patch_sheet_xml(xml_text, rows, start_row, start_col):
|
||||
_make_cell(row_no, cnum, value) for cnum, value in sorted(values.items())) + '</row>'
|
||||
if old:
|
||||
out = out.replace(old, new, 1)
|
||||
col_styles = {}
|
||||
if needed > last_existing:
|
||||
# 新增行没有原单元格可继承样式:向上回看最多 20 行,同列取最近的 s=,
|
||||
# 否则日期序列号会以「常规」格式显示成 4xxxx 这类裸数字。
|
||||
for row_no in range(last_existing, max(start_row - 1, last_existing - 20), -1):
|
||||
for cnum, style in _row_style_map(existing.get(row_no)).items():
|
||||
col_styles.setdefault(cnum, style)
|
||||
added = ''.join(
|
||||
f'<row r="{row_no}">' + ''.join(
|
||||
_make_cell(row_no, lo + offset, value)
|
||||
_make_cell(row_no, lo + offset, value, col_styles.get(lo + offset, ''))
|
||||
for offset, value in enumerate(rows[row_no - start_row]) if value is not None)
|
||||
+ '</row>'
|
||||
for row_no in range(last_existing + 1, max(needed, last_existing) + 1))
|
||||
@@ -195,8 +229,7 @@ def _write_master(path, jobs):
|
||||
if info.filename in patches:
|
||||
dst.writestr(zi, patches.pop(info.filename).encode('utf-8'))
|
||||
else:
|
||||
with zf.open(info) as fin, dst.open(zi, 'w', force_zip64=True) as fout:
|
||||
shutil.copyfileobj(fin, fout, 4 << 20)
|
||||
dst.writestr(zi, zf.read(info))
|
||||
for name, data in patches.items():
|
||||
dst.writestr(name, data.encode('utf-8'))
|
||||
finally:
|
||||
@@ -232,7 +265,7 @@ def run(day=None, douyin_dir='', crm_dir='', dry_run=False, jobs_filter=None):
|
||||
if not wanted or 'douyin' in wanted:
|
||||
dy = _local_path(target, config.DOUYIN_SHARE_KEYWORD)
|
||||
if not os.path.isfile(dy):
|
||||
dy = os.path.join(str(douyin_dir or config.DOUYIN_WORK_DIR).strip('"'),
|
||||
dy = os.path.join(str(douyin_dir or config.OUT_DIR).strip('"'),
|
||||
f'{config.DOUYIN_TABLE_PREFIX}{target.month}.{target.day}.xlsx')
|
||||
if os.path.isfile(dy):
|
||||
values = _read_values(dy, config.REPORT_SHEET, *config.DOUYIN_REPORT_COLS,
|
||||
|
||||
Reference in New Issue
Block a user