123Sabrina commited on
Commit
aa3ebce
·
verified ·
1 Parent(s): dc5332e

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +12 -57
src/streamlit_app.py CHANGED
@@ -119,51 +119,17 @@ def process_with_gemini(model, text, instruction="請解釋以下內容"):
119
  logger.error(f"Gemini處理失敗: {e}")
120
  return f"處理失敗: {str(e)}"
121
 
122
- def provide_download_options(result_df, export_format="嘗試標準CSV格式", include_checkbox=True):
123
- """提供下載選項,根據選擇的匯出格式提供不同的CSV方案"""
124
 
125
- # 如果需要添加核取方塊欄位
126
- if include_checkbox:
127
- # 添加一個空的核取方塊欄位
128
- result_df.insert(0, "", "")
129
-
130
- if export_format == "嘗試標準CSV格式":
131
- # 標準方案:使用UTF-8-SIG編碼
132
- csv_data = result_df.to_csv(index=False, encoding='utf-8-sig')
133
- st.download_button(
134
- label="下載結果為CSV",
135
- data=csv_data.encode('utf-8-sig'), # 確保編碼正確
136
- file_name=f"pdf_process_result_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
137
- mime="text/csv; charset=utf-8-sig", # 指定MIME類型包含編碼
138
- )
139
- else:
140
- # Excel兼容方案:使用GBK或Big5編碼,適合中文Windows環境
141
- try:
142
- # 先嘗試Big5編碼(適合繁體中文)
143
- if "繁體中文" in target_language:
144
- csv_data = result_df.to_csv(index=False, encoding='big5')
145
- encoding_used = 'big5'
146
- else:
147
- # 否則嘗試GBK編碼(適合簡體中文)
148
- csv_data = result_df.to_csv(index=False, encoding='gbk')
149
- encoding_used = 'gbk'
150
-
151
- st.download_button(
152
- label=f"下載結果為CSV (Excel兼容 - {encoding_used}編碼)",
153
- data=csv_data,
154
- file_name=f"pdf_process_result_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
155
- mime=f"text/csv; charset={encoding_used}",
156
- )
157
- except UnicodeEncodeError:
158
- # 如果特定編碼失敗,回退到UTF-8
159
- st.warning("無法使用本地編碼,回退到UTF-8格式。若CSV開啟有亂碼,請用Excel的「資料匯入」功能匯入檔案。")
160
- csv_data = result_df.to_csv(index=False, encoding='utf-8-sig')
161
- st.download_button(
162
- label="下載結果為CSV (UTF-8編碼)",
163
- data=csv_data,
164
- file_name=f"pdf_process_result_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
165
- mime="text/csv; charset=utf-8-sig",
166
- )
167
 
168
  # 如果支援Excel格式,也提供Excel下載選項
169
  if EXCEL_SUPPORT:
@@ -182,17 +148,6 @@ def provide_download_options(result_df, export_format="嘗試標準CSV格式", i
182
  # 上傳PDF文件
183
  uploaded_file = st.file_uploader("上傳PDF文件", type="pdf")
184
 
185
- # 添加匯出選項
186
- st.sidebar.markdown("---")
187
- st.sidebar.markdown("### 匯出選項")
188
- export_format = st.sidebar.radio(
189
- "CSV開啟後中文亂碼怎麼辦?",
190
- ["嘗試標準CSV格式", "使用Excel兼容格式"]
191
- )
192
-
193
- # 添加核取方塊選項
194
- include_checkbox = st.sidebar.checkbox("在CSV中添加核取方塊欄位", value=True, help="在Excel中開啟時,會顯示一個可勾選的核取方塊欄位")
195
-
196
  if uploaded_file is not None:
197
  # 顯示上傳成功訊息
198
  st.success(f"文件 '{uploaded_file.name}' 上傳成功!")
@@ -253,7 +208,7 @@ if uploaded_file is not None:
253
  })
254
 
255
  # 提供下載選項
256
- provide_download_options(result_df, export_format)
257
  else:
258
  # 先解釋後翻譯
259
  with st.spinner("解釋內容..."):
@@ -282,6 +237,6 @@ if uploaded_file is not None:
282
  })
283
 
284
  # 提供下載選項
285
- provide_download_options(result_df, export_format)
286
  else:
287
  st.error("無法提取PDF內容,請檢查文件格式是否正確。")
 
119
  logger.error(f"Gemini處理失敗: {e}")
120
  return f"處理失敗: {str(e)}"
121
 
122
+ def provide_download_options(result_df):
123
+ """提供下載選項,確保CSV中文不會出現亂碼"""
124
 
125
+ # 使用UTF-8-SIG編碼保證中文在CSV中不會出現亂碼
126
+ csv_data = result_df.to_csv(index=False, encoding='utf-8-sig')
127
+ st.download_button(
128
+ label="下載結果為CSV",
129
+ data=csv_data,
130
+ file_name=f"pdf_process_result_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
131
+ mime="text/csv; charset=utf-8-sig", # 指定MIME類型包含編碼
132
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
  # 如果支援Excel格式,也提供Excel下載選項
135
  if EXCEL_SUPPORT:
 
148
  # 上傳PDF文件
149
  uploaded_file = st.file_uploader("上傳PDF文件", type="pdf")
150
 
 
 
 
 
 
 
 
 
 
 
 
151
  if uploaded_file is not None:
152
  # 顯示上傳成功訊息
153
  st.success(f"文件 '{uploaded_file.name}' 上傳成功!")
 
208
  })
209
 
210
  # 提供下載選項
211
+ provide_download_options(result_df)
212
  else:
213
  # 先解釋後翻譯
214
  with st.spinner("解釋內容..."):
 
237
  })
238
 
239
  # 提供下載選項
240
+ provide_download_options(result_df)
241
  else:
242
  st.error("無法提取PDF內容,請檢查文件格式是否正確。")