feat: Add zip download feature for multiple processed files
This commit is contained in:
parent
d2a9c49d5c
commit
80186643bb
@ -4,6 +4,7 @@ import logging
|
||||
import warnings
|
||||
from io import BytesIO
|
||||
import tempfile
|
||||
import zipfile
|
||||
from main import (
|
||||
load_workbook_with_possible_passwords,
|
||||
copy_excel_file,
|
||||
@ -28,6 +29,14 @@ def save_uploaded_file(uploaded_file):
|
||||
tmp_file.write(uploaded_file.getvalue())
|
||||
return tmp_file.name
|
||||
|
||||
def create_zip_file(files_dict):
|
||||
"""Create a zip file containing all processed files"""
|
||||
zip_buffer = BytesIO()
|
||||
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
||||
for filename, content in files_dict.items():
|
||||
zip_file.writestr(filename, content)
|
||||
return zip_buffer
|
||||
|
||||
# Main UI
|
||||
st.title("🔓 Office File Protection Remover")
|
||||
st.write("Remove protection from Excel and Word files easily")
|
||||
@ -87,6 +96,9 @@ if uploaded_files:
|
||||
progress_bar = st.progress(0)
|
||||
status_text = st.empty()
|
||||
|
||||
# Dictionary to store processed files for zip download
|
||||
processed_files = {}
|
||||
|
||||
for idx, uploaded_file in enumerate(uploaded_files):
|
||||
try:
|
||||
# Create a container for each file
|
||||
@ -109,6 +121,7 @@ if uploaded_files:
|
||||
# Provide download button
|
||||
with open(temp_output_path, "rb") as f:
|
||||
processed_file = f.read()
|
||||
processed_files[f"processed_{uploaded_file.name}"] = processed_file
|
||||
|
||||
mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" if file_type == "Excel" else "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
|
||||
@ -136,6 +149,16 @@ if uploaded_files:
|
||||
progress_bar.empty()
|
||||
status_text.text("✨ All processing complete!")
|
||||
|
||||
# Add zip download button if multiple files were processed
|
||||
if len(processed_files) > 1:
|
||||
zip_buffer = create_zip_file(processed_files)
|
||||
st.download_button(
|
||||
label="⬇️ Download all files as ZIP",
|
||||
data=zip_buffer.getvalue(),
|
||||
file_name="processed_files.zip",
|
||||
mime="application/zip",
|
||||
)
|
||||
|
||||
# Footer
|
||||
st.sidebar.markdown("---")
|
||||
st.sidebar.markdown("### Instructions")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user