feat: Enhance directory processing to preserve full folder structure

This commit is contained in:
Bobby Abellana (aider) 2025-02-11 10:12:08 -08:00
parent 9b9322ddec
commit 6a580785a8

View File

@ -2,6 +2,7 @@ import streamlit as st
import os
import logging
import warnings
import shutil
from io import BytesIO
import tempfile
import zipfile
@ -266,43 +267,79 @@ elif input_method == "Select Directory" and source_dir and dest_dir and st.butto
os.makedirs(dest_dir, exist_ok=True)
# Get all files recursively
if file_type == "Excel":
files = glob.glob(os.path.join(source_dir, "**/*.xlsx"), recursive=True)
files.extend(glob.glob(os.path.join(source_dir, "**/*.xlsm"), recursive=True))
else: # Word
files = glob.glob(os.path.join(source_dir, "**/*.docx"), recursive=True)
files.extend(glob.glob(os.path.join(source_dir, "**/*.docm"), recursive=True))
all_files = []
files_to_process = []
if not files:
for root, _, files in os.walk(source_dir):
for file in files:
full_path = os.path.join(root, file)
file_lower = file.lower()
if file_type == "Excel" and file_lower.endswith(('.xlsx', '.xlsm')):
files_to_process.append(full_path)
elif file_type == "Word" and file_lower.endswith(('.docx', '.docm')):
files_to_process.append(full_path)
else:
all_files.append(full_path)
if not files_to_process:
st.warning(f"No {file_type} files found in the source directory")
else:
progress_bar = st.progress(0)
status_text = st.empty()
for idx, source_path in enumerate(files):
total_files = len(files_to_process) + len(all_files)
progress_bar = st.progress(0)
status_text = st.empty()
files_processed = 0
# Process Office files
for source_path in files_to_process:
try:
relative_path = os.path.relpath(source_path, source_dir)
dest_path = os.path.join(dest_dir, relative_path)
with st.expander(f"Processing {relative_path}", expanded=True):
st.write(f"📝 Processing {relative_path}...")
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
if file_type == "Excel":
copy_excel_file(source_path, dest_path, passwords)
else: # Word
remove_all_protection_tags(source_path, dest_path)
st.success("✅ Processing complete!")
files_processed += 1
progress = files_processed / total_files
progress_bar.progress(progress)
status_text.text(f"Processed {files_processed} of {total_files} files")
except Exception as e:
st.error(f"❌ Error processing {relative_path}: {str(e)}")
# Copy all other files
with st.expander("Copying other files", expanded=True):
for source_path in all_files:
try:
relative_path = os.path.relpath(source_path, source_dir)
dest_path = os.path.join(dest_dir, relative_path)
with st.expander(f"Processing {relative_path}", expanded=True):
st.write(f"📝 Processing {relative_path}...")
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
if file_type == "Excel":
copy_excel_file(source_path, dest_path, passwords)
else: # Word
remove_all_protection_tags(source_path, dest_path)
st.success("✅ Processing complete!")
# Create destination directory if it doesn't exist
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
progress = (idx + 1) / len(files)
# Copy the file
import shutil
shutil.copy2(source_path, dest_path)
files_processed += 1
progress = files_processed / total_files
progress_bar.progress(progress)
status_text.text(f"Processed {idx + 1} of {len(files)} files")
status_text.text(f"Processed {files_processed} of {total_files} files")
except Exception as e:
st.error(f"❌ Error processing {relative_path}: {str(e)}")
st.success(f"✨ All files processed and saved to: {dest_dir}")
st.error(f"❌ Error copying {relative_path}: {str(e)}")
st.success(f"✨ All files processed and saved to: {dest_dir}")
if len(all_files) > 0:
st.info(f"📁 Copied {len(all_files)} additional files to maintain folder structure")
# Footer
st.sidebar.markdown("---")