#!/usr/bin/env python3 # -*- coding: utf-8 -*- """生成 CRM 销售报价单 Word 模板(最小 OOXML 包)。 占位符:companyName / customerName / quotationNo / quotationName / quotationDate / validUntil / contactName / ownerUserName / productTotal / discountPrice / taxRate / taxPrice / totalPrice / totalUpper / remark 明细行占位符:index / itemName / itemSpecification / unitName / count / price / amount / itemRemark 签章图片: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, align_bold_color=None): props = '' if bold: props += '' if align_bold_color: props += '' % align_bold_color props += '' % size return '%s%s' % (props, text) def para(text, bold=False, size=20, align=None, space_after=0, color=None): ppr = '' if align: ppr += '' % align if space_after: ppr += '' % space_after ppr += '' return '%s%s' % (ppr, run(text, bold=bold, size=size, align_bold_color=color)) 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) # ---------- 1x1 透明 PNG(占位电子章,导出时替换) ---------- 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 seal_drawing(): return ( '' '' '' '' '' '' '' '' '' '' % A_NS) def build_document(): info_widths = [1300, 2200, 1300, 2200] item_widths = [620, 2450, 1500, 700, 950, 1150, 1300] info_rows = [ row([cell('客户名称', info_widths[0], shade='F2F2F2'), cell('${customerName}', info_widths[1] + info_widths[2] + info_widths[3], span=3, align='left')]), row([cell('报价单号', info_widths[0], shade='F2F2F2'), cell('${quotationNo}', info_widths[1], align='left'), cell('报价日期', info_widths[2], shade='F2F2F2'), cell('${quotationDate}', info_widths[3], align='left')]), row([cell('报价名称', info_widths[0], shade='F2F2F2'), cell('${quotationName}', info_widths[1], align='left'), cell('有效期至', info_widths[2], shade='F2F2F2'), cell('${validUntil}', info_widths[3], align='left')]), row([cell('联系人', info_widths[0], shade='F2F2F2'), cell('${contactName}', info_widths[1], align='left'), cell('负责人', info_widths[2], shade='F2F2F2'), cell('${ownerUserName}', info_widths[3], align='left')]), ] info_tbl = ('' '' '' '' '' '' '' '%s%s' % (table_grid(info_widths), ''.join(info_rows))) item_header = row([cell('序号', item_widths[0], bold=True, shade='D9E2F3'), cell('产品名称', item_widths[1], bold=True, shade='D9E2F3'), cell('规格型号', item_widths[2], bold=True, shade='D9E2F3'), cell('单位', item_widths[3], bold=True, shade='D9E2F3'), cell('数量', item_widths[4], bold=True, shade='D9E2F3'), cell('单价(元)', item_widths[5], bold=True, shade='D9E2F3'), cell('金额(元)', item_widths[6], bold=True, shade='D9E2F3')], header=True) item_template = row([cell('${index}', item_widths[0]), cell('${itemName}', item_widths[1], align='left'), cell('${itemSpecification}', item_widths[2]), cell('${unitName}', item_widths[3]), cell('${count}', item_widths[4]), cell('${price}', item_widths[5], align='right'), cell('${amount}', item_widths[6], align='right')]) total_rows = ''.join([ row([cell('产品金额', sum(item_widths[:6]), shade='F2F2F2'), cell('${productTotal}', item_widths[6], align='right')]), row([cell('优惠金额', sum(item_widths[:6]), shade='F2F2F2'), cell('${discountPrice}', item_widths[6], align='right')]), row([cell('税率 / 税额', sum(item_widths[:6]), shade='F2F2F2'), cell('${taxRate}% / ${taxPrice}', item_widths[6], align='right')]), row([cell('报价总额', sum(item_widths[:6]), bold=True, shade='F2F2F2'), cell('${totalPrice}', item_widths[6], bold=True, align='right')]), row([cell('大写金额', sum(item_widths[:6]), bold=True, shade='F2F2F2'), cell('${totalUpper}', item_widths[6], align='left')]), ]) item_tbl = ('' '' '' '' '' '' '' '%s%s%s%s' % (table_grid(item_widths), item_header, item_template, total_rows)) remark = ('' '备注:' + run('${remark}', size=18) + '') terms = ('' + run('报价说明:', bold=True, size=18) + '' '' + run('1. 本报价自报价日期起生效,有效期以“有效期至”为准,逾期请重新询价。', size=18) + '' '' + run('2. 以上价格为含税价,税率按表中列示;运输方式及运费另行约定。', size=18) + '' '' + run('3. 付款方式、交货周期以双方确认后的正式合同为准。', size=18) + '' '' + run('4. 本报价单经本公司盖章后生效,传真件、扫描件具有同等效力。', size=18) + '') sign_tbl = ('' '' '' '' '' '' '' '' '%s' '%s%s' % (terms, 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('报 价 单', bold=True, size=28, align='center', space_after=120) + info_tbl + '' + run('', size=12) + '' + para('产品明细', bold=True, size=20, space_after=40) + item_tbl + '' + run('', size=12) + '' + remark + '' + run('', size=12) + '' + sign_tbl + sect + '') CONTENT_TYPES = ''' ''' ROOT_RELS = ''' ''' DOC_RELS = ''' ''' def main(): base = os.path.dirname(os.path.abspath(__file__)) out = os.path.join(base, '..', '..', 'yudao-module-crm', 'src', 'main', 'resources', 'templates', 'quotation', 'sale-quotation.docx') os.makedirs(os.path.dirname(out), exist_ok=True) with zipfile.ZipFile(out, '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()) z.writestr('word/_rels/document.xml.rels', DOC_RELS) z.writestr('word/media/seal.png', tiny_png()) print(out) if __name__ == '__main__': main()