feat: Enhance directory processing to preserve full folder structure
This commit is contained in:
parent
9b9322ddec
commit
6a580785a8
@ -2,6 +2,7 @@ import streamlit as st
|
|||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import warnings
|
import warnings
|
||||||
|
import shutil
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
import tempfile
|
import tempfile
|
||||||
import zipfile
|
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)
|
os.makedirs(dest_dir, exist_ok=True)
|
||||||
|
|
||||||
# Get all files recursively
|
# Get all files recursively
|
||||||
if file_type == "Excel":
|
all_files = []
|
||||||
files = glob.glob(os.path.join(source_dir, "**/*.xlsx"), recursive=True)
|
files_to_process = []
|
||||||
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))
|
|
||||||
|
|
||||||
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")
|
st.warning(f"No {file_type} files found in the source directory")
|
||||||
else:
|
|
||||||
progress_bar = st.progress(0)
|
total_files = len(files_to_process) + len(all_files)
|
||||||
status_text = st.empty()
|
progress_bar = st.progress(0)
|
||||||
|
status_text = st.empty()
|
||||||
for idx, source_path in enumerate(files):
|
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:
|
try:
|
||||||
relative_path = os.path.relpath(source_path, source_dir)
|
relative_path = os.path.relpath(source_path, source_dir)
|
||||||
dest_path = os.path.join(dest_dir, relative_path)
|
dest_path = os.path.join(dest_dir, relative_path)
|
||||||
|
|
||||||
with st.expander(f"Processing {relative_path}", expanded=True):
|
# Create destination directory if it doesn't exist
|
||||||
st.write(f"📝 Processing {relative_path}...")
|
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
|
||||||
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!")
|
|
||||||
|
|
||||||
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)
|
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:
|
except Exception as e:
|
||||||
st.error(f"❌ Error processing {relative_path}: {str(e)}")
|
st.error(f"❌ Error copying {relative_path}: {str(e)}")
|
||||||
|
|
||||||
st.success(f"✨ All files processed and saved to: {dest_dir}")
|
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
|
# Footer
|
||||||
st.sidebar.markdown("---")
|
st.sidebar.markdown("---")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user