#!/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, align=None):
props = ''
if bold:
props += ''
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):
tcpr = '' % width
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))
BORDERS = (''
''
''
''
''
''
''
'')
def info_table(fields):
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, ''.join('' % w for w in widths), ''.join(rows)))
def tracking_table():
columns = [
('序号', 620), ('跟踪阶段', 1300), ('计划完成时间', 1400),
('实际完成时间', 1400), ('负责人', 1100), ('完成情况说明', 2600),
]
widths = [c[1] for c in columns]
header = row([cell(c[0], c[1], bold=True, shade='D9E2F3') for c in columns], header=True)
blank_rows = ''.join(
row([cell(str(index), widths[0])] + [cell('', w) for w in widths[1:]])
for index in range(1, 9)
)
return ('%s'
'%s%s%s'
% (BORDERS, ''.join('' % w for w in widths), header, blank_rows))
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''))
FIELDS = [
('项目编码', 'taskCode'), ('项目名称', 'taskName'),
('产品编码', 'productCode'), ('产品名称', 'productName'),
('版本号', 'version'), ('项目状态', 'status'),
('主设计师', 'chiefDesigner'), ('审核人', 'reviewer'),
('计划完成时间', 'planFinishTime'), ('制表日期', 'printDate'),
]
def build_document():
sign_tbl = (''
''
''
''
''
''
''
''
'%s'
'%s%s'
% (para('制表人:____________ 审核人:____________', size=18),
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_table(FIELDS) + '' + run('', size=12) + ''
+ para('跟踪记录(可手写补充)', bold=True, size=20, space_after=40)
+ tracking_table() + '' + run('', size=12) + ''
+ para('备注:${remark}', size=18)
+ sign_tbl + sect + '')
CONTENT_TYPES = '''
'''
ROOT_RELS = '''
'''
DOC_RELS = '''
'''
def main():
base = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..',
'yudao-module-mes', 'src', 'main', 'resources', 'templates', 'pd')
os.makedirs(base, exist_ok=True)
path = os.path.join(base, 'project-tracking.docx')
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())
z.writestr('word/_rels/document.xml.rels', DOC_RELS)
z.writestr('word/media/seal.png', tiny_png())
print(path)
if __name__ == '__main__':
main()