Upload app.py
Browse files
app.py
CHANGED
@@ -37,83 +37,52 @@ def format_docx(file, chapter_keywords):
|
|
37 |
# 合併所有模式
|
38 |
combined_pattern = '|'.join(patterns)
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
to_delete = []
|
43 |
|
44 |
-
for
|
45 |
-
|
46 |
-
current_empty = len(current_para.text.strip()) == 0
|
47 |
|
48 |
-
#
|
49 |
-
if
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
56 |
|
57 |
-
#
|
58 |
-
for
|
59 |
-
p =
|
60 |
p.getparent().remove(p)
|
61 |
|
62 |
-
#
|
63 |
-
for
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
# 2. 首行縮排兩個字元位置(約0.7公分)
|
74 |
-
paragraph.paragraph_format.left_indent = Cm(0) # 整體不縮排
|
75 |
-
paragraph.paragraph_format.first_line_indent = Cm(0.7) # 只有首行縮排
|
76 |
-
|
77 |
-
# 3. 檢查是否為章節標題
|
78 |
-
if re.search(combined_pattern, paragraph.text):
|
79 |
-
# 保存原始文字
|
80 |
-
original_text = paragraph.text
|
81 |
-
|
82 |
-
# 嘗試設定標題 1 樣式(處理不同語言版本的樣式名稱)
|
83 |
-
try:
|
84 |
-
# 英文版本
|
85 |
-
paragraph.style = doc.styles['Heading 1']
|
86 |
-
except KeyError:
|
87 |
-
try:
|
88 |
-
# 中文版本
|
89 |
-
paragraph.style = doc.styles['標題 1']
|
90 |
-
except KeyError:
|
91 |
-
try:
|
92 |
-
# 其他可能的名稱
|
93 |
-
paragraph.style = doc.styles['Heading1']
|
94 |
-
except KeyError:
|
95 |
-
# 如果都找不到,創建自定義標題樣式
|
96 |
-
from docx.enum.style import WD_STYLE_TYPE
|
97 |
-
try:
|
98 |
-
heading_style = doc.styles.add_style('CustomHeading1', WD_STYLE_TYPE.PARAGRAPH)
|
99 |
-
heading_style.font.bold = True
|
100 |
-
heading_style.font.size = Pt(16)
|
101 |
-
paragraph.style = heading_style
|
102 |
-
except:
|
103 |
-
# 最後手段:直接設定格式
|
104 |
-
paragraph.style = doc.styles['Normal']
|
105 |
-
|
106 |
-
# 確保粗體設定和字體大小
|
107 |
-
for run in paragraph.runs:
|
108 |
-
run.font.bold = True
|
109 |
-
run.font.size = Pt(16)
|
110 |
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
|
118 |
# 儲存處理後的文件
|
119 |
output_path = tempfile.mktemp(suffix='.docx')
|
|
|
37 |
# 合併所有模式
|
38 |
combined_pattern = '|'.join(patterns)
|
39 |
|
40 |
+
# 處理文件:先收集所有段落內容和類型
|
41 |
+
content_list = []
|
|
|
42 |
|
43 |
+
for paragraph in doc.paragraphs:
|
44 |
+
text = paragraph.text.strip()
|
|
|
45 |
|
46 |
+
# 檢查是否為章節標題
|
47 |
+
if text and re.search(combined_pattern, text):
|
48 |
+
content_list.append(('heading', text))
|
49 |
+
elif text: # 有內容的一般段落
|
50 |
+
content_list.append(('paragraph', text))
|
51 |
+
else: # 空段落
|
52 |
+
# 只有在前一個不是空段落時才添加空段落
|
53 |
+
if not content_list or content_list[-1][0] != 'empty':
|
54 |
+
content_list.append(('empty', ''))
|
55 |
|
56 |
+
# 清空整個文件
|
57 |
+
for paragraph in doc.paragraphs:
|
58 |
+
p = paragraph._element
|
59 |
p.getparent().remove(p)
|
60 |
|
61 |
+
# 重新建立文件內容
|
62 |
+
for content_type, text in content_list:
|
63 |
+
if content_type == 'heading':
|
64 |
+
# 使用 add_heading 方法建立標題
|
65 |
+
heading = doc.add_heading(text, level=1)
|
66 |
+
# 在標題前分頁
|
67 |
+
heading.paragraph_format.page_break_before = True
|
68 |
+
# 重置標題的縮排
|
69 |
+
heading.paragraph_format.left_indent = Cm(0)
|
70 |
+
heading.paragraph_format.first_line_indent = Cm(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
elif content_type == 'paragraph':
|
73 |
+
# 添加一般段落
|
74 |
+
para = doc.add_paragraph(text)
|
75 |
+
# 設定樣式
|
76 |
+
para.style.paragraph_format.space_before = Cm(0)
|
77 |
+
para.style.paragraph_format.space_after = Cm(0)
|
78 |
+
para.style.paragraph_format.line_spacing = 1.0
|
79 |
+
# 首行縮排
|
80 |
+
para.paragraph_format.left_indent = Cm(0)
|
81 |
+
para.paragraph_format.first_line_indent = Cm(0.7)
|
82 |
|
83 |
+
elif content_type == 'empty':
|
84 |
+
# 添加空段落
|
85 |
+
doc.add_paragraph('')
|
86 |
|
87 |
# 儲存處理後的文件
|
88 |
output_path = tempfile.mktemp(suffix='.docx')
|