Upload app.py
Browse files
app.py
CHANGED
|
@@ -21,7 +21,57 @@ def normalize_paragraph(text):
|
|
| 21 |
text = re.sub(r'\s{2,}', ' ', text)
|
| 22 |
return text.strip()
|
| 23 |
|
| 24 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if file is None:
|
| 26 |
return None, "請上傳一個 Word 文件"
|
| 27 |
if not chapter_keywords.strip():
|
|
@@ -43,56 +93,25 @@ def format_docx(file, chapter_keywords):
|
|
| 43 |
patterns = [f'第\s*[0-9一二三四五六七八九十百千萬壹貳參肆伍陸柒捌玖拾佰仟萬IVXLCDMivxlcdm]+\s*{k}' for k in keywords]
|
| 44 |
combined_pattern = '|'.join(patterns)
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
content_list =
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
if
|
| 52 |
-
if not prev_empty:
|
| 53 |
-
content_list.append(('empty', ''))
|
| 54 |
-
prev_empty = True
|
| 55 |
-
elif re.search(combined_pattern, text):
|
| 56 |
-
content_list.append(('heading', text))
|
| 57 |
-
prev_empty = False
|
| 58 |
-
else:
|
| 59 |
-
content_list.append(('paragraph', text))
|
| 60 |
-
prev_empty = False
|
| 61 |
|
| 62 |
# 清空原始內容
|
| 63 |
for p in doc.paragraphs:
|
| 64 |
p._element.getparent().remove(p._element)
|
| 65 |
|
| 66 |
# 重建段落
|
| 67 |
-
|
| 68 |
-
if kind == 'heading':
|
| 69 |
-
heading = doc.add_paragraph(text, style='Heading 1')
|
| 70 |
-
heading.paragraph_format.page_break_before = True
|
| 71 |
-
heading.paragraph_format.space_before = Cm(0)
|
| 72 |
-
heading.paragraph_format.space_after = Cm(0.3)
|
| 73 |
-
heading.paragraph_format.line_spacing = 1.0
|
| 74 |
-
heading.paragraph_format.left_indent = Cm(0)
|
| 75 |
-
heading.paragraph_format.first_line_indent = Cm(0)
|
| 76 |
-
set_outline_level(heading, 0)
|
| 77 |
-
for run in heading.runs:
|
| 78 |
-
run.font.name = '新細明體'
|
| 79 |
-
run.font.size = Pt(16)
|
| 80 |
-
elif kind == 'paragraph':
|
| 81 |
-
clean_text = normalize_paragraph(text)
|
| 82 |
-
para = doc.add_paragraph(clean_text)
|
| 83 |
-
para.paragraph_format.space_before = Cm(0)
|
| 84 |
-
para.paragraph_format.space_after = Cm(0)
|
| 85 |
-
para.paragraph_format.line_spacing = 1.0
|
| 86 |
-
para.paragraph_format.left_indent = Cm(0)
|
| 87 |
-
para.paragraph_format.first_line_indent = Cm(0.7)
|
| 88 |
-
for run in para.runs:
|
| 89 |
-
run.font.name = '新細明體'
|
| 90 |
-
elif kind == 'empty':
|
| 91 |
-
doc.add_paragraph('')
|
| 92 |
|
| 93 |
output_path = tempfile.mktemp(suffix='.docx')
|
| 94 |
doc.save(output_path)
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
return None, f"❌ 處理失敗:{str(e)}"
|
| 98 |
|
|
@@ -109,12 +128,21 @@ def create_interface():
|
|
| 109 |
with gr.Column(scale=1):
|
| 110 |
file_input = gr.File(label="上傳 Word 文件 (.docx)", file_types=[".docx"], file_count="single")
|
| 111 |
chapter_input = gr.Textbox(label="章節分段方式", placeholder="章,節,話", value="章,節,話")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
process_btn = gr.Button("🔄 開始處理", variant="primary", size="lg")
|
| 113 |
with gr.Column(scale=1):
|
| 114 |
status_output = gr.Textbox(label="處理狀態", interactive=False, lines=3)
|
| 115 |
download_output = gr.File(label="下載處理後的文件", interactive=False)
|
| 116 |
|
| 117 |
-
process_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
return demo
|
| 120 |
|
|
|
|
| 21 |
text = re.sub(r'\s{2,}', ' ', text)
|
| 22 |
return text.strip()
|
| 23 |
|
| 24 |
+
def process_paragraphs_with_cleanup(doc, combined_pattern):
|
| 25 |
+
"""處理段落並清理多餘空行,實現 ^p^p -> ^p 效果"""
|
| 26 |
+
content_list = []
|
| 27 |
+
prev_empty = False
|
| 28 |
+
|
| 29 |
+
for para in doc.paragraphs:
|
| 30 |
+
text = para.text.strip()
|
| 31 |
+
if not text:
|
| 32 |
+
# 只有當前一個段落不是空的時候,才保留這個空段落
|
| 33 |
+
if not prev_empty:
|
| 34 |
+
content_list.append(('empty', ''))
|
| 35 |
+
prev_empty = True
|
| 36 |
+
elif re.search(combined_pattern, text):
|
| 37 |
+
content_list.append(('heading', text))
|
| 38 |
+
prev_empty = False
|
| 39 |
+
else:
|
| 40 |
+
content_list.append(('paragraph', text))
|
| 41 |
+
prev_empty = False
|
| 42 |
+
|
| 43 |
+
return content_list
|
| 44 |
+
|
| 45 |
+
def rebuild_document(doc, content_list):
|
| 46 |
+
"""重建文檔內容"""
|
| 47 |
+
for kind, text in content_list:
|
| 48 |
+
if kind == 'heading':
|
| 49 |
+
heading = doc.add_paragraph(text, style='Heading 1')
|
| 50 |
+
heading.paragraph_format.page_break_before = True
|
| 51 |
+
heading.paragraph_format.space_before = Cm(0)
|
| 52 |
+
heading.paragraph_format.space_after = Cm(0.3)
|
| 53 |
+
heading.paragraph_format.line_spacing = 1.0
|
| 54 |
+
heading.paragraph_format.left_indent = Cm(0)
|
| 55 |
+
heading.paragraph_format.first_line_indent = Cm(0)
|
| 56 |
+
set_outline_level(heading, 0)
|
| 57 |
+
for run in heading.runs:
|
| 58 |
+
run.font.name = '新細明體'
|
| 59 |
+
run.font.size = Pt(16)
|
| 60 |
+
elif kind == 'paragraph':
|
| 61 |
+
clean_text = normalize_paragraph(text)
|
| 62 |
+
para = doc.add_paragraph(clean_text)
|
| 63 |
+
para.paragraph_format.space_before = Cm(0)
|
| 64 |
+
para.paragraph_format.space_after = Cm(0)
|
| 65 |
+
para.paragraph_format.line_spacing = 1.0
|
| 66 |
+
para.paragraph_format.left_indent = Cm(0)
|
| 67 |
+
para.paragraph_format.first_line_indent = Cm(0.7)
|
| 68 |
+
for run in para.runs:
|
| 69 |
+
run.font.name = '新細明體'
|
| 70 |
+
elif kind == 'empty':
|
| 71 |
+
# 只有在真正需要保留空段落時才創建
|
| 72 |
+
doc.add_paragraph('')
|
| 73 |
+
|
| 74 |
+
def format_docx(file, chapter_keywords, remove_empty_paragraphs=True):
|
| 75 |
if file is None:
|
| 76 |
return None, "請上傳一個 Word 文件"
|
| 77 |
if not chapter_keywords.strip():
|
|
|
|
| 93 |
patterns = [f'第\s*[0-9一二三四五六七八九十百千萬壹貳參肆伍陸柒捌玖拾佰仟萬IVXLCDMivxlcdm]+\s*{k}' for k in keywords]
|
| 94 |
combined_pattern = '|'.join(patterns)
|
| 95 |
|
| 96 |
+
# 處理段落並清理多餘空行
|
| 97 |
+
content_list = process_paragraphs_with_cleanup(doc, combined_pattern)
|
| 98 |
+
|
| 99 |
+
# 如果選擇移除空段落,過濾掉所有空段落
|
| 100 |
+
if remove_empty_paragraphs:
|
| 101 |
+
content_list = [item for item in content_list if item[0] != 'empty']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
# 清空原始內容
|
| 104 |
for p in doc.paragraphs:
|
| 105 |
p._element.getparent().remove(p._element)
|
| 106 |
|
| 107 |
# 重建段落
|
| 108 |
+
rebuild_document(doc, content_list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
output_path = tempfile.mktemp(suffix='.docx')
|
| 111 |
doc.save(output_path)
|
| 112 |
+
|
| 113 |
+
empty_status = "已移除所有空段落" if remove_empty_paragraphs else "保留單個空段落"
|
| 114 |
+
return output_path, f"✅ 處理完成!找到章節關鍵字:{', '.join(keywords)},{empty_status}"
|
| 115 |
except Exception as e:
|
| 116 |
return None, f"❌ 處理失敗:{str(e)}"
|
| 117 |
|
|
|
|
| 128 |
with gr.Column(scale=1):
|
| 129 |
file_input = gr.File(label="上傳 Word 文件 (.docx)", file_types=[".docx"], file_count="single")
|
| 130 |
chapter_input = gr.Textbox(label="章節分段方式", placeholder="章,節,話", value="章,節,話")
|
| 131 |
+
remove_empty_checkbox = gr.Checkbox(
|
| 132 |
+
label="移除空段落",
|
| 133 |
+
value=True,
|
| 134 |
+
info="勾選時會移除所有空段落,取消勾選時會保留單個空段落(^p^p -> ^p)"
|
| 135 |
+
)
|
| 136 |
process_btn = gr.Button("🔄 開始處理", variant="primary", size="lg")
|
| 137 |
with gr.Column(scale=1):
|
| 138 |
status_output = gr.Textbox(label="處理狀態", interactive=False, lines=3)
|
| 139 |
download_output = gr.File(label="下載處理後的文件", interactive=False)
|
| 140 |
|
| 141 |
+
process_btn.click(
|
| 142 |
+
fn=format_docx,
|
| 143 |
+
inputs=[file_input, chapter_input, remove_empty_checkbox],
|
| 144 |
+
outputs=[download_output, status_output]
|
| 145 |
+
)
|
| 146 |
|
| 147 |
return demo
|
| 148 |
|