dseditor commited on
Commit
7ec7618
·
verified ·
1 Parent(s): 244abc7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -70
app.py CHANGED
@@ -37,83 +37,52 @@ def format_docx(file, chapter_keywords):
37
  # 合併所有模式
38
  combined_pattern = '|'.join(patterns)
39
 
40
- # 先標記空段落,準備刪除多餘的換行
41
- paragraphs_list = list(doc.paragraphs)
42
- to_delete = []
43
 
44
- for i in range(len(paragraphs_list)):
45
- current_para = paragraphs_list[i]
46
- current_empty = len(current_para.text.strip()) == 0
47
 
48
- # 如果是空段落,檢查前一個段落
49
- if current_empty and i > 0:
50
- prev_para = paragraphs_list[i-1]
51
- prev_empty = len(prev_para.text.strip()) == 0
52
-
53
- # 如果前一個段落也是空的,標記當前段落刪除
54
- if prev_empty:
55
- to_delete.append(current_para)
 
56
 
57
- # 刪除多餘的空段落(從後往前刪除)
58
- for para in reversed(to_delete):
59
- p = para._element
60
  p.getparent().remove(p)
61
 
62
- # 處理每個段落
63
- for paragraph in doc.paragraphs:
64
- # 跳過空段落
65
- if len(paragraph.text.strip()) == 0:
66
- continue
67
-
68
- # 1. 將樣式改為無間距
69
- paragraph.style.paragraph_format.space_before = Cm(0)
70
- paragraph.style.paragraph_format.space_after = Cm(0)
71
- paragraph.style.paragraph_format.line_spacing = 1.0
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
- # 4. 在章節前分頁
112
- paragraph.paragraph_format.page_break_before = True
 
 
 
 
 
 
 
 
113
 
114
- # 重置章節標題的縮排(標題不需要首行縮排)
115
- paragraph.paragraph_format.left_indent = Cm(0)
116
- paragraph.paragraph_format.first_line_indent = Cm(0)
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')