Improve Error Handling and PDF Processing in pdf_util.py and pdf_compiler.py #5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces several enhancements and bug fixes to the PDF processing utilities within the pdf_util.py and pdf_compiler.py modules.
Key Changes
Retry Logic: The rtf_file_to_pdf method now includes a retry mechanism that attempts to convert RTF files to PDF up to three times before reporting a failure. This addresses intermittent issues with Word automation where conversions might fail randomly.
Process Cleanup: Improved the cleanup process by ensuring that Word instances are properly closed after each attempt, preventing orphaned processes that could consume system resources.
Refined close_word_proc Method: Modified this method to handle process termination more gracefully. It now attempts to kill Word-related processes within a try-except block, ensuring that exceptions are caught and handled without crashing the application.
Interactive and Silent Modes: The method now distinguishes between silent mode and interactive mode, asking the user for confirmation in interactive mode before terminating processes.
New _fitz_combine Method: Added a helper method to streamline the PDF combining process using PyMuPDF (fitz). This method handles bookmarks, table of contents, and encryption more effectively.
Conditional TOC Addition: The addition of the Table of Contents is now conditional, only occurring if PDFs were successfully combined. This prevents errors when there are no PDFs to combine.
Detailed Logging: Updated logging messages to provide clearer and more informative feedback, including the number of files processed and successfully added to the combined PDF.
User Notifications: Enhanced user notifications for successful operations and error conditions, making it easier to understand the process flow and troubleshoot issues.
Data Processing: Simplified data handling using pandas DataFrames, improving code readability and performance.
Removed Redundancies: Eliminated unnecessary code segments, such as unused variables and redundant loops.
Error Messages: Standardized error messages for consistency and clarity.
Testing and Validation
RTF to PDF Conversion: Verified that RTF files are correctly converted to PDFs, and the retry logic effectively handles intermittent failures.
Process Termination: Confirmed that Word processes are terminated appropriately after conversions, both in silent and interactive modes.
PDF Combining: Tested the PDF combining process with various datasets to ensure bookmarks and TOC are correctly handled.
Logging Output: Reviewed logging outputs to ensure messages are accurate and helpful for end-users.