patch-styles.py
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""把 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('参考样式已改写为公文风格')