Upload app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
import re
|
3 |
from docx import Document
|
4 |
from docx.shared import Cm, Pt
|
5 |
-
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
6 |
from docx.enum.style import WD_STYLE_TYPE
|
7 |
from docx.oxml.ns import qn
|
8 |
from docx.oxml import OxmlElement
|
@@ -61,14 +61,34 @@ def format_docx(file, chapter_keywords):
|
|
61 |
# 重新建立文件內容
|
62 |
for content_type, text in content_list:
|
63 |
if content_type == 'heading':
|
64 |
-
#
|
65 |
-
heading = doc.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
2 |
import re
|
3 |
from docx import Document
|
4 |
from docx.shared import Cm, Pt
|
5 |
+
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT, WD_OUTLINE_LEVEL
|
6 |
from docx.enum.style import WD_STYLE_TYPE
|
7 |
from docx.oxml.ns import qn
|
8 |
from docx.oxml import OxmlElement
|
|
|
61 |
# 重新建立文件內容
|
62 |
for content_type, text in content_list:
|
63 |
if content_type == 'heading':
|
64 |
+
# 直接添加段落,然後手動設定為標題格式
|
65 |
+
heading = doc.add_paragraph(text)
|
66 |
+
|
67 |
+
# 手動設定標題格式
|
68 |
+
for run in heading.runs:
|
69 |
+
run.font.bold = True
|
70 |
+
run.font.size = Pt(16)
|
71 |
+
run.font.name = '新細明體' # 設定中文字體
|
72 |
+
|
73 |
+
# 設定段落格式
|
74 |
+
heading.paragraph_format.space_before = Cm(0)
|
75 |
+
heading.paragraph_format.space_after = Cm(0.3) # 標題後稍微間距
|
76 |
+
heading.paragraph_format.line_spacing = 1.0
|
77 |
+
|
78 |
# 在標題前分頁
|
79 |
heading.paragraph_format.page_break_before = True
|
80 |
+
|
81 |
# 重置標題的縮排
|
82 |
heading.paragraph_format.left_indent = Cm(0)
|
83 |
heading.paragraph_format.first_line_indent = Cm(0)
|
84 |
|
85 |
+
# 嘗試設定大綱層級(這樣可以在導覽窗格中顯示)
|
86 |
+
try:
|
87 |
+
# 直接設定大綱層級,不依賴樣式
|
88 |
+
heading.paragraph_format.outline_level = 0 # 0 = 層級1
|
89 |
+
except:
|
90 |
+
pass # 如果失敗就跳過
|
91 |
+
|
92 |
elif content_type == 'paragraph':
|
93 |
# 添加一般段落
|
94 |
para = doc.add_paragraph(text)
|