ahalamora commited on
Commit
988af20
·
1 Parent(s): af508c3

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -8,6 +8,7 @@ from doc_process import doc_process
8
 
9
 
10
  extract_folder = 'documents'
 
11
 
12
  # 压缩目录到zip文件
13
  def compress_directory(directory_path, output_zip):
@@ -32,8 +33,6 @@ def zip_extract_all(src_zip_file: ZipFile, target_path: str) -> None:
32
 
33
  # 遍历压缩包内所有内容,创建所有目录
34
  for file_or_path in file.namelist():
35
-
36
- print(file_or_path)
37
 
38
  # 若当前节点是文件夹
39
  if file_or_path.endswith('/'):
@@ -50,8 +49,6 @@ def zip_extract_all(src_zip_file: ZipFile, target_path: str) -> None:
50
 
51
  # 遍历压缩包内所有内容,解压文件
52
  for file_or_path in file.namelist():
53
-
54
- print(file_or_path)
55
 
56
  # 若当前节点是文件夹
57
  if file_or_path.endswith('/'):
@@ -87,13 +84,12 @@ if uploaded_file:
87
  # 显示解压缩完成的消息
88
  st.success(f"ZIP文件已成功解压缩到目录 {extract_folder}")
89
  input_folder = os.listdir(extract_folder)[0] if os.listdir(extract_folder)[0] != 'output' else os.listdir(extract_folder)[1]
90
- print(input_folder)
91
 
92
  # 删除临时文件
93
  os.remove("temp.zip")
94
 
95
  if st.button('自动处理并生成ZIP文件'):
96
- if 'input_folder' in globals():
97
  input_path = os.path.join(extract_folder, input_folder)
98
  output_path = os.path.join(extract_folder, 'output')
99
  result = doc_process(input_path=input_path, output_path=output_path)
@@ -101,7 +97,7 @@ if st.button('自动处理并生成ZIP文件'):
101
  result = '请先上传ZIP文件'
102
  st.markdown(result)
103
 
104
- if 'input_folder' in globals():
105
  # 在Streamlit中压缩目录
106
  compressed_file_name = "output"
107
  if compress_directory(os.path.join(extract_folder, 'output'), compressed_file_name):
@@ -109,13 +105,17 @@ if st.button('自动处理并生成ZIP文件'):
109
 
110
  # 创建下载链接
111
  with open(f"{compressed_file_name}.zip", "rb") as file:
112
- st.download_button("点击此处下载压缩文件", file.read(), f"{compressed_file_name}.zip")
113
  else:
114
  st.error("目录压缩失败。")
115
 
116
- if st.button('清空所有文档'):
117
  try:
118
  shutil.rmtree(extract_folder)
119
- st.write(f"文件夹 {extract_folder} 已成功删除。")
120
  except Exception as e:
121
- st.write(f"删除文件夹 {extract_folder} 时发生错误:{str(e)}")
 
 
 
 
 
 
8
 
9
 
10
  extract_folder = 'documents'
11
+ input_folder = None
12
 
13
  # 压缩目录到zip文件
14
  def compress_directory(directory_path, output_zip):
 
33
 
34
  # 遍历压缩包内所有内容,创建所有目录
35
  for file_or_path in file.namelist():
 
 
36
 
37
  # 若当前节点是文件夹
38
  if file_or_path.endswith('/'):
 
49
 
50
  # 遍历压缩包内所有内容,解压文件
51
  for file_or_path in file.namelist():
 
 
52
 
53
  # 若当前节点是文件夹
54
  if file_or_path.endswith('/'):
 
84
  # 显示解压缩完成的消息
85
  st.success(f"ZIP文件已成功解压缩到目录 {extract_folder}")
86
  input_folder = os.listdir(extract_folder)[0] if os.listdir(extract_folder)[0] != 'output' else os.listdir(extract_folder)[1]
 
87
 
88
  # 删除临时文件
89
  os.remove("temp.zip")
90
 
91
  if st.button('自动处理并生成ZIP文件'):
92
+ if input_folder:
93
  input_path = os.path.join(extract_folder, input_folder)
94
  output_path = os.path.join(extract_folder, 'output')
95
  result = doc_process(input_path=input_path, output_path=output_path)
 
97
  result = '请先上传ZIP文件'
98
  st.markdown(result)
99
 
100
+ if input_folder:
101
  # 在Streamlit中压缩目录
102
  compressed_file_name = "output"
103
  if compress_directory(os.path.join(extract_folder, 'output'), compressed_file_name):
 
105
 
106
  # 创建下载链接
107
  with open(f"{compressed_file_name}.zip", "rb") as file:
108
+ st.download_button("点击此处下载ZIP文件", file.read(), f"{compressed_file_name}.zip")
109
  else:
110
  st.error("目录压缩失败。")
111
 
112
+ if st.button('清空输出文档', type='primary'):
113
  try:
114
  shutil.rmtree(extract_folder)
 
115
  except Exception as e:
116
+ st.write(f"删除文件夹 {extract_folder} 时发生错误:{str(e)}")
117
+
118
+ if extract_folder in os.listdir():
119
+ st.markdown('#### :red[完成任务后请点击“清空输出文档”]')
120
+ else:
121
+ st.markdown('#### 所有输出文档已清空')