|
|
@@ -1,6 +1,7 @@
|
|
1
|
1
|
"""
|
|
2
|
2
|
提取 Word 文档中所有样式的完整 XML 定义(不遗漏任何属性)。
|
|
3
|
3
|
同时提取编号格式定义(numbering.xml)以支持标题编号。
|
|
|
4
|
+同时提取页面设置(页边距、纸张大小、方向、页眉页脚等)。
|
|
4
|
5
|
目标文件: default.docx
|
|
5
|
6
|
"""
|
|
6
|
7
|
|
|
|
@@ -22,6 +23,13 @@ def emu_to_pt(emu) -> float | None:
|
|
22
|
23
|
return round(int(emu) / 12700, 2)
|
|
23
|
24
|
|
|
24
|
25
|
|
|
|
26
|
+def emu_to_twips(emu) -> int | None:
|
|
|
27
|
+ """EMU 转 twips (1 twips = 635 EMU)"""
|
|
|
28
|
+ if emu is None:
|
|
|
29
|
+ return None
|
|
|
30
|
+ return int(emu / 635)
|
|
|
31
|
+
|
|
|
32
|
+
|
|
25
|
33
|
def extract_font(style) -> dict | None:
|
|
26
|
34
|
"""通过 API 提取字体信息(作为便捷摘要)"""
|
|
27
|
35
|
try:
|
|
|
@@ -184,6 +192,152 @@ def extract_numbering(doc_path: Path) -> dict | None:
|
|
184
|
192
|
return None
|
|
185
|
193
|
|
|
186
|
194
|
|
|
|
195
|
+def infer_paper_size(width_twips: int, height_twips: int) -> str:
|
|
|
196
|
+ """根据页面尺寸推断纸张类型"""
|
|
|
197
|
+ # 常见纸张尺寸(twips)
|
|
|
198
|
+ # A4 (纵向): 210mm x 297mm = 11906 x 16838 twips
|
|
|
199
|
+ # A4 (横向): 297mm x 210mm = 16838 x 11906 twips
|
|
|
200
|
+ # Letter (纵向): 8.5" x 11" = 12240 x 15840 twips
|
|
|
201
|
+ # A3 (纵向): 297mm x 420mm = 16838 x 23811 twips
|
|
|
202
|
+
|
|
|
203
|
+ # 允许 ±100 twips 的误差
|
|
|
204
|
+ tolerance = 100
|
|
|
205
|
+
|
|
|
206
|
+ # A4 纵向
|
|
|
207
|
+ if abs(width_twips - 11906) < tolerance and abs(height_twips - 16838) < tolerance:
|
|
|
208
|
+ return "A4"
|
|
|
209
|
+ # A4 横向
|
|
|
210
|
+ elif abs(width_twips - 16838) < tolerance and abs(height_twips - 11906) < tolerance:
|
|
|
211
|
+ return "A4 (Landscape)"
|
|
|
212
|
+ # Letter 纵向
|
|
|
213
|
+ elif abs(width_twips - 12240) < tolerance and abs(height_twips - 15840) < tolerance:
|
|
|
214
|
+ return "Letter"
|
|
|
215
|
+ # A3 纵向
|
|
|
216
|
+ elif abs(width_twips - 16838) < tolerance and abs(height_twips - 23811) < tolerance:
|
|
|
217
|
+ return "A3"
|
|
|
218
|
+ else:
|
|
|
219
|
+ return "Custom"
|
|
|
220
|
+
|
|
|
221
|
+
|
|
|
222
|
+def extract_page_setup(doc_path: Path) -> dict:
|
|
|
223
|
+ """
|
|
|
224
|
+ 提取页面设置信息(页边距、纸张大小、方向、页眉页脚等)
|
|
|
225
|
+ 只提取第一个节(section)的设置(大多数文档只有一个节)
|
|
|
226
|
+ """
|
|
|
227
|
+ try:
|
|
|
228
|
+ doc = Document(str(doc_path))
|
|
|
229
|
+
|
|
|
230
|
+ # 检查是否有 section
|
|
|
231
|
+ if not doc.sections:
|
|
|
232
|
+ return {"sections": []}
|
|
|
233
|
+
|
|
|
234
|
+ # 只提取第一个 section
|
|
|
235
|
+ section = doc.sections[0]
|
|
|
236
|
+ sections_data = []
|
|
|
237
|
+ # 提取页边距(转换为 twips)
|
|
|
238
|
+ top_margin = emu_to_twips(section.top_margin)
|
|
|
239
|
+ bottom_margin = emu_to_twips(section.bottom_margin)
|
|
|
240
|
+ left_margin = emu_to_twips(section.left_margin)
|
|
|
241
|
+ right_margin = emu_to_twips(section.right_margin)
|
|
|
242
|
+ gutter = emu_to_twips(section.gutter)
|
|
|
243
|
+
|
|
|
244
|
+ # 提取纸张尺寸(转换为 twips)
|
|
|
245
|
+ page_width = emu_to_twips(section.page_width)
|
|
|
246
|
+ page_height = emu_to_twips(section.page_height)
|
|
|
247
|
+
|
|
|
248
|
+ # 提取方向
|
|
|
249
|
+ # orientation: 0 = PORTRAIT, 1 = LANDSCAPE
|
|
|
250
|
+ orientation = "portrait" if section.orientation == 0 else "landscape"
|
|
|
251
|
+
|
|
|
252
|
+ # 提取页眉页脚距离(转换为 twips)
|
|
|
253
|
+ header_distance = emu_to_twips(section.header_distance)
|
|
|
254
|
+ footer_distance = emu_to_twips(section.footer_distance)
|
|
|
255
|
+
|
|
|
256
|
+ # 提取首页不同设置
|
|
|
257
|
+ different_first_page = section.different_first_page_header_footer
|
|
|
258
|
+
|
|
|
259
|
+ # 从 XML 提取文档网格设置
|
|
|
260
|
+ # 注意:需要通过 section._sectPr 访问 XML 元素
|
|
|
261
|
+ grid_type = None
|
|
|
262
|
+ chars_per_line = None
|
|
|
263
|
+ lines_per_page = None
|
|
|
264
|
+
|
|
|
265
|
+ try:
|
|
|
266
|
+ # 尝试获取 section 的 XML 元素
|
|
|
267
|
+ if hasattr(section, '_sectPr'):
|
|
|
268
|
+ sectPr = section._sectPr
|
|
|
269
|
+ elif hasattr(section, 'element'):
|
|
|
270
|
+ sectPr = section.element
|
|
|
271
|
+ else:
|
|
|
272
|
+ sectPr = None
|
|
|
273
|
+
|
|
|
274
|
+ if sectPr is not None:
|
|
|
275
|
+ docGrid = sectPr.find(qn('w:docGrid'))
|
|
|
276
|
+
|
|
|
277
|
+ if docGrid is not None:
|
|
|
278
|
+ # 网格类型: default, lines, linesAndChars, snapToChars
|
|
|
279
|
+ grid_type = docGrid.get(qn('w:type'))
|
|
|
280
|
+
|
|
|
281
|
+ # linePitch: 每行的高度(用于计算行数)
|
|
|
282
|
+ # charSpace: 字符间距
|
|
|
283
|
+ line_pitch = docGrid.get(qn('w:linePitch'))
|
|
|
284
|
+ char_space = docGrid.get(qn('w:charSpace'))
|
|
|
285
|
+
|
|
|
286
|
+ # 注意:Word UI 显示的"每页行数"对应 linePitch
|
|
|
287
|
+ # "每行字符数"对应 charSpace
|
|
|
288
|
+ if line_pitch:
|
|
|
289
|
+ lines_per_page = int(line_pitch)
|
|
|
290
|
+ if char_space:
|
|
|
291
|
+ chars_per_line = int(char_space)
|
|
|
292
|
+ except Exception as e:
|
|
|
293
|
+ # 如果提取网格失败,继续(网格不是必需的)
|
|
|
294
|
+ pass
|
|
|
295
|
+
|
|
|
296
|
+ # 推断纸张大小
|
|
|
297
|
+ paper_size_inferred = infer_paper_size(page_width, page_height)
|
|
|
298
|
+
|
|
|
299
|
+ section_data = {
|
|
|
300
|
+ # 页边距(twips)
|
|
|
301
|
+ "top_margin": top_margin,
|
|
|
302
|
+ "bottom_margin": bottom_margin,
|
|
|
303
|
+ "left_margin": left_margin,
|
|
|
304
|
+ "right_margin": right_margin,
|
|
|
305
|
+ "gutter": gutter,
|
|
|
306
|
+
|
|
|
307
|
+ # 纸张(twips)
|
|
|
308
|
+ "page_width": page_width,
|
|
|
309
|
+ "page_height": page_height,
|
|
|
310
|
+ "orientation": orientation,
|
|
|
311
|
+
|
|
|
312
|
+ # 版式(twips)
|
|
|
313
|
+ "header_distance": header_distance,
|
|
|
314
|
+ "footer_distance": footer_distance,
|
|
|
315
|
+ "different_first_page": different_first_page,
|
|
|
316
|
+
|
|
|
317
|
+ # 文档网格(可选)
|
|
|
318
|
+ "grid_type": grid_type,
|
|
|
319
|
+ "chars_per_line": chars_per_line,
|
|
|
320
|
+ "lines_per_page": lines_per_page,
|
|
|
321
|
+
|
|
|
322
|
+ # 推断信息
|
|
|
323
|
+ "paper_size_inferred": paper_size_inferred,
|
|
|
324
|
+ }
|
|
|
325
|
+
|
|
|
326
|
+ sections_data.append(section_data)
|
|
|
327
|
+
|
|
|
328
|
+ return {
|
|
|
329
|
+ "sections": sections_data
|
|
|
330
|
+ }
|
|
|
331
|
+
|
|
|
332
|
+ except Exception as e:
|
|
|
333
|
+ print(f"警告: 提取页面设置失败: {e}")
|
|
|
334
|
+ import traceback
|
|
|
335
|
+ traceback.print_exc()
|
|
|
336
|
+ return {
|
|
|
337
|
+ "sections": []
|
|
|
338
|
+ }
|
|
|
339
|
+
|
|
|
340
|
+
|
|
187
|
341
|
def main():
|
|
188
|
342
|
print(f"读取文件: {DOC_PATH}")
|
|
189
|
343
|
if not DOC_PATH.exists():
|
|
|
@@ -194,12 +348,16 @@ def main():
|
|
194
|
348
|
|
|
195
|
349
|
# 提取编号格式
|
|
196
|
350
|
numbering_data = extract_numbering(DOC_PATH)
|
|
|
351
|
+
|
|
|
352
|
+ # 提取页面设置
|
|
|
353
|
+ page_setup_data = extract_page_setup(DOC_PATH)
|
|
197
|
354
|
|
|
198
|
355
|
result = {
|
|
199
|
356
|
"source_file": DOC_PATH.name,
|
|
200
|
357
|
"total_styles": len(styles_data),
|
|
201
|
358
|
"styles": styles_data,
|
|
202
|
|
- "numbering": numbering_data, # 新增:编号格式定义
|
|
|
359
|
+ "numbering": numbering_data, # 编号格式定义
|
|
|
360
|
+ "page_setup": page_setup_data, # 页面设置
|
|
203
|
361
|
}
|
|
204
|
362
|
|
|
205
|
363
|
OUTPUT_PATH.write_text(
|
|
|
@@ -212,9 +370,21 @@ def main():
|
|
212
|
370
|
print(f"✅ 已提取编号格式定义")
|
|
213
|
371
|
else:
|
|
214
|
372
|
print(f"ℹ️ 文档中没有编号格式")
|
|
|
373
|
+
|
|
|
374
|
+ # 打印页面设置摘要
|
|
|
375
|
+ if page_setup_data and page_setup_data.get("sections"):
|
|
|
376
|
+ section = page_setup_data["sections"][0]
|
|
|
377
|
+ print(f"✅ 已提取页面设置:")
|
|
|
378
|
+ print(f" - 纸张: {section.get('paper_size_inferred')} ({section.get('orientation')})")
|
|
|
379
|
+ print(f" - 页边距: 上{section.get('top_margin')} 下{section.get('bottom_margin')} "
|
|
|
380
|
+ f"左{section.get('left_margin')} 右{section.get('right_margin')} twips")
|
|
|
381
|
+ if section.get('grid_type'):
|
|
|
382
|
+ print(f" - 文档网格: {section.get('grid_type')} "
|
|
|
383
|
+ f"(每行{section.get('chars_per_line')}字符, 每页{section.get('lines_per_page')}行)")
|
|
|
384
|
+
|
|
215
|
385
|
print(f"完整定义已保存至: {OUTPUT_PATH}")
|
|
216
|
386
|
|
|
217
|
|
- # 打印摘要
|
|
|
387
|
+ # 打印样式类型分布
|
|
218
|
388
|
by_type: dict[str, list[str]] = {}
|
|
219
|
389
|
for s in styles_data:
|
|
220
|
390
|
t = s["type"]
|