|
|
@@ -1,5 +1,6 @@
|
|
1
|
1
|
"""export_service.py — 将文档 Markdown 内容转换为 .doc 文件并返回永久下载链接。"""
|
|
2
|
2
|
|
|
|
3
|
+import base64
|
|
3
|
4
|
import io
|
|
4
|
5
|
import json
|
|
5
|
6
|
import time
|
|
|
@@ -10,6 +11,7 @@ from typing import Optional
|
|
10
|
11
|
|
|
11
|
12
|
import mistune
|
|
12
|
13
|
from docx import Document
|
|
|
14
|
+from docx.enum.text import WD_ALIGN_PARAGRAPH
|
|
13
|
15
|
from docx.oxml import OxmlElement
|
|
14
|
16
|
from docx.oxml.ns import qn
|
|
15
|
17
|
from docx.shared import Pt, RGBColor
|
|
|
@@ -91,6 +93,42 @@ def inject_styles_from_json(doc: Document, style_data: dict) -> None:
|
|
91
|
93
|
styles_element.append(new_elem)
|
|
92
|
94
|
|
|
93
|
95
|
|
|
|
96
|
+def inject_numbering_from_json(doc: Document, style_data: dict) -> None:
|
|
|
97
|
+ """
|
|
|
98
|
+ 将 JSON 中的 numbering 定义注入到文档中。
|
|
|
99
|
+ 这样可以恢复标题的编号格式。
|
|
|
100
|
+ """
|
|
|
101
|
+ numbering_def = style_data.get("numbering")
|
|
|
102
|
+ if not numbering_def:
|
|
|
103
|
+ return # 没有编号定义,跳过
|
|
|
104
|
+
|
|
|
105
|
+ try:
|
|
|
106
|
+ # 将字典转换为 lxml Element
|
|
|
107
|
+ numbering_elem = dict_to_element(numbering_def)
|
|
|
108
|
+
|
|
|
109
|
+ # 获取文档的 numbering part
|
|
|
110
|
+ # python-docx 可能没有 numbering part,需要创建
|
|
|
111
|
+ if doc.part.numbering_part is None:
|
|
|
112
|
+ # 创建 numbering part
|
|
|
113
|
+ from docx.opc.constants import CONTENT_TYPE as CT
|
|
|
114
|
+ from docx.opc.part import XmlPart
|
|
|
115
|
+ from docx.opc.packuri import PackURI
|
|
|
116
|
+
|
|
|
117
|
+ numbering_part = XmlPart(
|
|
|
118
|
+ PackURI('/word/numbering.xml'),
|
|
|
119
|
+ CT.WML_NUMBERING,
|
|
|
120
|
+ numbering_elem,
|
|
|
121
|
+ doc.part.package
|
|
|
122
|
+ )
|
|
|
123
|
+ doc.part.relate_to(numbering_part, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering')
|
|
|
124
|
+ else:
|
|
|
125
|
+ # 替换现有的 numbering part 内容
|
|
|
126
|
+ doc.part.numbering_part._element = numbering_elem
|
|
|
127
|
+ except Exception as e:
|
|
|
128
|
+ # 编号注入失败,不影响其他功能
|
|
|
129
|
+ print(f"警告: 编号格式注入失败: {e}")
|
|
|
130
|
+
|
|
|
131
|
+
|
|
94
|
132
|
def _resolve_style_id(style_map: dict, *keys: str) -> Optional[str]:
|
|
95
|
133
|
for key in keys:
|
|
96
|
134
|
entry = style_map.get(key)
|
|
|
@@ -111,13 +149,53 @@ class DocxRenderer(mistune.BaseRenderer):
|
|
111
|
149
|
self.style_map = style_map
|
|
112
|
150
|
self.doc = Document()
|
|
113
|
151
|
inject_styles_from_json(self.doc, style_data)
|
|
|
152
|
+ inject_numbering_from_json(self.doc, style_data) # 新增:注入编号格式
|
|
114
|
153
|
self._normal_id: Optional[str] = _resolve_style_id(style_map, "Normal", "1")
|
|
|
154
|
+ self.pending_style: Optional[str] = None # 待应用的样式名
|
|
|
155
|
+ self.pending_image_style: Optional[dict] = None # 待应用的图片样式
|
|
115
|
156
|
|
|
116
|
157
|
def _get_style_by_id(self, style_id: str):
|
|
117
|
158
|
for style in self.doc.styles:
|
|
118
|
159
|
if style.style_id == style_id:
|
|
119
|
160
|
return style
|
|
120
|
161
|
raise KeyError(style_id)
|
|
|
162
|
+
|
|
|
163
|
+ def _apply_numbering_from_style(self, paragraph):
|
|
|
164
|
+ """
|
|
|
165
|
+ 从段落的样式中提取编号属性并应用到段落。
|
|
|
166
|
+ 这是必需的,因为 python-docx 不会自动继承样式的编号格式。
|
|
|
167
|
+ """
|
|
|
168
|
+ if not paragraph.style:
|
|
|
169
|
+ return
|
|
|
170
|
+
|
|
|
171
|
+ try:
|
|
|
172
|
+ # 获取样式的 XML 元素
|
|
|
173
|
+ style_elem = paragraph.style.element
|
|
|
174
|
+
|
|
|
175
|
+ # 查找样式中的编号定义
|
|
|
176
|
+ pPr = style_elem.find(qn('w:pPr'))
|
|
|
177
|
+ if pPr is None:
|
|
|
178
|
+ return
|
|
|
179
|
+
|
|
|
180
|
+ numPr = pPr.find(qn('w:numPr'))
|
|
|
181
|
+ if numPr is None:
|
|
|
182
|
+ return
|
|
|
183
|
+
|
|
|
184
|
+ # 复制编号属性到段落
|
|
|
185
|
+ para_pPr = paragraph._p.get_or_add_pPr()
|
|
|
186
|
+
|
|
|
187
|
+ # 移除现有的 numPr(如果有)
|
|
|
188
|
+ existing_numPr = para_pPr.find(qn('w:numPr'))
|
|
|
189
|
+ if existing_numPr is not None:
|
|
|
190
|
+ para_pPr.remove(existing_numPr)
|
|
|
191
|
+
|
|
|
192
|
+ # 深度复制样式的 numPr 到段落
|
|
|
193
|
+ from copy import deepcopy
|
|
|
194
|
+ new_numPr = deepcopy(numPr)
|
|
|
195
|
+ para_pPr.append(new_numPr)
|
|
|
196
|
+ except Exception:
|
|
|
197
|
+ # 编号应用失败,不影响其他功能
|
|
|
198
|
+ pass
|
|
121
|
199
|
|
|
122
|
200
|
@staticmethod
|
|
123
|
201
|
def _extract_text(children: list) -> str:
|
|
|
@@ -139,6 +217,8 @@ class DocxRenderer(mistune.BaseRenderer):
|
|
139
|
217
|
para = self.doc.add_paragraph(text)
|
|
140
|
218
|
try:
|
|
141
|
219
|
para.style = self._get_style_by_id(style_id)
|
|
|
220
|
+ # 应用样式后,复制编号属性到段落
|
|
|
221
|
+ self._apply_numbering_from_style(para)
|
|
142
|
222
|
except KeyError:
|
|
143
|
223
|
pass
|
|
144
|
224
|
else:
|
|
|
@@ -146,18 +226,180 @@ class DocxRenderer(mistune.BaseRenderer):
|
|
146
|
226
|
return ""
|
|
147
|
227
|
|
|
148
|
228
|
def paragraph(self, token: dict, state) -> str:
|
|
|
229
|
+ # 检查是否包含图片
|
|
|
230
|
+ children = token.get("children", [])
|
|
|
231
|
+ has_image = any(child.get("type") == "image" for child in children)
|
|
|
232
|
+
|
|
|
233
|
+ if has_image:
|
|
|
234
|
+ # 如果包含图片,直接调用 image 处理
|
|
|
235
|
+ for child in children:
|
|
|
236
|
+ if child.get("type") == "image":
|
|
|
237
|
+ self.image(child, state)
|
|
|
238
|
+ return ""
|
|
|
239
|
+
|
|
149
|
240
|
p = self.doc.add_paragraph()
|
|
150
|
|
- if self._normal_id:
|
|
|
241
|
+
|
|
|
242
|
+ # 尝试应用待定样式
|
|
|
243
|
+ style_applied = False
|
|
|
244
|
+ if self.pending_style:
|
|
|
245
|
+ style_id = _resolve_style_id(self.style_map, self.pending_style)
|
|
|
246
|
+ if style_id:
|
|
|
247
|
+ try:
|
|
|
248
|
+ p.style = self._get_style_by_id(style_id)
|
|
|
249
|
+ style_applied = True
|
|
|
250
|
+ except KeyError:
|
|
|
251
|
+ pass # 样式不存在,静默忽略
|
|
|
252
|
+ self.pending_style = None
|
|
|
253
|
+
|
|
|
254
|
+ # 如果没有应用样式,使用 Normal
|
|
|
255
|
+ if not style_applied and self._normal_id:
|
|
151
|
256
|
try:
|
|
152
|
257
|
p.style = self._get_style_by_id(self._normal_id)
|
|
153
|
258
|
except Exception:
|
|
154
|
259
|
pass
|
|
|
260
|
+
|
|
155
|
261
|
self._render_inline_children(p, token.get("children", []))
|
|
156
|
262
|
return ""
|
|
|
263
|
+
|
|
|
264
|
+ def html(self, token: dict, state) -> str:
|
|
|
265
|
+ """处理内联 HTML 注释(表格单元格中的样式标记)"""
|
|
|
266
|
+ raw = token.get("raw", "")
|
|
|
267
|
+ if "<!-- style:" in raw and "-->" in raw:
|
|
|
268
|
+ try:
|
|
|
269
|
+ start = raw.index("<!-- style:") + 11
|
|
|
270
|
+ end = raw.index("-->", start)
|
|
|
271
|
+ self.pending_style = raw[start:end].strip()
|
|
|
272
|
+ except (ValueError, IndexError):
|
|
|
273
|
+ pass
|
|
|
274
|
+ return ""
|
|
|
275
|
+
|
|
|
276
|
+ def block_html(self, token: dict, state) -> str:
|
|
|
277
|
+ """处理块级 HTML(样式注释+文本在同一行)"""
|
|
|
278
|
+ raw = token.get("raw", "")
|
|
|
279
|
+
|
|
|
280
|
+ # 处理图片样式注释
|
|
|
281
|
+ if "<!-- img-style:" in raw and "-->" in raw:
|
|
|
282
|
+ try:
|
|
|
283
|
+ start = raw.index("{")
|
|
|
284
|
+ end = raw.rindex("}") + 1
|
|
|
285
|
+ self.pending_image_style = json.loads(raw[start:end])
|
|
|
286
|
+ except (ValueError, json.JSONDecodeError):
|
|
|
287
|
+ pass
|
|
|
288
|
+ return ""
|
|
|
289
|
+
|
|
|
290
|
+ # 处理文本样式注释
|
|
|
291
|
+ if "<!-- style:" in raw and "-->" in raw:
|
|
|
292
|
+ try:
|
|
|
293
|
+ # 提取样式名
|
|
|
294
|
+ style_start = raw.index("<!-- style:") + 11
|
|
|
295
|
+ style_end = raw.index("-->", style_start)
|
|
|
296
|
+ style_name = raw[style_start:style_end].strip()
|
|
|
297
|
+
|
|
|
298
|
+ # 提取文本(注释后面的内容)
|
|
|
299
|
+ text_start = style_end + 3 # "-->".length = 3
|
|
|
300
|
+ text = raw[text_start:].strip()
|
|
|
301
|
+
|
|
|
302
|
+ # 创建段落并应用样式
|
|
|
303
|
+ p = self.doc.add_paragraph(text)
|
|
|
304
|
+ style_id = _resolve_style_id(self.style_map, style_name)
|
|
|
305
|
+ if style_id:
|
|
|
306
|
+ try:
|
|
|
307
|
+ p.style = self._get_style_by_id(style_id)
|
|
|
308
|
+ # 应用样式后,复制编号属性到段落
|
|
|
309
|
+ self._apply_numbering_from_style(p)
|
|
|
310
|
+ except KeyError:
|
|
|
311
|
+ pass # 样式不存在,使用默认
|
|
|
312
|
+ except (ValueError, IndexError):
|
|
|
313
|
+ # 解析失败,当作普通 HTML 处理(忽略)
|
|
|
314
|
+ pass
|
|
|
315
|
+ return ""
|
|
157
|
316
|
|
|
158
|
317
|
def blank_line(self, token: dict, state) -> str:
|
|
159
|
318
|
return ""
|
|
160
|
319
|
|
|
|
320
|
+ def image(self, token: dict, state) -> str:
|
|
|
321
|
+ """处理图片 token(支持 Base64 Data URL)"""
|
|
|
322
|
+ url = token['attrs']['url']
|
|
|
323
|
+ alt = token['attrs'].get('alt', '图片')
|
|
|
324
|
+
|
|
|
325
|
+ # 只处理 Data URL
|
|
|
326
|
+ if not url.startswith('data:'):
|
|
|
327
|
+ return ""
|
|
|
328
|
+
|
|
|
329
|
+ try:
|
|
|
330
|
+ # 解析 data:image/png;base64,xxxxx
|
|
|
331
|
+ if ',' not in url:
|
|
|
332
|
+ return ""
|
|
|
333
|
+
|
|
|
334
|
+ header, b64_data = url.split(',', 1)
|
|
|
335
|
+ image_bytes = base64.b64decode(b64_data)
|
|
|
336
|
+
|
|
|
337
|
+ # 获取样式(来自前面的 HTML 注释)
|
|
|
338
|
+ style = self.pending_image_style or {}
|
|
|
339
|
+ self.pending_image_style = None
|
|
|
340
|
+
|
|
|
341
|
+ # 创建段落并设置对齐
|
|
|
342
|
+ paragraph = self.doc.add_paragraph()
|
|
|
343
|
+
|
|
|
344
|
+ # 应用段落样式:优先使用保存的样式,否则使用 Normal
|
|
|
345
|
+ para_style = style.get('para_style', 'Normal')
|
|
|
346
|
+ style_id = _resolve_style_id(self.style_map, para_style)
|
|
|
347
|
+
|
|
|
348
|
+ if style_id:
|
|
|
349
|
+ try:
|
|
|
350
|
+ paragraph.style = self._get_style_by_id(style_id)
|
|
|
351
|
+ except KeyError:
|
|
|
352
|
+ # 如果样式不存在,回退到 Normal
|
|
|
353
|
+ if self._normal_id:
|
|
|
354
|
+ try:
|
|
|
355
|
+ paragraph.style = self._get_style_by_id(self._normal_id)
|
|
|
356
|
+ except Exception:
|
|
|
357
|
+ pass
|
|
|
358
|
+ elif self._normal_id:
|
|
|
359
|
+ # 如果没有找到样式 ID,使用 Normal
|
|
|
360
|
+ try:
|
|
|
361
|
+ paragraph.style = self._get_style_by_id(self._normal_id)
|
|
|
362
|
+ except Exception:
|
|
|
363
|
+ pass
|
|
|
364
|
+
|
|
|
365
|
+ align = style.get('align', 'left')
|
|
|
366
|
+ if align == 'center':
|
|
|
367
|
+ paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
|
368
|
+ elif align == 'right':
|
|
|
369
|
+ paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
|
|
|
370
|
+
|
|
|
371
|
+ # 插入图片
|
|
|
372
|
+ run = paragraph.add_run()
|
|
|
373
|
+ width = style.get('width', 10.0)
|
|
|
374
|
+ height = style.get('height', 7.0)
|
|
|
375
|
+ unit = style.get('unit', 'cm')
|
|
|
376
|
+
|
|
|
377
|
+ # 转换为磅(Word内部单位:1厘米 = 28.35磅,1英寸 = 72磅)
|
|
|
378
|
+ if unit == 'cm':
|
|
|
379
|
+ width_pt = width * 28.35
|
|
|
380
|
+ height_pt = height * 28.35
|
|
|
381
|
+ else: # inches
|
|
|
382
|
+ width_pt = width * 72
|
|
|
383
|
+ height_pt = height * 72
|
|
|
384
|
+
|
|
|
385
|
+ run.add_picture(
|
|
|
386
|
+ io.BytesIO(image_bytes),
|
|
|
387
|
+ width=Pt(width_pt),
|
|
|
388
|
+ height=Pt(height_pt)
|
|
|
389
|
+ )
|
|
|
390
|
+
|
|
|
391
|
+ except Exception as e:
|
|
|
392
|
+ # 失败时添加占位文本
|
|
|
393
|
+ p = self.doc.add_paragraph(f"[图片加载失败: {alt}]")
|
|
|
394
|
+ if self._normal_id:
|
|
|
395
|
+ try:
|
|
|
396
|
+ p.style = self._get_style_by_id(self._normal_id)
|
|
|
397
|
+ except Exception:
|
|
|
398
|
+ pass
|
|
|
399
|
+ p.runs[0].font.color.rgb = RGBColor(255, 0, 0)
|
|
|
400
|
+
|
|
|
401
|
+ return ""
|
|
|
402
|
+
|
|
161
|
403
|
def thematic_break(self, token: dict, state) -> str:
|
|
162
|
404
|
p = self.doc.add_paragraph()
|
|
163
|
405
|
pPr = p._p.get_or_add_pPr()
|
|
|
@@ -248,17 +490,14 @@ class DocxRenderer(mistune.BaseRenderer):
|
|
248
|
490
|
tbl = self.doc.add_table(rows=1 + len(body_rows), cols=cols)
|
|
249
|
491
|
tbl.style = "Table Grid"
|
|
250
|
492
|
|
|
251
|
|
- # 表头行(保留内联格式)
|
|
|
493
|
+ # 表头行(保留内联格式,不强制加粗)
|
|
252
|
494
|
for c, cell_token in enumerate(head_cells):
|
|
253
|
495
|
cell = tbl.rows[0].cells[c]
|
|
254
|
496
|
# 清空默认段落
|
|
255
|
497
|
cell.text = ""
|
|
256
|
498
|
para = cell.paragraphs[0]
|
|
257
|
|
- # 渲染内联内容
|
|
|
499
|
+ # 渲染内联内容(样式由 _render_inline_children 处理)
|
|
258
|
500
|
self._render_inline_children(para, cell_token.get("children", []))
|
|
259
|
|
- # 设置粗体
|
|
260
|
|
- for run in para.runs:
|
|
261
|
|
- run.bold = True
|
|
262
|
501
|
|
|
263
|
502
|
# 数据行(保留内联格式)
|
|
264
|
503
|
for r, row_cells in enumerate(body_rows):
|
|
|
@@ -266,22 +505,54 @@ class DocxRenderer(mistune.BaseRenderer):
|
|
266
|
505
|
if c >= cols:
|
|
267
|
506
|
break
|
|
268
|
507
|
cell = tbl.rows[r + 1].cells[c]
|
|
269
|
|
- # 清空默认段落
|
|
270
|
508
|
cell.text = ""
|
|
271
|
509
|
para = cell.paragraphs[0]
|
|
272
|
|
- # 渲染内联内容
|
|
|
510
|
+ # 渲染内联内容(样式注释会在 _render_inline_children 中处理)
|
|
273
|
511
|
self._render_inline_children(para, cell_token.get("children", []))
|
|
274
|
512
|
|
|
275
|
513
|
return ""
|
|
276
|
514
|
|
|
277
|
|
- return ""
|
|
278
|
|
-
|
|
279
|
515
|
def _render_inline_children(self, paragraph, children: list) -> None:
|
|
|
516
|
+ """渲染内联子元素,处理粗体、斜体等格式"""
|
|
280
|
517
|
for child in children:
|
|
281
|
518
|
ctype = child.get("type", "")
|
|
282
|
519
|
raw = child.get("raw", "")
|
|
283
|
|
- if ctype == "text":
|
|
|
520
|
+
|
|
|
521
|
+ if ctype == "inline_html":
|
|
|
522
|
+ # 处理图片样式注释
|
|
|
523
|
+ if "<!-- img-style:" in raw and "-->" in raw:
|
|
|
524
|
+ try:
|
|
|
525
|
+ start = raw.index("{")
|
|
|
526
|
+ end = raw.rindex("}") + 1
|
|
|
527
|
+ self.pending_image_style = json.loads(raw[start:end])
|
|
|
528
|
+ except (ValueError, json.JSONDecodeError):
|
|
|
529
|
+ pass
|
|
|
530
|
+ # 注释不输出
|
|
|
531
|
+ continue
|
|
|
532
|
+
|
|
|
533
|
+ # 处理文本样式注释
|
|
|
534
|
+ if "<!-- style:" in raw and "-->" in raw:
|
|
|
535
|
+ try:
|
|
|
536
|
+ start = raw.index("<!-- style:") + 11
|
|
|
537
|
+ end = raw.index("-->", start)
|
|
|
538
|
+ self.pending_style = raw[start:end].strip()
|
|
|
539
|
+ except (ValueError, IndexError):
|
|
|
540
|
+ pass
|
|
|
541
|
+ # 注释不输出
|
|
|
542
|
+ continue
|
|
|
543
|
+
|
|
|
544
|
+ elif ctype == "text":
|
|
|
545
|
+ # 应用待定样式(来自前一个 inline_html)
|
|
|
546
|
+ if self.pending_style:
|
|
|
547
|
+ style_id = _resolve_style_id(self.style_map, self.pending_style)
|
|
|
548
|
+ if style_id:
|
|
|
549
|
+ try:
|
|
|
550
|
+ paragraph.style = self._get_style_by_id(style_id)
|
|
|
551
|
+ except KeyError:
|
|
|
552
|
+ pass
|
|
|
553
|
+ self.pending_style = None
|
|
284
|
554
|
paragraph.add_run(raw)
|
|
|
555
|
+
|
|
285
|
556
|
elif ctype == "strong":
|
|
286
|
557
|
paragraph.add_run(self._extract_text(child.get("children", []))).bold = True
|
|
287
|
558
|
elif ctype == "emphasis":
|
|
|
@@ -296,6 +567,11 @@ class DocxRenderer(mistune.BaseRenderer):
|
|
296
|
567
|
paragraph.add_run().add_break()
|
|
297
|
568
|
elif ctype == "softlinebreak":
|
|
298
|
569
|
paragraph.add_run(" ")
|
|
|
570
|
+ elif ctype == "image":
|
|
|
571
|
+ # 处理内联图片
|
|
|
572
|
+ # 注意:这里的图片是在段落中的,需要特殊处理
|
|
|
573
|
+ # 我们需要跳过这个段落,让 image() 方法来处理
|
|
|
574
|
+ pass
|
|
299
|
575
|
else:
|
|
300
|
576
|
sub = child.get("children")
|
|
301
|
577
|
if sub:
|