GuXiaopeng commited on
Commit
3bb9f3d
·
verified ·
1 Parent(s): 16a23c8

Update utils/docx_builder.py

Browse files
Files changed (1) hide show
  1. utils/docx_builder.py +8 -2
utils/docx_builder.py CHANGED
@@ -3,14 +3,14 @@ import io
3
  import mistune
4
  from bs4 import BeautifulSoup
5
  from docx import Document
6
- from docx.shared import Pt
7
 
8
  class DocxRenderer(mistune.HTMLRenderer):
9
  def __init__(self):
10
  super().__init__()
11
  self.doc = Document()
12
  self.h_counters = [0] * 6
13
- self.is_first_h1 = True
14
  try:
15
  style = self.doc.styles['Normal']
16
  style.font.name = 'Times New Roman'
@@ -33,8 +33,10 @@ class DocxRenderer(mistune.HTMLRenderer):
33
  def heading(self, text, level):
34
  clean_text = BeautifulSoup(text, 'html.parser').get_text().strip()
35
  lower_text = clean_text.lower()
 
36
  skip_keywords = ["abstract", "keywords", "references", "摘要", "关键词", "关键字", "参考文献", "reference list", "аннотация", "ключевые слова", "түйіндеме", "кілт сөздер"]
37
 
 
38
  if any(kw in lower_text for kw in skip_keywords) or (level == 1 and self.is_first_h1):
39
  if level == 1: self.is_first_h1 = False
40
  self.doc.add_heading(clean_text, level=1)
@@ -42,11 +44,15 @@ class DocxRenderer(mistune.HTMLRenderer):
42
 
43
  if level == 1: self.is_first_h1 = False
44
 
 
45
  idx = level - 1
46
  if 0 <= idx < 6:
47
  self.h_counters[idx] += 1
 
48
  for i in range(idx + 1, 6):
49
  self.h_counters[i] = 0
 
 
50
  nums = self.h_counters[0:idx+1]
51
  num_str = ".".join(map(str, nums))
52
  self.doc.add_heading(f"{num_str} {clean_text}", level=level)
 
3
  import mistune
4
  from bs4 import BeautifulSoup
5
  from docx import Document
6
+ from docx.shared import Inches, Pt
7
 
8
  class DocxRenderer(mistune.HTMLRenderer):
9
  def __init__(self):
10
  super().__init__()
11
  self.doc = Document()
12
  self.h_counters = [0] * 6
13
+ self.is_first_h1 = True # 新增:用于捕获首个H1(论文题目),避免为其编号
14
  try:
15
  style = self.doc.styles['Normal']
16
  style.font.name = 'Times New Roman'
 
33
  def heading(self, text, level):
34
  clean_text = BeautifulSoup(text, 'html.parser').get_text().strip()
35
  lower_text = clean_text.lower()
36
+ # 黑名单:严格跳过这些关键字的自动编号
37
  skip_keywords = ["abstract", "keywords", "references", "摘要", "关键词", "关键字", "参考文献", "reference list", "аннотация", "ключевые слова", "түйіндеме", "кілт сөздер"]
38
 
39
+ # 拦截逻辑:若是黑名单、或者是文档首个H1(论文题目),直接输出不加编号
40
  if any(kw in lower_text for kw in skip_keywords) or (level == 1 and self.is_first_h1):
41
  if level == 1: self.is_first_h1 = False
42
  self.doc.add_heading(clean_text, level=1)
 
44
 
45
  if level == 1: self.is_first_h1 = False
46
 
47
+ # === 核心自动编号逻辑 ===
48
  idx = level - 1
49
  if 0 <= idx < 6:
50
  self.h_counters[idx] += 1
51
+ # 将当前层级以下的子级计数器全部清零
52
  for i in range(idx + 1, 6):
53
  self.h_counters[i] = 0
54
+
55
+ # 拼接数组生成编号,如 [2, 1, 0] -> "2.1"
56
  nums = self.h_counters[0:idx+1]
57
  num_str = ".".join(map(str, nums))
58
  self.doc.add_heading(f"{num_str} {clean_text}", level=level)