"""Shared semantic document layout: paragraphs, headings, lists and action tables.""" import io,re def paragraphs(text): """Add whitespace boundaries only, preserving the words and numbered clauses.""" result=[] for paragraph in re.split(r'\n\s*\n',str(text).strip()): markers=list(re.finditer(r'[((]\d{1,2}[))]',paragraph)) pieces=[] if len(markers)>=2: starts=[0]+[m.start() for m in markers if m.start()>0] pieces=[paragraph[a:b].strip() for a,b in zip(starts,starts[1:]+[len(paragraph)])] else:pieces=[paragraph.strip()] for piece in pieces: while len(piece)>300: end=piece.rfind('。',100,300) if end<0:break result.append(piece[:end+1].strip());piece=piece[end+1:].strip() if piece:result.append(piece) return '\n\n'.join(result) def blocks(text): rows=[] def flush(): nonlocal rows value=rows;rows=[];return ('table',value) for raw in str(text).splitlines(): line=raw.strip() if line.startswith('|') and line.endswith('|'): cells=[v.strip() for v in re.split(r'(?0 and width==3 else 0 margin=OxmlElement('w:tcMar') for edge in ('top','left','bottom','right'): pad=OxmlElement('w:'+edge);pad.set(qn('w:w'),'90');pad.set(qn('w:type'),'dxa');margin.append(pad) cell._tc.get_or_add_tcPr().append(margin) runs(p,values[i] if i24 and '—' in title:title=re.sub(r'—+','\n',title,count=1) p=doc.add_paragraph(style='Title');p.alignment=1;p.paragraph_format.space_before=Pt(0);p.paragraph_format.space_after=Pt(15);runs(p,title) if len(title)>24: for r in p.runs:r.font.size=Pt(16) doc.core_properties.title=plain(title);title_done=True;continue if kind=='heading':p=doc.add_paragraph(style='Heading '+str(min(max(value[0]-1,1),3)));runs(p,value[1]) else:p=doc.add_paragraph(style='List Bullet' if kind=='bullet' else 'List Number' if kind=='number' else 'Normal');runs(p,value) footer=section.footer.paragraphs[0];footer.alignment=1;footer.add_run(f'Work OS · v{artifact["version"]} · '+('已核对' if artifact['approval_status']=='approved' else '待核对草稿')+' · 第 ') field=OxmlElement('w:fldSimple');field.set(qn('w:instr'),'PAGE');footer._p.append(field);footer.add_run(' 页') for run in footer.runs:run.font.size=Pt(9);run.font.color.rgb=RGBColor.from_string('708478') # Some bundled Word templates carry a decorative border on Title/Normal. for element in [style.element for style in doc.styles]+[p._p for p in doc.paragraphs]: for border in list(element.iter(qn('w:pBdr'))):border.getparent().remove(border) stream=io.BytesIO();doc.save(stream);return stream.getvalue() def pdf_bytes(artifact,font='STSong-Light'): from reportlab.platypus import SimpleDocTemplate,Paragraph,Spacer,Table,TableStyle from reportlab.lib.styles import ParagraphStyle from reportlab.lib.pagesizes import A4 from reportlab.lib import colors from xml.sax.saxutils import escape stream=io.BytesIO();doc=SimpleDocTemplate(stream,pagesize=A4,leftMargin=59,rightMargin=59,topMargin=62,bottomMargin=56) body=ParagraphStyle('body',fontName=font,fontSize=11,leading=18,wordWrap='CJK',spaceAfter=8) title=ParagraphStyle('title',parent=body,fontSize=17,leading=25,alignment=1,spaceAfter=16,keepWithNext=True) heading=ParagraphStyle('heading',parent=body,fontSize=13,leading=20,spaceBefore=12,spaceAfter=9,keepWithNext=True) cell=ParagraphStyle('cell',parent=body,fontSize=10,leading=15,spaceAfter=0) story=[];first=True for kind,value in blocks(artifact['content']): if kind=='table': width=max(len(row) for row in value);rows=[[Paragraph(escape(plain(row[i] if i