|
import streamlit as st |
|
import os |
|
import shutil |
|
import zipfile |
|
from zipfile import ZipFile |
|
|
|
from doc_process import doc_process |
|
|
|
|
|
extract_folder = 'documents' |
|
compressed_file_name = "output" |
|
input_folder = None |
|
|
|
|
|
def compress_directory(directory_path, output_zip): |
|
try: |
|
shutil.make_archive(output_zip, 'zip', directory_path) |
|
return True |
|
except Exception as e: |
|
return str(e) |
|
|
|
def recode(raw: str) -> str: |
|
''' |
|
编码修正 |
|
''' |
|
|
|
try: |
|
return raw.encode('cp437').decode('utf-8') |
|
|
|
except: |
|
return raw.encode('utf-8').decode('utf-8') |
|
|
|
def zip_extract_all(src_zip_file: ZipFile, target_path: str) -> None: |
|
|
|
|
|
for file_or_path in file.namelist(): |
|
|
|
|
|
if file_or_path.endswith('/'): |
|
try: |
|
|
|
os.makedirs(os.path.join(target_path, recode(file_or_path))) |
|
except FileExistsError: |
|
|
|
pass |
|
|
|
|
|
else: |
|
pass |
|
|
|
|
|
for file_or_path in file.namelist(): |
|
|
|
|
|
if file_or_path.endswith('/'): |
|
pass |
|
|
|
|
|
else: |
|
|
|
with open(os.path.join(target_path, recode(file_or_path)), 'wb') as z: |
|
|
|
shutil.copyfileobj(src_zip_file.open(file_or_path), z) |
|
|
|
st.header('起诉书 & 委托书 - 自动处理程序') |
|
|
|
with open('readme.txt', 'r') as f: |
|
readme = f.read() |
|
|
|
if st.toggle('显示说明文档'): |
|
st.markdown(readme.split('[img]')[0]) |
|
st.image('docs_format.jpg') |
|
st.markdown(readme.split('[img]')[1]) |
|
|
|
|
|
uploaded_file = st.file_uploader("选择要上传的文件", type=["zip"]) |
|
|
|
|
|
if uploaded_file: |
|
|
|
with open("temp.zip", "wb") as f: |
|
f.write(uploaded_file.read()) |
|
|
|
|
|
os.makedirs(extract_folder, exist_ok=True) |
|
|
|
|
|
with zipfile.ZipFile("temp.zip", "r") as file: |
|
|
|
|
|
zip_extract_all(file, extract_folder) |
|
|
|
|
|
st.success(f"ZIP文件已成功解压缩到目录 {extract_folder}") |
|
input_folder = os.listdir(extract_folder)[0] if os.listdir(extract_folder)[0] != 'output' else os.listdir(extract_folder)[1] |
|
|
|
|
|
os.remove("temp.zip") |
|
|
|
if st.button('自动处理并生成ZIP文件'): |
|
if input_folder: |
|
input_path = os.path.join(extract_folder, input_folder) |
|
output_path = os.path.join(extract_folder, 'output') |
|
result = doc_process(input_path=input_path, output_path=output_path) |
|
else: |
|
result = '请先上传ZIP文件' |
|
st.markdown(result) |
|
|
|
if input_folder: |
|
|
|
if compress_directory(os.path.join(extract_folder, 'output'), compressed_file_name): |
|
st.success("导出目录已成功压缩为ZIP文件") |
|
|
|
|
|
with open(f"{compressed_file_name}.zip", "rb") as file: |
|
st.download_button("点击此处下载ZIP文件", file.read(), f"{compressed_file_name}.zip") |
|
else: |
|
st.error("目录压缩失败。") |
|
|
|
if st.button('清空输出文档', type='primary'): |
|
try: |
|
shutil.rmtree(extract_folder) |
|
except Exception as e: |
|
|
|
pass |
|
|
|
try: |
|
os.remove(f"{compressed_file_name}.zip") |
|
except Exception as e: |
|
|
|
pass |
|
|
|
if extract_folder in os.listdir() or f"{compressed_file_name}.zip" in os.listdir(): |
|
st.markdown(':red[完成任务后请点击“清空输出文档”]') |
|
else: |
|
st.markdown('所有输出文档已清空') |