Upload app.py
Browse files
app.py
CHANGED
|
@@ -9,111 +9,84 @@ from docx.oxml import OxmlElement
|
|
| 9 |
import tempfile
|
| 10 |
import os
|
| 11 |
|
|
|
|
| 12 |
def format_docx(file, chapter_keywords):
|
| 13 |
"""
|
| 14 |
處理上傳的 Word 文件
|
| 15 |
"""
|
| 16 |
if file is None:
|
| 17 |
return None, "請上傳一個 Word 文件"
|
| 18 |
-
|
| 19 |
if not chapter_keywords.strip():
|
| 20 |
return None, "請輸入章節分段方式(例如:章,節,話)"
|
| 21 |
-
|
| 22 |
try:
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
doc = Document(file.name)
|
| 25 |
-
|
| 26 |
# 解析章節關鍵字
|
| 27 |
keywords = [keyword.strip() for keyword in chapter_keywords.split(',')]
|
| 28 |
-
|
| 29 |
# 建立正規表示式模式
|
| 30 |
patterns = []
|
| 31 |
for keyword in keywords:
|
| 32 |
-
|
| 33 |
-
# 例如:第1章、第一章、第二十三章、第I章等
|
| 34 |
-
pattern = rf'第\s*[0-9一二三四五六七八九十百千萬壹貳參肆伍陸柒捌玖拾佰仟萬IVXLCDMivxlcdm]+\s*{keyword}'
|
| 35 |
patterns.append(pattern)
|
| 36 |
-
|
| 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 |
-
|
| 65 |
-
heading =
|
| 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 |
-
|
| 87 |
-
# 直接設定大綱層級,不依賴樣式
|
| 88 |
-
heading.paragraph_format.outline_level = 0 # 0 = 層級1
|
| 89 |
-
except:
|
| 90 |
-
pass # 如果失敗就跳過
|
| 91 |
-
|
| 92 |
elif content_type == 'paragraph':
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
para.
|
| 97 |
-
para.
|
| 98 |
-
para.
|
| 99 |
-
# 首行縮排
|
| 100 |
para.paragraph_format.left_indent = Cm(0)
|
| 101 |
para.paragraph_format.first_line_indent = Cm(0.7)
|
| 102 |
-
|
|
|
|
| 103 |
elif content_type == 'empty':
|
| 104 |
-
# 添加空段落
|
| 105 |
doc.add_paragraph('')
|
| 106 |
-
|
| 107 |
-
# 儲存處理後的文件
|
| 108 |
output_path = tempfile.mktemp(suffix='.docx')
|
| 109 |
doc.save(output_path)
|
| 110 |
-
|
| 111 |
return output_path, f"✅ 處理完成!找到章節關鍵字:{', '.join(keywords)}"
|
| 112 |
-
|
| 113 |
except Exception as e:
|
| 114 |
return None, f"❌ 處理失敗:{str(e)}"
|
| 115 |
|
| 116 |
def create_interface():
|
|
|
|
| 117 |
"""
|
| 118 |
建立 Gradio 介面
|
| 119 |
"""
|
|
|
|
| 9 |
import tempfile
|
| 10 |
import os
|
| 11 |
|
| 12 |
+
|
| 13 |
def format_docx(file, chapter_keywords):
|
| 14 |
"""
|
| 15 |
處理上傳的 Word 文件
|
| 16 |
"""
|
| 17 |
if file is None:
|
| 18 |
return None, "請上傳一個 Word 文件"
|
| 19 |
+
|
| 20 |
if not chapter_keywords.strip():
|
| 21 |
return None, "請輸入章節分段方式(例如:章,節,話)"
|
| 22 |
+
|
| 23 |
try:
|
| 24 |
+
from docx import Document
|
| 25 |
+
from docx.shared import Cm, Pt
|
| 26 |
+
import re
|
| 27 |
+
import tempfile
|
| 28 |
+
|
| 29 |
doc = Document(file.name)
|
| 30 |
+
|
| 31 |
# 解析章節關鍵字
|
| 32 |
keywords = [keyword.strip() for keyword in chapter_keywords.split(',')]
|
| 33 |
+
|
| 34 |
# 建立正規表示式模式
|
| 35 |
patterns = []
|
| 36 |
for keyword in keywords:
|
| 37 |
+
pattern = f'第\s*[0-9一二三四五六七八九十百千萬壹貳參肆伍陸柒捌玖拾佰仟萬IVXLCDMivxlcdm]+\s*{keyword}'
|
|
|
|
|
|
|
| 38 |
patterns.append(pattern)
|
|
|
|
|
|
|
| 39 |
combined_pattern = '|'.join(patterns)
|
| 40 |
+
|
|
|
|
| 41 |
content_list = []
|
|
|
|
| 42 |
for paragraph in doc.paragraphs:
|
| 43 |
text = paragraph.text.strip()
|
|
|
|
|
|
|
| 44 |
if text and re.search(combined_pattern, text):
|
| 45 |
content_list.append(('heading', text))
|
| 46 |
+
elif text:
|
| 47 |
content_list.append(('paragraph', text))
|
| 48 |
+
else:
|
|
|
|
| 49 |
if not content_list or content_list[-1][0] != 'empty':
|
| 50 |
content_list.append(('empty', ''))
|
| 51 |
+
|
|
|
|
| 52 |
for paragraph in doc.paragraphs:
|
| 53 |
p = paragraph._element
|
| 54 |
p.getparent().remove(p)
|
| 55 |
+
|
|
|
|
| 56 |
for content_type, text in content_list:
|
| 57 |
if content_type == 'heading':
|
| 58 |
+
heading = doc.add_paragraph(text, style='Heading 1')
|
| 59 |
+
heading.paragraph_format.page_break_before = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
heading.paragraph_format.space_before = Cm(0)
|
| 61 |
+
heading.paragraph_format.space_after = Cm(0.3)
|
| 62 |
heading.paragraph_format.line_spacing = 1.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
heading.paragraph_format.left_indent = Cm(0)
|
| 64 |
heading.paragraph_format.first_line_indent = Cm(0)
|
| 65 |
+
for run in heading.runs:
|
| 66 |
+
run.font.name = '新細明體'
|
| 67 |
+
run.font.size = Pt(16)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
elif content_type == 'paragraph':
|
| 69 |
+
lines = [line.strip() for line in text.splitlines() if line.strip()]
|
| 70 |
+
clean_text = ' '.join(lines)
|
| 71 |
+
para = doc.add_paragraph(clean_text)
|
| 72 |
+
para.paragraph_format.space_before = Cm(0)
|
| 73 |
+
para.paragraph_format.space_after = Cm(0)
|
| 74 |
+
para.paragraph_format.line_spacing = 1.0
|
|
|
|
| 75 |
para.paragraph_format.left_indent = Cm(0)
|
| 76 |
para.paragraph_format.first_line_indent = Cm(0.7)
|
| 77 |
+
for run in para.runs:
|
| 78 |
+
run.font.name = '新細明體'
|
| 79 |
elif content_type == 'empty':
|
|
|
|
| 80 |
doc.add_paragraph('')
|
| 81 |
+
|
|
|
|
| 82 |
output_path = tempfile.mktemp(suffix='.docx')
|
| 83 |
doc.save(output_path)
|
|
|
|
| 84 |
return output_path, f"✅ 處理完成!找到章節關鍵字:{', '.join(keywords)}"
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
return None, f"❌ 處理失敗:{str(e)}"
|
| 87 |
|
| 88 |
def create_interface():
|
| 89 |
+
|
| 90 |
"""
|
| 91 |
建立 Gradio 介面
|
| 92 |
"""
|