"""把 pandoc 默认参考样式改造成党政机关公文(GB/T 9704)风格: - 正文:仿宋 三号(16pt),首行缩进 2 字符,固定行距 29pt - 一级标题黑体、二级标题楷体、三级标题仿宋加粗,均三号 - 表格:细边框、表头灰底加粗、表内 9.5pt - 代码块(JSON 示例):等宽字体 9pt、浅灰底 - 新增公文专用样式:红头 GovRedHead、发文字号 GovDocNo、红色分隔线 GovRedRule、标题 GovTitle、副标题 GovTitleSub、落款 GovSignOff - 目录:TOCHeading / TOC1 / TOC2 用法: patch-styles.py <解压目录>/word/styles.xml """ import re import sys if len(sys.argv) != 2: raise SystemExit('用法: patch-styles.py <解压目录>/word/styles.xml') path = sys.argv[1] s = open(path, encoding='utf-8').read() FONT_FS = 'w:ascii="Times New Roman" w:eastAsia="仿宋" w:hAnsi="Times New Roman" w:cs="Times New Roman"' FONT_HT = 'w:ascii="Arial" w:eastAsia="黑体" w:hAnsi="Arial" w:cs="Arial"' FONT_KT = 'w:ascii="Times New Roman" w:eastAsia="楷体" w:hAnsi="Times New Roman" w:cs="Times New Roman"' FONT_MONO = 'w:ascii="Consolas" w:eastAsia="宋体" w:hAnsi="Consolas" w:cs="Consolas"' # ---------- 默认字体 ---------- s = re.sub(r']*minorEastAsia[^>]*/>', f'', s, flags=re.S) s = re.sub(r']*majorEastAsia[^>]*/>', f'', s, flags=re.S) # ---------- 标题主题色统一改黑 ---------- s = re.sub(r']*w:val="(?:0F4761|365F91|4F81BD)"[^>]*/>', '', s, flags=re.S) def pstyle(style_id, name, based, ppr, rpr, extra=''): """拼一个段落样式。子元素顺序:name/basedOn/qFormat/pPr/rPr。""" inherit = '' if based == style_id else (f'' f'') return (f'' f'' f'{inherit}' '' f'{extra}' f'{ppr}{rpr}') BODY_PPR = ('' '') BODY_RPR = f'' FLUSH_PPR = ('' '') # 标题:三号(sz 32),黑体 / 楷体 / 仿宋加粗,与正文同号,保持公文层级感 H1_PPR = ('' '' '') H1_RPR = f'' H2_PPR = ('' '' '') H2_RPR = f'' H3_PPR = ('' '' '') H3_RPR = (f'' '') def replace_style(style_id, xml): """按 styleId 整块替换样式;不存在则插到 之前。""" global s pat = re.compile(r']*w:styleId="%s"[^>]*>.*?' % style_id, re.S) if pat.search(s): s = pat.sub(lambda _m: xml, s, count=1) else: s = s.replace('', xml + '') # ---------- 正文与列表 ---------- replace_style('Normal', pstyle('Normal', 'Normal', 'Normal', BODY_PPR, BODY_RPR)) # pandoc 正文段落用 BodyText;列表与表格单元格用 Compact(不缩进) replace_style('BodyText', pstyle('BodyText', 'Body Text', 'Normal', BODY_PPR, BODY_RPR)) replace_style('Compact', pstyle('Compact', 'Compact', 'Normal', FLUSH_PPR, BODY_RPR)) replace_style('FirstParagraph', pstyle('FirstParagraph', 'First Paragraph', 'Normal', FLUSH_PPR, BODY_RPR)) # 有序/无序列表:缩进但不首行缩进 LIST_PPR = ('' '') replace_style('ListParagraph', pstyle('ListParagraph', 'List Paragraph', 'Normal', LIST_PPR, BODY_RPR)) # ---------- 标题 ---------- replace_style('Heading1', pstyle('Heading1', 'heading 1', 'Normal', H1_PPR, H1_RPR)) replace_style('Heading2', pstyle('Heading2', 'heading 2', 'Normal', H2_PPR, H2_RPR)) replace_style('Heading3', pstyle('Heading3', 'heading 3', 'Normal', H3_PPR, H3_RPR)) for i in (4, 5, 6): replace_style(f'Heading{i}', pstyle(f'Heading{i}', f'heading {i}', 'Normal', H3_PPR, H3_RPR)) # ---------- 代码块(JSON 示例) ---------- CODE_PPR = ('' '' '') CODE_RPR = (f'' '') replace_style('SourceCode', pstyle('SourceCode', 'Source Code', 'Normal', CODE_PPR, CODE_RPR)) CHAR_CODE = ('' '' f'' '') replace_style('VerbatimChar', CHAR_CODE) # ---------- 目录 ---------- TOC_HEAD_RPR = (f'' '') replace_style('TOCHeading', pstyle( 'TOCHeading', 'TOC Heading', 'Normal', '' '', TOC_HEAD_RPR)) TOC_TABS = '' replace_style('TOC1', pstyle( 'TOC1', 'toc 1', 'Normal', f'{TOC_TABS}' '', f'')) replace_style('TOC2', pstyle( 'TOC2', 'toc 2', 'Normal', f'{TOC_TABS}' '' '', f'')) # ---------- 公文要素 ---------- SBT = ('w:ascii="Times New Roman" w:eastAsia="方正小标宋简体" ' 'w:hAnsi="Times New Roman" w:cs="Times New Roman"') replace_style('GovRedHead', pstyle( 'GovRedHead', 'Gov Red Head', 'Normal', '' '', f'')) replace_style('GovDocNo', pstyle( 'GovDocNo', 'Gov Doc Number', 'Normal', '' '', f'')) replace_style('GovRedRule', pstyle( 'GovRedRule', 'Gov Red Rule', 'Normal', '' '' '', '')) replace_style('GovTitle', pstyle( 'GovTitle', 'Gov Title', 'Normal', '' '', f'')) replace_style('GovTitleSub', pstyle( 'GovTitleSub', 'Gov Title Sub', 'Normal', '' '', f'')) replace_style('GovSignOff', pstyle( 'GovSignOff', 'Gov Sign Off', 'Normal', '' '' '', f'')) # ---------- Table 样式:边框 + 表头底纹 + 9.5pt ---------- TABLE_STYLE = ''' ''' replace_style('Table', TABLE_STYLE) open(path, 'w', encoding='utf-8').write(s) print('参考样式已改写为公文风格')