#!/usr/bin/env python3 # -*- coding: utf-8 -*- """生成 MES 发货单 / 成品出库单 Word 模板(最小 OOXML 包)。 导出时由后端替换 ${...} 槽位与 word/media/seal.png 电子章。 """ import os import struct import zipfile import zlib W_NS = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"' R_NS = 'xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"' WP_NS = 'xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"' A_NS = 'xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"' PIC_NS = 'xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"' def run(text, bold=False, size=20, color=None): props = '' if bold: props += '' if color: props += '' % color props += '' % size return '%s%s' % (props, text) def para(text, bold=False, size=20, align=None, space_after=0): ppr = '' if align: ppr += '' % align if space_after: ppr += '' % space_after ppr += '' return '%s%s' % (ppr, run(text, bold=bold, size=size)) def cell(text, width, bold=False, size=18, align='center', shade=None, span=None): tcpr = '' % width if span: tcpr += '' % span if shade: tcpr += '' % shade tcpr += '' return '%s%s' % (tcpr, para(text, bold=bold, size=size, align=align)) def row(cells, header=False): trpr = '' if header else '' return '%s%s' % (trpr, ''.join(cells)) def table_grid(widths): return '%s' % ''.join('' % w for w in widths) BORDERS = ('' '' '' '' '' '' '' '') def info_table(fields): """fields: [(label, placeholder)],两列一组。""" widths = [1300, 2200, 1300, 2200] rows = [] for i in range(0, len(fields), 2): first = fields[i] second = fields[i + 1] if i + 1 < len(fields) else ('', '') rows.append(row([ cell(first[0], widths[0], shade='F2F2F2'), cell('${%s}' % first[1], widths[1], align='left'), cell(second[0], widths[2], shade='F2F2F2'), cell('${%s}' % second[1] if second[1] else '', widths[3], align='left'), ])) return ('%s%s%s' % (BORDERS, table_grid(widths), ''.join(rows))) def item_table(columns): """columns: [(title, placeholder, width, align)]""" widths = [c[2] for c in columns] header = row([cell(c[0], c[2], bold=True, shade='D9E2F3') for c in columns], header=True) template = row([cell('${%s}' % c[1], c[2], align=c[3]) for c in columns]) return ('%s%s%s%s' % (BORDERS, table_grid(widths), header, template)) def seal_drawing(): return ( '' '' '' '' '' '' '' '' '' '' % A_NS) def tiny_png(): def chunk(tag, data): return struct.pack('>I', len(data)) + tag + data + struct.pack('>I', zlib.crc32(tag + data) & 0xffffffff) ihdr = struct.pack('>IIBBBBB', 1, 1, 8, 6, 0, 0, 0) raw = b'\x00\x00\x00\x00\x00' return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', ihdr) + chunk(b'IDAT', zlib.compress(raw)) + chunk(b'IEND', b'')) def build_document(title, fields, columns, footer_lines): sign_tbl = ('' '' '' '' '' '' '' '' '%s' '%s%s' % (''.join(para(line, size=18) for line in footer_lines), seal_drawing(), para('${companyName}', size=18, align='center'))) sect = ('' '') return ('' '' % (W_NS, R_NS, WP_NS, PIC_NS) + para('${companyName}', bold=True, size=32, align='center', space_after=60) + para(title, bold=True, size=28, align='center', space_after=120) + info_table(fields) + '' + run('', size=12) + '' + para('货物明细', bold=True, size=20, space_after=40) + item_table(columns) + '' + run('', size=12) + '' + para('备注:${remark}', size=18) + sign_tbl + sect + '') CONTENT_TYPES = ''' ''' ROOT_RELS = ''' ''' DOC_RELS = ''' ''' DELIVERY_FIELDS = [ ('客户名称', 'clientName'), ('发货单号', 'code'), ('发货日期', 'salesDate'), ('销售订单号', 'saleOrderCode'), ('收货人', 'recipientName'), ('联系电话', 'recipientTelephone'), ('收货地址', 'recipientAddress'), ('物流公司', 'logisticsCompany'), ('快递费', 'expressFee'), ('开单人', 'creator'), ] DELIVERY_COLUMNS = [ ('序号', 'index', 620, 'center'), ('产品名称', 'itemName', 2600, 'left'), ('规格型号', 'itemSpecification', 1600, 'center'), ('单位', 'unitName', 700, 'center'), ('发货数量', 'quantity', 1100, 'right'), ('批次号', 'batchCode', 1400, 'center'), ('备注', 'itemRemark', 1400, 'left'), ] OUTBOUND_FIELDS = [ ('客户名称', 'clientName'), ('出库单号', 'code'), ('出库日期', 'salesDate'), ('销售订单号', 'saleOrderCode'), ('发货单号', 'noticeCode'), ('联系人', 'contactName'), ('联系电话', 'contactTelephone'), ('收货地址', 'contactAddress'), ('物流公司', 'carrier'), ('运输单号', 'shippingNumber'), ] OUTBOUND_COLUMNS = [ ('序号', 'index', 620, 'center'), ('产品名称', 'itemName', 2400, 'left'), ('规格型号', 'itemSpecification', 1400, 'center'), ('单位', 'unitName', 650, 'center'), ('数量', 'quantity', 900, 'right'), ('单价', 'price', 1000, 'right'), ('金额', 'amount', 1100, 'right'), ('批次号', 'batchCode', 1300, 'center'), ] FOOTER = ['制单人:____________ 审核人:____________', '收货人签字:____________ 日期:________年____月____日'] def write_docx(path, title, fields, columns): os.makedirs(os.path.dirname(path), exist_ok=True) with zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED) as z: z.writestr('[Content_Types].xml', CONTENT_TYPES) z.writestr('_rels/.rels', ROOT_RELS) z.writestr('word/document.xml', build_document(title, fields, columns, FOOTER)) z.writestr('word/_rels/document.xml.rels', DOC_RELS) z.writestr('word/media/seal.png', tiny_png()) def main(): base = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', 'yudao-module-mes', 'src', 'main', 'resources', 'templates', 'wm') write_docx(os.path.join(base, 'sales-delivery.docx'), '发 货 单', DELIVERY_FIELDS, DELIVERY_COLUMNS) write_docx(os.path.join(base, 'product-outbound.docx'), '成 品 出 库 单', OUTBOUND_FIELDS, OUTBOUND_COLUMNS) print(base) if __name__ == '__main__': main()