function main_v63
A test harness function that validates the ability to open and process PowerPoint and Word document files, with fallback to LibreOffice conversion for problematic files.
/tf/active/vicechatdev/docchat/test_problematic_files.py
161 - 211
moderate
Purpose
This function serves as a comprehensive testing utility for document file processing. It iterates through a predefined list of test files (PPTX, PPT, DOCX, DOC, DOCM formats), attempts to open them using native Python libraries (python-pptx and python-docx), and falls back to LibreOffice conversion if direct opening fails. It provides detailed console output with status indicators and generates a summary report of all test results.
Source Code
def main():
print("="*80)
print("TESTING PROBLEMATIC FILES")
print("="*80)
results = {}
for file_path in test_files:
file_path_obj = Path(file_path)
if not file_path_obj.exists():
print(f"\nā Skipping non-existent file: {file_path_obj.name}")
results[file_path_obj.name] = "NOT_FOUND"
continue
ext = file_path_obj.suffix.lower()
if ext in ['.pptx', '.ppt']:
success = test_pptx_file(file_path)
results[file_path_obj.name] = "PASS" if success else "FAIL"
# If direct opening failed, try LibreOffice conversion
if not success:
print(f"\nTrying LibreOffice conversion as fallback...")
conv_success = test_libreoffice_conversion(file_path)
if conv_success:
results[file_path_obj.name] = "PASS_WITH_CONVERSION"
elif ext in ['.docx', '.doc', '.docm']:
success = test_docx_file(file_path)
results[file_path_obj.name] = "PASS" if success else "FAIL"
# If direct opening failed, try LibreOffice conversion
if not success:
print(f"\nTrying LibreOffice conversion as fallback...")
conv_success = test_libreoffice_conversion(file_path)
if conv_success:
results[file_path_obj.name] = "PASS_WITH_CONVERSION"
# Print summary
print("\n" + "="*80)
print("SUMMARY")
print("="*80)
for filename, status in results.items():
status_icon = {
"PASS": "ā",
"FAIL": "ā",
"PASS_WITH_CONVERSION": "ā ļø",
"NOT_FOUND": "ā"
}.get(status, "?")
print(f"{status_icon} {filename}: {status}")
Return Value
This function does not return any value (implicitly returns None). Instead, it prints test results to the console and displays a summary table showing the status of each tested file with visual indicators (ā for pass, ā for fail, ā ļø for pass with conversion, ā for not found).
Dependencies
pathlibtracebackpython-pptxpython-docxsubprocesstempfilesys
Required Imports
import sys
from pathlib import Path
import traceback
import pptx
from docx import Document as DocxDocument
import subprocess
import tempfile
Usage Example
# Define required dependencies first
test_files = [
'/path/to/presentation.pptx',
'/path/to/document.docx',
'/path/to/legacy.ppt'
]
def test_pptx_file(file_path):
try:
prs = pptx.Presentation(file_path)
return True
except:
return False
def test_docx_file(file_path):
try:
doc = DocxDocument(file_path)
return True
except:
return False
def test_libreoffice_conversion(file_path):
try:
result = subprocess.run(['libreoffice', '--headless', '--convert-to', 'pdf', file_path], capture_output=True)
return result.returncode == 0
except:
return False
# Run the test suite
if __name__ == '__main__':
main()
Best Practices
- Ensure the 'test_files' list is populated with valid file paths before calling this function
- Implement the required helper functions (test_pptx_file, test_docx_file, test_libreoffice_conversion) before using this function
- Install LibreOffice on the system to enable the conversion fallback feature
- Consider adding error handling for the case where helper functions are not defined
- The function modifies no state and only produces console output, making it safe for repeated execution
- Use this function as part of a test suite or diagnostic tool rather than in production code
- Consider capturing the results dictionary for programmatic access instead of relying solely on console output
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_pptx_file 74.1% similar
-
function test_docx_file 71.6% similar
-
function test_libreoffice_conversion 64.1% similar
-
function main_v19 63.5% similar
-
function test_document_extractor 62.5% similar