Process a folder containing text, PDF, and image files. Returns: Tuple containing: - Concatenated text content from all text and PDF files - Path to a selected image (or None if no images found)
(
folder_path: str, custom_logger: Any = None
)
| 78 | |
| 79 | |
| 80 | def process_folder( |
| 81 | folder_path: str, custom_logger: Any = None |
| 82 | ) -> Tuple[str, Optional[str]]: |
| 83 | """Process a folder containing text, PDF, and image files. |
| 84 | |
| 85 | Returns: |
| 86 | Tuple containing: |
| 87 | - Concatenated text content from all text and PDF files |
| 88 | - Path to a selected image (or None if no images found) |
| 89 | """ |
| 90 | # Use custom logger if provided, otherwise use module logger |
| 91 | log = custom_logger if custom_logger else logger |
| 92 | |
| 93 | if not os.path.exists(folder_path) or not os.path.isdir(folder_path): |
| 94 | log.error(f"Folder not found or not a directory: {folder_path}") |
| 95 | return "", None |
| 96 | |
| 97 | log.info(f"📂 Processing folder: {folder_path}") |
| 98 | |
| 99 | # Find all text, PDF, and image files in the folder |
| 100 | txt_files = glob.glob(os.path.join(folder_path, "*.txt")) |
| 101 | pdf_files = glob.glob(os.path.join(folder_path, "*.pdf")) |
| 102 | image_files = [] |
| 103 | for ext in ["*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp"]: |
| 104 | image_files.extend(glob.glob(os.path.join(folder_path, ext))) |
| 105 | |
| 106 | # Process text files |
| 107 | all_text_content = [] |
| 108 | for txt_file in txt_files: |
| 109 | txt_content = extract_text_from_txt(txt_file, custom_logger) |
| 110 | if txt_content: |
| 111 | all_text_content.append( |
| 112 | f"--- Content from {os.path.basename(txt_file)} ---\n{txt_content}" |
| 113 | ) |
| 114 | |
| 115 | # Process PDF files |
| 116 | for pdf_file in pdf_files: |
| 117 | pdf_content = extract_text_from_pdf(pdf_file, custom_logger) |
| 118 | if pdf_content: |
| 119 | all_text_content.append( |
| 120 | f"--- Content from {os.path.basename(pdf_file)} ---\n{pdf_content}" |
| 121 | ) |
| 122 | |
| 123 | # Select one image (if available) |
| 124 | selected_image = None |
| 125 | if image_files: |
| 126 | selected_image = image_files[0] # Select the first image |
| 127 | log.info(f"🖼️ Selected image: {selected_image}") |
| 128 | |
| 129 | # Combine all text content |
| 130 | combined_text = "\n\n".join(all_text_content) |
| 131 | |
| 132 | file_summary = f"Processed {len(txt_files)} text files, {len(pdf_files)} PDF files, and found {len(image_files)} images." |
| 133 | log.info(f"✅ Folder processing complete. {file_summary}") |
| 134 | |
| 135 | return combined_text, selected_image |
no test coverage detected