#!/usr/bin/env python3 """ Test script for DOCX to PDF conversion with Arabic RTL support This script tests the conversion functionality locally """ import sys import os from pathlib import Path # Add the current directory to Python path sys.path.insert(0, str(Path(__file__).parent)) from app import convert_docx_to_pdf, setup_libreoffice, setup_font_environment def test_arabic_conversion(): """Test the Arabic DOCX to PDF conversion""" print("๐Ÿงช Testing Arabic DOCX to PDF Conversion") print("=" * 50) # Check LibreOffice setup print("1. Checking LibreOffice setup...") if not setup_libreoffice(): print("โŒ LibreOffice setup failed!") return False print("โœ… LibreOffice setup successful") # Setup font environment print("\n2. Setting up font environment...") setup_font_environment() print("โœ… Font environment setup completed") # Check for test files test_files_dir = Path("test_files") if not test_files_dir.exists(): print(f"\nโš ๏ธ Test files directory '{test_files_dir}' not found") print("Please create test_files/ directory and add sample DOCX files") return False docx_files = list(test_files_dir.glob("*.docx")) if not docx_files: print(f"\nโš ๏ธ No DOCX files found in '{test_files_dir}'") print("Please add sample DOCX files to test the conversion") return False print(f"\n3. Found {len(docx_files)} DOCX files for testing:") for docx_file in docx_files: print(f" ๐Ÿ“„ {docx_file.name}") # Test conversion for each file results_dir = Path("test_results") results_dir.mkdir(exist_ok=True) success_count = 0 total_count = len(docx_files) for docx_file in docx_files: print(f"\n4. Testing conversion: {docx_file.name}") print("-" * 30) # Create a mock file object class MockFile: def __init__(self, path): self.name = str(path) mock_file = MockFile(docx_file) try: pdf_path, status_message = convert_docx_to_pdf(mock_file) if pdf_path and os.path.exists(pdf_path): # Move the result to test_results directory result_name = docx_file.stem + "_converted.pdf" result_path = results_dir / result_name import shutil shutil.move(pdf_path, result_path) print(f"โœ… Conversion successful!") print(f"๐Ÿ“ Output: {result_path}") print(f"๐Ÿ“Š Status: {status_message[:100]}...") success_count += 1 else: print(f"โŒ Conversion failed!") print(f"๐Ÿ“Š Error: {status_message}") except Exception as e: print(f"โŒ Conversion error: {str(e)}") # Summary print(f"\n๐ŸŽฏ Test Summary:") print(f" Total files: {total_count}") print(f" Successful: {success_count}") print(f" Failed: {total_count - success_count}") print(f" Success rate: {(success_count/total_count)*100:.1f}%") if success_count > 0: print(f"\n๐Ÿ“ Results saved in: {results_dir}") return success_count == total_count def create_sample_test_files(): """Create sample test files for testing""" test_files_dir = Path("test_files") test_files_dir.mkdir(exist_ok=True) print("๐Ÿ“ Creating sample test files...") print("Note: You need to manually create DOCX files with Arabic content") print("Suggested test cases:") print("1. Simple Arabic text document") print("2. Document with Arabic tables") print("3. Mixed Arabic/English document") print("4. Document with Arabic headers and footers") print("5. Document with Arabic bullet points") print(f"\nPlace your test DOCX files in: {test_files_dir.absolute()}") if __name__ == "__main__": if len(sys.argv) > 1 and sys.argv[1] == "--create-samples": create_sample_test_files() else: test_arabic_conversion()