This commit is contained in:
csj
2026-09-17 15:43:40 +08:00
parent 10697d3dfa
commit 45ccfd69be
11 changed files with 812 additions and 12 deletions
+3 -1
View File
@@ -188,6 +188,7 @@ class PreviewCalculator:
def prepare_preview(path):
from lxml import etree as ET
from spreadsheet_layout import read_layout
template = Template(path)
properties = template.workbook.find(tag('workbookPr'))
@@ -217,4 +218,5 @@ def prepare_preview(path):
with zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED) as output:
for name, data in template.parts.items():
output.writestr(name, data)
return dict(expensePreview='xlsx-quicklook-v1', previewUncalculatedCells=uncalculated)
return dict(expensePreview='xlsx-layout-v2', previewUncalculatedCells=uncalculated,
previewLayout=read_layout(path))
+272
View File
@@ -0,0 +1,272 @@
"""Read-only display geometry from the exact XLSX that will be saved."""
import datetime
import math
import mimetypes
import re
from decimal import Decimal, ROUND_HALF_UP
# Match the travel exporter: the app sandbox cannot read system MIME databases.
mimetypes.knownfiles = []
mimetypes.init()
from openpyxl.styles.colors import COLOR_INDEX
from openpyxl.styles.numbers import BUILTIN_FORMATS, is_date_format
from openpyxl.utils.datetime import from_excel, MAC_EPOCH, WINDOWS_EPOCH
from expense_template import Template, column_name, coordinate, tag, xml
DRAWING = 'http://schemas.openxmlformats.org/drawingml/2006/main'
MAX_CELLS = 50000
def flag(node, name, default=False):
return node.get(name, '1' if default else '0') in ('1', 'true')
def child_value(node, name, fallback):
child = node.find(tag(name))
return child.get('val', fallback) if child is not None else fallback
def theme_colors(template):
if 'xl/theme/theme1.xml' not in template.parts:
return ['FFFFFF', '000000', 'EEECE1', '1F497D', '4F81BD', 'C0504D',
'9BBB59', '8064A2', '4BACC6', 'F79646', '0000FF', '800080']
scheme = xml(template.parts['xl/theme/theme1.xml']).find('.//{' + DRAWING + '}clrScheme')
by_name = {entry.tag.split('}')[-1]: entry[0].get('lastClr', entry[0].get('val', '000000'))
for entry in scheme}
return [by_name.get(name, '000000') for name in
['lt1', 'dk1', 'lt2', 'dk2', 'accent1', 'accent2', 'accent3', 'accent4',
'accent5', 'accent6', 'hlink', 'folHlink']]
def color(node, palette, fallback='000000'):
if node is None:
return fallback
value = node.get('rgb')
if value is None and 'theme' in node.attrib:
value = palette[int(node.get('theme')) % len(palette)]
if value is None and 'indexed' in node.attrib:
index = int(node.get('indexed'))
value = COLOR_INDEX[index] if index < len(COLOR_INDEX) else fallback
value = (value or fallback)[-6:]
if not re.fullmatch('[0-9a-fA-F]{6}', value):
return fallback
tint = max(-1, min(1, float(node.get('tint', 0))))
return ''.join(f'{round(channel * (1 + tint) if tint < 0 else channel * (1 - tint) + 255 * tint):02X}'
for channel in (int(value[index:index + 2], 16) for index in (0, 2, 4)))
def literal(value):
return re.sub(r'"([^"]*)"|\\(.)|_.|\*.', lambda m: m[1] or m[2] or '', value)
def formatted(value, kind, code, date_1904, warnings):
if value == '' or kind in ('s', 'inlineStr', 'str', 'e'):
return value
if kind == 'b':
return 'TRUE' if value == '1' else 'FALSE'
number = Decimal(value)
if is_date_format(code):
date = from_excel(float(number), MAC_EPOCH if date_1904 else WINDOWS_EPOCH)
if isinstance(date, (datetime.datetime, datetime.time)):
# Excel date/time tokens are not strftime tokens (notably month/minute).
section = code.split(';')[0]
section = re.sub(r'\[[^\]]*\]', '', section)
section = literal(section)
if isinstance(date, datetime.datetime) and section.lower() == 'mm-dd-yy':
return date.strftime('%m-%d-%y')
if re.fullmatch(r'yyyy([/.-])m{1,2}\1d{1,2}', section, re.I):
sep = section[4]
return f'{date.year}{sep}{date.month:02d}{sep}{date.day:02d}' if 'mm' in section.lower() else f'{date.year}{sep}{date.month}{sep}{date.day}'
if section.lower() in ('yyyy""m""d""', 'yyyy年m月d日'):
return f'{date.year}{date.month}{date.day}'
if section.lower() in ('h:mm', 'hh:mm', 'h:mm:ss', 'hh:mm:ss'):
return date.strftime('%H:%M:%S' if 'ss' in section.lower() else '%H:%M')
warnings.add('部分日期格式使用标准日期显示')
return date.date().isoformat() if isinstance(date, datetime.datetime) else date.isoformat()
if code.lower() in ('general', '@'):
return format(number, 'f').rstrip('0').rstrip('.') if '.' in str(number) else str(number)
sections = re.split(r';(?=(?:[^"]*"[^"]*")*[^"]*$)', code)
section = sections[2] if number == 0 and len(sections) > 2 else sections[1] if number < 0 and len(sections) > 1 else sections[0]
section = re.sub(r'\[\$([^\]-]*)[^\]]*\]', lambda m: m[1], section)
if re.search(r'\[(?:[<>=]|\d)', section):
warnings.add('部分条件数字格式使用原始数值显示')
return str(number)
section = re.sub(r'\[[^\]]*\]', '', section)
# Ignore placeholders inside quoted/escaped literals.
masked = re.sub(r'"[^"]*"|\\.|_.|\*.', lambda m: ' ' * len(m[0]), section)
match = re.search(r'[#0][#0,]*(?:\.[#0?]+)?%?', masked)
if not match:
return literal(section).strip().replace('?', '')
if re.search(r'[Ee][+-]|[/]', masked) or masked[match.end():].lstrip().startswith(','):
warnings.add('部分科学计数或分数格式使用原始数值显示')
return str(number)
pattern = match[0]
places = len(pattern.rstrip('%').split('.')[1]) if '.' in pattern else 0
value = abs(number) if number < 0 and len(sections) > 1 else number
if '%' in pattern:
value *= 100
value = value.quantize(Decimal(1).scaleb(-places), rounding=ROUND_HALF_UP)
text = format(value, (',' if ',' in pattern else '') + f'.{places}f')
if '.' in pattern:
fraction = pattern.rstrip('%').split('.')[1]
optional = len(fraction) - len(fraction.rstrip('#?'))
for _ in range(optional):
if text.endswith('0'):
text = text[:-1]
text = text.rstrip('.')
if ',' not in pattern and '.' not in pattern:
text = text.zfill(pattern.count('0'))
if '%' in pattern:
text += '%'
return literal(section[:match.start()]) + text + literal(section[match.end():])
def read_styles(template, palette):
root = xml(template.parts['xl/styles.xml'])
fonts, fills, borders = root.find(tag('fonts')), root.find(tag('fills')), root.find(tag('borders'))
formats = dict(BUILTIN_FORMATS)
for item in root.findall(tag('numFmts') + '/' + tag('numFmt')):
formats[int(item.get('numFmtId'))] = item.get('formatCode')
styles = []
for entry in root.find(tag('cellXfs')):
font = fonts[int(entry.get('fontId', 0))]
fill = fills[int(entry.get('fillId', 0))].find(tag('patternFill'))
border = borders[int(entry.get('borderId', 0))]
alignment = entry.find(tag('alignment'))
if alignment is None:
alignment = {}
sides = {}
for edge in ('left', 'right', 'top', 'bottom'):
side = border.find(tag(edge))
if side is not None and side.get('style'):
sides[edge] = dict(style=side.get('style'), color=color(side.find(tag('color')), palette))
styles.append(dict(
font=child_value(font, 'name', 'Calibri'), fontSize=float(child_value(font, 'sz', '11')),
bold=font.find(tag('b')) is not None and flag(font.find(tag('b')), 'val', True),
italic=font.find(tag('i')) is not None and flag(font.find(tag('i')), 'val', True),
underline=font.find(tag('u')) is not None and child_value(font, 'u', 'single') != 'none',
strike=font.find(tag('strike')) is not None and flag(font.find(tag('strike')), 'val', True),
color=color(font.find(tag('color')), palette),
fill=color(fill.find(tag('fgColor')), palette, 'FFFFFF') if fill is not None and fill.get('patternType') == 'solid' else 'FFFFFF',
horizontal=alignment.get('horizontal', 'general'), vertical=alignment.get('vertical', 'bottom'),
wrap=alignment.get('wrapText') in ('1', 'true'), shrink=alignment.get('shrinkToFit') in ('1', 'true'),
rotation=int(alignment.get('textRotation', 0)), indent=float(alignment.get('indent', 0)),
borders=sides, numberFormat=formats.get(int(entry.get('numFmtId', 0)), 'General')
))
return styles
def read_layout(path):
template = Template(path)
palette = theme_colors(template)
styles = read_styles(template, palette)
properties = template.workbook.find(tag('workbookPr'))
date_1904 = properties is not None and flag(properties, 'date1904')
warnings = set()
sheets, total_cells = [], 0
for sheet_index, sheet in enumerate(template.sheets):
if sheet.get('state', 'visible') != 'visible':
continue
name = sheet.get('name')
_, _, document = template.sheet(name)
cells = {cell.get('r'): cell for cell in document.iter(tag('c'))}
merges = [item.get('ref') for item in document.iter(tag('mergeCell'))]
# Empty formatted tail columns must not squeeze the actual form. Preserve
# all nonempty cells, even when they are outside the workbook print area.
references = [ref for ref, cell in cells.items() if cell.find(tag('v')) is not None or
cell.find(tag('is')) is not None or cell.find(tag('f')) is not None]
ranges = []
for definition in template.workbook.findall(tag('definedNames') + '/' + tag('definedName')):
if definition.get('name') == '_xlnm.Print_Area' and definition.get('localSheetId') == str(sheet_index):
ranges += re.findall(r'\$?([A-Z]+)\$?(\d+):\$?([A-Z]+)\$?(\d+)', definition.text or '')
references += [f'{c}{r}' for a, b, c, r in ranges]
references += [entry.split(':')[-1] for entry in merges]
if not references:
references = list(cells) or ['A1']
positions = [coordinate(ref) for ref in references]
columns, rows = max(p[0] for p in positions), max(p[1] for p in positions)
if columns * rows > MAX_CELLS or total_cells + len(cells) > MAX_CELLS:
raise ValueError('模板范围过大,无法完整预览;请精简模板后重试')
total_cells += len(cells)
defaults = document.find(tag('sheetFormatPr'))
default_width = float(defaults.get('defaultColWidth', 8.43)) if defaults is not None else 8.43
default_height = float(defaults.get('defaultRowHeight', 15)) if defaults is not None else 15
widths = [default_width] * columns
heights = [default_height] * rows
for column in document.findall(tag('cols') + '/' + tag('col')):
for index in range(int(column.get('min')) - 1, min(columns, int(column.get('max')))):
widths[index] = 0 if flag(column, 'hidden') else float(column.get('width', default_width))
for row in document.findall(tag('sheetData') + '/' + tag('row')):
index = int(row.get('r')) - 1
if index < rows:
heights[index] = 0 if flag(row, 'hidden') else float(row.get('ht', default_height))
# OOXML width is expressed in maximum-digit units. Geometry is kept at
# 96 dpi; Swift scales the complete sheet, never individual columns.
widths = [math.floor(((256 * width + 18) / 256) * 7) for width in widths]
heights = [height * 4 / 3 for height in heights]
xs, ys = [0], [0]
for width in widths:
xs.append(xs[-1] + width)
for height in heights:
ys.append(ys[-1] + height)
covered, spans = set(), {}
for merged in merges:
first, last = merged.split(':')
left, top = coordinate(first)
right, bottom = coordinate(last)
spans[first] = (right, bottom)
covered.update((col, row) for col in range(left, right + 1) for row in range(top, bottom + 1)
if (col, row) != (left, top))
output = []
occupied = {coordinate(ref) for ref, cell in cells.items()
if cell.find(tag('v')) is not None or cell.find(tag('is')) is not None
or cell.find(tag('f')) is not None}
occupied.update(covered)
occupied.update(coordinate(ref) for ref in spans)
for reference, cell in cells.items():
col, row = coordinate(reference)
if col > columns or row > rows or (col, row) in covered:
continue
right, bottom = spans.get(reference, (col, row))
style = int(cell.get('s', 0))
appearance = dict(styles[style])
appearance['borders'] = dict(appearance['borders'])
# Merged right/bottom edges may be stored on the last cell.
for edge, edge_ref in [('right', column_name(right) + str(row)),
('bottom', column_name(col) + str(bottom)),
('right', column_name(right) + str(bottom)),
('bottom', column_name(right) + str(bottom))]:
if edge not in appearance['borders'] and edge_ref in cells:
candidate = styles[int(cells[edge_ref].get('s', 0))]['borders'].get(edge)
if candidate:
appearance['borders'][edge] = candidate
kind = cell.get('t', 'n')
value = cell.findtext(tag('v'), '')
if kind == 's':
value = template.shared[int(value)] if value else ''
elif kind == 'inlineStr':
value = ''.join(node.text or '' for node in cell.iter(tag('t')))
text = formatted(value, kind, appearance['numberFormat'], date_1904, warnings)
if appearance['horizontal'] == 'general':
appearance['horizontal'] = 'right' if kind == 'n' else 'center' if kind == 'b' else 'left'
width, height = xs[right] - xs[col - 1], ys[bottom] - ys[row - 1]
if width > 0 and height > 0:
left_limit, right_limit = col, right
if (text and kind in ('s', 'inlineStr', 'str') and reference not in spans
and not appearance['wrap'] and not appearance['shrink'] and appearance['rotation'] == 0):
if appearance['horizontal'] in ('left', 'center'):
while right_limit < columns and (right_limit + 1, row) not in occupied:
right_limit += 1
if appearance['horizontal'] in ('right', 'center'):
while left_limit > 1 and (left_limit - 1, row) not in occupied:
left_limit -= 1
output.append(dict(reference=reference, x=xs[col - 1], y=ys[row - 1], width=width, height=height,
text=text, style=appearance,
overflowLeft=xs[left_limit - 1], overflowRight=xs[right_limit]))
for feature in ('drawing', 'legacyDrawing', 'conditionalFormatting'):
if document.find(tag(feature)) is not None:
warnings.add('模板中的图片、图表或条件格式请在 Excel / WPS 中核对')
sheets.append(dict(name=name, width=max(1, xs[-1]), height=max(1, ys[-1]), cells=output))
return dict(version=1, sheets=sheets, warnings=sorted(warnings))
+3 -2
View File
@@ -36,7 +36,7 @@ class ExpensePreviewTests(unittest.TestCase):
self.assertEqual(formulas.active['H29'].value, '=IF(F27>C27,F27-C27,0)')
self.assertEqual(result['previewUncalculatedCells'], [])
self.assertEqual(sheet['D29'].value, '伍佰肆拾肆元叁角整')
self.assertEqual(result['expensePreview'], 'xlsx-quicklook-v1')
self.assertEqual(result['expensePreview'], 'xlsx-layout-v2')
values.close()
formulas.close()
@@ -105,7 +105,8 @@ class ExpensePreviewTests(unittest.TestCase):
process = subprocess.run(command, input=json.dumps(request) + '\n', text=True, capture_output=True, timeout=60)
self.assertEqual(process.returncode, 0, process.stderr + process.stdout)
result = next(json.loads(line)['result'] for line in process.stdout.splitlines() if json.loads(line).get('event') == 'result')
self.assertEqual(result['expensePreview'], 'xlsx-quicklook-v1')
self.assertEqual(result['expensePreview'], 'xlsx-layout-v2')
self.assertTrue(result['previewLayout']['sheets'])
self.assertEqual(result['expenseRowCount'], 2)
workbook = load_workbook(output, data_only=True)
self.assertEqual(workbook.active['F27'].value, 544.3)
@@ -0,0 +1,143 @@
import json
import os
from pathlib import Path
import sys
import subprocess
import unittest
from lxml import etree as ET
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import test_expense_template as fixtures
from expense_preview import prepare_preview
from expense_template import Template, encoded, tag
from spreadsheet_layout import formatted, read_layout
class SpreadsheetLayoutTests(unittest.TestCase):
def setUp(self):
self.fixture = fixtures.ExpenseTemplateTests()
self.fixture.setUp()
self.addCleanup(self.fixture.tearDown)
def test_fresh_import_does_not_read_system_mime_database(self):
code = """
import sys
def audit(event, args):
if event == 'open' and str(args[0]).endswith('mime.types'):
raise PermissionError('System MIME database is unavailable in the app sandbox')
sys.addaudithook(audit)
import spreadsheet_layout
"""
result = subprocess.run([sys.executable, '-c', code],
cwd=Path(__file__).resolve().parents[1], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stderr)
def test_actual_workbook_geometry_styles_and_values(self):
output, _ = self.fixture.export(payee=dict(recipient='测试收款人', bankName='测试银行测试支行',
accountNumber='001234567890'))
result = prepare_preview(output)
before = output.read_bytes()
layout = read_layout(output)
self.assertEqual(before, output.read_bytes())
self.assertEqual(layout, result['previewLayout'])
sheet = layout['sheets'][0]
cells = {cell['reference']: cell for cell in sheet['cells']}
self.assertEqual(cells['C23']['text'], '测试收款人')
self.assertEqual(cells['C25']['text'], '001234567890')
self.assertEqual(cells['F27']['text'].strip(), '544.30')
self.assertEqual(cells['D29']['text'], '伍佰肆拾肆元叁角整')
self.assertNotIn('D23', cells)
self.assertAlmostEqual(cells['C23']['x'] + cells['C23']['width'], sheet['width'])
self.assertEqual(cells['H30']['style']['rotation'], 255)
self.assertGreater(cells['A30']['overflowRight'], cells['A30']['x'] + cells['A30']['width'])
self.assertLessEqual(cells['A30']['overflowRight'], cells['D30']['x'])
_, _, document = Template(output).sheet(sheet['name'])
row = document.find(tag('sheetData') + '/' + tag('row') + '[@r="30"]')
self.assertAlmostEqual(cells['A30']['height'], float(row.get('ht')) * 4 / 3)
self.assertAlmostEqual(cells['H30']['height'], sum(cells[ref]['height'] for ref in ['A30', 'A31', 'A32']))
self.assertEqual(cells['F27']['style']['horizontal'], 'center')
self.assertEqual(cells['F27']['style']['font'], 'SimSun-ExtB')
self.assertEqual(cells['C23']['style']['borders']['right']['style'], 'medium')
self.assertLess(sheet['width'], 1200)
self.assertGreater(sheet['height'], 1200)
if os.environ.get('RECEIPT_PREVIEW_FIXTURE_DIR'):
folder = Path(os.environ['RECEIPT_PREVIEW_FIXTURE_DIR'])
folder.mkdir(parents=True, exist_ok=True)
(folder / 'layout.json').write_text(json.dumps(layout, ensure_ascii=False), encoding='utf-8')
(folder / 'preview.xlsx').write_bytes(before)
def test_all_visible_sheets_and_pagination_remain_available(self):
template, mapping = self.fixture.alternate()
state = dict(matches=self.fixture.state['matches'] + [self.fixture.match('third', '住宿', '50')])
output, _ = self.fixture.export(mapping=mapping, template=template, state=state)
layout = prepare_preview(output)['previewLayout']
self.assertEqual([sheet['name'] for sheet in layout['sheets']], ['组 B 报销', '保留说明', '组 B 报销-续2'])
notes = layout['sheets'][1]
self.assertEqual(notes['cells'][0]['text'], '保持这个工作表不变')
self.assertTrue(all(sheet['width'] > 0 and sheet['height'] > 0 for sheet in layout['sheets']))
def test_hidden_rows_columns_and_cells_outside_print_area(self):
output, _ = self.fixture.export()
template = Template(output)
_, path, document = template.sheet('个人报销单')
document.find(tag('cols'))[0].set('hidden', '1')
row = document.find(tag('sheetData'))[0]
row.set('hidden', '1')
from expense_template import set_cell
set_cell(document, 'J36', '不能遗漏打印区域以外的内容')
template.parts[path] = encoded(document)
self.fixture.rewrite(output, template.parts)
sheet = read_layout(output)['sheets'][0]
cells = {cell['reference']: cell for cell in sheet['cells']}
self.assertNotIn('A9', cells)
self.assertNotIn('A1', cells)
self.assertEqual(cells['B9']['x'], 0)
self.assertIn('J36', cells)
def test_oversized_range_fails_instead_of_truncating(self):
output, _ = self.fixture.export()
template = Template(output)
_, path, document = template.sheet('个人报销单')
from expense_template import set_cell
set_cell(document, 'IV2000', 'too large')
template.parts[path] = encoded(document)
self.fixture.rewrite(output, template.parts)
with self.assertRaisesRegex(ValueError, '范围过大'):
read_layout(output)
def test_drawings_and_conditional_formatting_have_explicit_warning(self):
output, _ = self.fixture.export()
template = Template(output)
_, path, document = template.sheet('个人报销单')
ET.SubElement(document, tag('conditionalFormatting'), sqref='H9')
template.parts[path] = encoded(document)
self.fixture.rewrite(output, template.parts)
self.assertTrue(read_layout(output)['warnings'])
def test_number_formats_and_text_identity(self):
warnings = set()
for value, kind, code, expected in [
('2122.60', 'n', '#,##0.00', '2,122.60'),
('662.8', 'n', '0.00_ ', '662.80'),
('0', 'n', '#,##0.00;(#,##0.00);"-"', '-'),
('-25.3', 'n', '#,##0.00;(#,##0.00)', '(25.30)'),
('0.125', 'n', '0.0%', '12.5%'),
('8', 'n', '0000', '0008'),
('80', 'n', '"¥"#,##0.00', '¥80.00'),
('001234567890123456789', 'inlineStr', 'General', '001234567890123456789'),
('=NotAFormula', 'inlineStr', 'General', '=NotAFormula'),
('1', 'b', 'General', 'TRUE'),
('12.50', 'n', 'General', '12.5'),
('12.50', 'n', '0.##', '12.5'),
('12', 'n', '0.0#', '12.0'),
('1', 'n', 'mm-dd-yy', '01-01-00'),
]:
with self.subTest(value=value, code=code):
self.assertEqual(formatted(value, kind, code, False, warnings), expected)
self.assertEqual(formatted('1', 'n', 'yyyy/m/d', True, warnings), '1904/1/2')
self.assertFalse(warnings)
if __name__ == '__main__':
unittest.main()