patch-styles.py 10.8 KB
"""把 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'<w:rFonts\b[^>]*minorEastAsia[^>]*/>',
           f'<w:rFonts {FONT_FS} />', s, flags=re.S)
s = re.sub(r'<w:rFonts\b[^>]*majorEastAsia[^>]*/>',
           f'<w:rFonts {FONT_HT} />', s, flags=re.S)

# ---------- 标题主题色统一改黑 ----------
s = re.sub(r'<w:color\b[^>]*w:val="(?:0F4761|365F91|4F81BD)"[^>]*/>',
           '<w:color w:val="000000" />', 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'<w:basedOn w:val="{based}" />'
                                            f'<w:next w:val="{based}" />')
    return (f'<w:style w:type="paragraph" w:styleId="{style_id}">'
            f'<w:name w:val="{name}" />'
            f'{inherit}'
            '<w:qFormat />'
            f'{extra}'
            f'<w:pPr>{ppr}</w:pPr><w:rPr>{rpr}</w:rPr></w:style>')


BODY_PPR = ('<w:spacing w:before="0" w:after="0" w:line="560" w:lineRule="exact" />'
            '<w:ind w:firstLineChars="200" w:firstLine="640" /><w:jc w:val="both" />')
BODY_RPR = f'<w:rFonts {FONT_FS} /><w:color w:val="000000" /><w:sz w:val="32" /><w:szCs w:val="32" />'

FLUSH_PPR = ('<w:spacing w:before="0" w:after="0" w:line="420" w:lineRule="auto" />'
             '<w:ind w:firstLineChars="0" w:firstLine="0" />')

# 标题:三号(sz 32),黑体 / 楷体 / 仿宋加粗,与正文同号,保持公文层级感
H1_PPR = ('<w:keepNext /><w:spacing w:before="360" w:after="240" w:line="560" w:lineRule="exact" />'
          '<w:ind w:firstLineChars="0" w:firstLine="0" /><w:jc w:val="left" />'
          '<w:outlineLvl w:val="0" />')
H1_RPR = f'<w:rFonts {FONT_HT} /><w:color w:val="000000" /><w:sz w:val="32" /><w:szCs w:val="32" />'
H2_PPR = ('<w:keepNext /><w:spacing w:before="240" w:after="120" w:line="560" w:lineRule="exact" />'
          '<w:ind w:firstLineChars="0" w:firstLine="0" /><w:jc w:val="left" />'
          '<w:outlineLvl w:val="1" />')
H2_RPR = f'<w:rFonts {FONT_KT} /><w:color w:val="000000" /><w:sz w:val="32" /><w:szCs w:val="32" />'
H3_PPR = ('<w:keepNext /><w:spacing w:before="180" w:after="60" w:line="560" w:lineRule="exact" />'
          '<w:ind w:firstLineChars="0" w:firstLine="0" /><w:jc w:val="left" />'
          '<w:outlineLvl w:val="2" />')
H3_RPR = (f'<w:rFonts {FONT_FS} /><w:b /><w:bCs /><w:color w:val="000000" />'
          '<w:sz w:val="32" /><w:szCs w:val="32" />')


def replace_style(style_id, xml):
    """按 styleId 整块替换样式;不存在则插到 </w:styles> 之前。"""
    global s
    pat = re.compile(r'<w:style\b[^>]*w:styleId="%s"[^>]*>.*?</w:style>' % style_id, re.S)
    if pat.search(s):
        s = pat.sub(lambda _m: xml, s, count=1)
    else:
        s = s.replace('</w:styles>', xml + '</w:styles>')


# ---------- 正文与列表 ----------
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 = ('<w:spacing w:before="0" w:after="0" w:line="560" w:lineRule="exact" />'
            '<w:ind w:firstLineChars="0" w:firstLine="0" />')
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 = ('<w:spacing w:before="0" w:after="0" w:line="240" w:lineRule="auto" />'
            '<w:ind w:leftChars="0" w:left="120" w:firstLineChars="0" w:firstLine="0" />'
            '<w:jc w:val="left" /><w:shd w:val="clear" w:color="auto" w:fill="F5F5F5" />')
CODE_RPR = (f'<w:rFonts {FONT_MONO} /><w:color w:val="1A1A1A" />'
            '<w:sz w:val="18" /><w:szCs w:val="18" />')
replace_style('SourceCode', pstyle('SourceCode', 'Source Code', 'Normal', CODE_PPR, CODE_RPR))
CHAR_CODE = ('<w:style w:type="character" w:styleId="VerbatimChar">'
             '<w:name w:val="Verbatim Char" /><w:basedOn w:val="DefaultParagraphFont" />'
             f'<w:rPr><w:rFonts {FONT_MONO} /><w:sz w:val="20" /><w:szCs w:val="20" /></w:rPr>'
             '</w:style>')
replace_style('VerbatimChar', CHAR_CODE)

# ---------- 目录 ----------
TOC_HEAD_RPR = (f'<w:rFonts {FONT_HT} /><w:color w:val="000000" />'
                '<w:sz w:val="36" /><w:szCs w:val="36" />')
replace_style('TOCHeading', pstyle(
    'TOCHeading', 'TOC Heading', 'Normal',
    '<w:spacing w:before="240" w:after="360" w:line="560" w:lineRule="exact" />'
    '<w:ind w:firstLineChars="0" w:firstLine="0" /><w:jc w:val="center" />',
    TOC_HEAD_RPR))
TOC_TABS = '<w:tabs><w:tab w:val="right" w:leader="dot" w:pos="8800" /></w:tabs>'
replace_style('TOC1', pstyle(
    'TOC1', 'toc 1', 'Normal',
    f'{TOC_TABS}<w:spacing w:before="120" w:after="0" w:line="400" w:lineRule="auto" />'
    '<w:ind w:firstLineChars="0" w:firstLine="0" /><w:jc w:val="left" />',
    f'<w:rFonts {FONT_FS} /><w:sz w:val="28" /><w:szCs w:val="28" />'))
replace_style('TOC2', pstyle(
    'TOC2', 'toc 2', 'Normal',
    f'{TOC_TABS}<w:spacing w:before="0" w:after="0" w:line="400" w:lineRule="auto" />'
    '<w:ind w:leftChars="100" w:left="420" w:firstLineChars="0" w:firstLine="0" />'
    '<w:jc w:val="left" />',
    f'<w:rFonts {FONT_FS} /><w:sz w:val="26" /><w:szCs w:val="26" />'))

# ---------- 公文要素 ----------
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',
    '<w:spacing w:before="480" w:after="60" w:line="720" w:lineRule="exact" />'
    '<w:ind w:firstLineChars="0" w:firstLine="0" /><w:jc w:val="center" />',
    f'<w:rFonts {SBT} /><w:color w:val="FF0000" /><w:sz w:val="56" /><w:szCs w:val="56" />'))

replace_style('GovDocNo', pstyle(
    'GovDocNo', 'Gov Doc Number', 'Normal',
    '<w:spacing w:before="120" w:after="0" w:line="480" w:lineRule="exact" />'
    '<w:ind w:firstLineChars="0" w:firstLine="0" /><w:jc w:val="center" />',
    f'<w:rFonts {FONT_FS} /><w:sz w:val="32" /><w:szCs w:val="32" />'))

replace_style('GovRedRule', pstyle(
    'GovRedRule', 'Gov Red Rule', 'Normal',
    '<w:pBdr><w:bottom w:val="single" w:sz="18" w:space="1" w:color="FF0000" /></w:pBdr>'
    '<w:spacing w:before="0" w:after="360" w:line="240" w:lineRule="exact" />'
    '<w:ind w:firstLineChars="0" w:firstLine="0" />',
    '<w:sz w:val="12" /><w:szCs w:val="12" />'))

replace_style('GovTitle', pstyle(
    'GovTitle', 'Gov Title', 'Normal',
    '<w:spacing w:before="240" w:after="120" w:line="660" w:lineRule="exact" />'
    '<w:ind w:firstLineChars="0" w:firstLine="0" /><w:jc w:val="center" />',
    f'<w:rFonts {SBT} /><w:sz w:val="44" /><w:szCs w:val="44" />'))

replace_style('GovTitleSub', pstyle(
    'GovTitleSub', 'Gov Title Sub', 'Normal',
    '<w:spacing w:before="0" w:after="360" w:line="560" w:lineRule="exact" />'
    '<w:ind w:firstLineChars="0" w:firstLine="0" /><w:jc w:val="center" />',
    f'<w:rFonts {FONT_FS} /><w:sz w:val="32" /><w:szCs w:val="32" />'))

replace_style('GovSignOff', pstyle(
    'GovSignOff', 'Gov Sign Off', 'Normal',
    '<w:spacing w:before="120" w:after="0" w:line="560" w:lineRule="exact" />'
    '<w:ind w:rightChars="200" w:right="640" w:firstLineChars="0" w:firstLine="0" />'
    '<w:jc w:val="right" />',
    f'<w:rFonts {FONT_FS} /><w:sz w:val="32" /><w:szCs w:val="32" />'))

# ---------- Table 样式:边框 + 表头底纹 + 9.5pt ----------
TABLE_STYLE = '''<w:style w:type="table" w:default="1" w:styleId="Table">
    <w:name w:val="Table" />
    <w:basedOn w:val="TableNormal" />
    <w:semiHidden />
    <w:unhideWhenUsed />
    <w:qFormat />
    <w:rPr>
      <w:sz w:val="19" />
      <w:szCs w:val="19" />
    </w:rPr>
    <w:tblPr>
      <w:tblBorders>
        <w:top w:val="single" w:sz="4" w:space="0" w:color="7F7F7F" />
        <w:left w:val="single" w:sz="4" w:space="0" w:color="7F7F7F" />
        <w:bottom w:val="single" w:sz="4" w:space="0" w:color="7F7F7F" />
        <w:right w:val="single" w:sz="4" w:space="0" w:color="7F7F7F" />
        <w:insideH w:val="single" w:sz="4" w:space="0" w:color="BFBFBF" />
        <w:insideV w:val="single" w:sz="4" w:space="0" w:color="BFBFBF" />
      </w:tblBorders>
      <w:tblInd w:type="dxa" w:w="0" />
      <w:tblCellMar>
        <w:top w:type="dxa" w:w="50" />
        <w:left w:type="dxa" w:w="90" />
        <w:bottom w:type="dxa" w:w="50" />
        <w:right w:type="dxa" w:w="90" />
      </w:tblCellMar>
    </w:tblPr>
    <w:tblStylePr w:type="firstRow">
      <w:rPr>
        <w:b />
        <w:bCs />
      </w:rPr>
      <w:tcPr>
        <w:shd w:val="clear" w:color="auto" w:fill="EDEDED" />
        <w:tcBorders>
          <w:bottom w:val="single" w:sz="6" w:space="0" w:color="7F7F7F" />
        </w:tcBorders>
        <w:vAlign w:val="center" />
      </w:tcPr>
    </w:tblStylePr>
  </w:style>'''
replace_style('Table', TABLE_STYLE)

open(path, 'w', encoding='utf-8').write(s)
print('参考样式已改写为公文风格')