Download create_test_template.py from fokan/pdf-4: direct link, hf CLI and curl.
- Browser
- Download file 10 kB
-
https://huggingface.co/spaces/fokan/pdf-4/resolve/main/create_test_template.py
- Command line
-
hf download hf://spaces/fokan/pdf-4/create_test_template.py
-
curl -L -o create_test_template.py https://huggingface.co/spaces/fokan/pdf-4/resolve/main/create_test_template.py
10 kB
| #!/usr/bin/env python3 | |
| """ | |
| Create a test template.docx file to demonstrate the dynamic font sizing system | |
| """ | |
| import zipfile | |
| import tempfile | |
| import os | |
| from pathlib import Path | |
| def create_test_template_docx(): | |
| """Create a test template.docx file with placeholders""" | |
| # Document.xml content with placeholders in different contexts | |
| document_xml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
| <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> | |
| <w:body> | |
| <w:p> | |
| <w:r> | |
| <w:rPr> | |
| <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> | |
| <w:sz w:val="24"/> | |
| </w:rPr> | |
| <w:t>عقد بيع عقار</w:t> | |
| </w:r> | |
| </w:p> | |
| <w:tbl> | |
| <w:tblPr> | |
| <w:tblW w:w="5000" w:type="pct"/> | |
| </w:tblPr> | |
| <w:tr> | |
| <w:tc> | |
| <w:tcPr> | |
| <w:tcW w:w="2500" w:type="pct"/> | |
| </w:tcPr> | |
| <w:p> | |
| <w:r> | |
| <w:rPr> | |
| <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> | |
| <w:sz w:val="20"/> | |
| </w:rPr> | |
| <w:t>الطرف الأول (البائع): {{name_1}}</w:t> | |
| </w:r> | |
| </w:p> | |
| </w:tc> | |
| <w:tc> | |
| <w:tcPr> | |
| <w:tcW w:w="2500" w:type="pct"/> | |
| </w:tcPr> | |
| <w:p> | |
| <w:r> | |
| <w:rPr> | |
| <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> | |
| <w:sz w:val="20"/> | |
| </w:rPr> | |
| <w:t>رقم الهوية: {{id_1}}</w:t> | |
| </w:r> | |
| </w:p> | |
| </w:tc> | |
| </w:tr> | |
| <w:tr> | |
| <w:tc> | |
| <w:p> | |
| <w:r> | |
| <w:rPr> | |
| <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> | |
| <w:sz w:val="20"/> | |
| </w:rPr> | |
| <w:t>الطرف الثاني (المشتري): {{name_2}}</w:t> | |
| </w:r> | |
| </w:p> | |
| </w:tc> | |
| <w:tc> | |
| <w:p> | |
| <w:r> | |
| <w:rPr> | |
| <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> | |
| <w:sz w:val="20"/> | |
| </w:rPr> | |
| <w:t>رقم الهوية: {{id_2}}</w:t> | |
| </w:r> | |
| </w:p> | |
| </w:tc> | |
| </w:tr> | |
| <w:tr> | |
| <w:tc> | |
| <w:p> | |
| <w:r> | |
| <w:rPr> | |
| <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> | |
| <w:sz w:val="18"/> | |
| </w:rPr> | |
| <w:t>العنوان: {{location_1}}</w:t> | |
| </w:r> | |
| </w:p> | |
| </w:tc> | |
| <w:tc> | |
| <w:p> | |
| <w:r> | |
| <w:rPr> | |
| <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> | |
| <w:sz w:val="18"/> | |
| </w:rPr> | |
| <w:t>الهاتف: {{phone_1}}</w:t> | |
| </w:r> | |
| </w:p> | |
| </w:tc> | |
| </w:tr> | |
| </w:tbl> | |
| <w:p> | |
| <w:r> | |
| <w:rPr> | |
| <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> | |
| <w:sz w:val="22"/> | |
| </w:rPr> | |
| <w:t>الشاهد الأول: {{name_3}}</w:t> | |
| </w:r> | |
| </w:p> | |
| <w:p> | |
| <w:r> | |
| <w:rPr> | |
| <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> | |
| <w:sz w:val="18"/> | |
| </w:rPr> | |
| <w:t>التاريخ: {{date}} الساعة: {{t_11}}</w:t> | |
| </w:r> | |
| </w:p> | |
| <w:p> | |
| <w:r> | |
| <w:rPr> | |
| <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/> | |
| <w:sz w:val="16"/> | |
| </w:rPr> | |
| <w:t>الرقم التسلسلي: {{serial_number}}</w:t> | |
| </w:r> | |
| </w:p> | |
| </w:body> | |
| </w:document>''' | |
| # App.xml content | |
| app_xml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
| <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"> | |
| <Application>Microsoft Office Word</Application> | |
| <DocSecurity>0</DocSecurity> | |
| <ScaleCrop>false</ScaleCrop> | |
| <SharedDoc>false</SharedDoc> | |
| <HyperlinksChanged>false</HyperlinksChanged> | |
| <AppVersion>16.0000</AppVersion> | |
| </Properties>''' | |
| # Core.xml content | |
| core_xml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
| <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
| <dc:title>Test Template</dc:title> | |
| <dc:creator>Dynamic Font Sizing System</dc:creator> | |
| <dcterms:created xsi:type="dcterms:W3CDTF">2024-01-01T00:00:00Z</dcterms:created> | |
| <dcterms:modified xsi:type="dcterms:W3CDTF">2024-01-01T00:00:00Z</dcterms:modified> | |
| </cp:coreProperties>''' | |
| # Content_Types.xml | |
| content_types_xml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
| <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> | |
| <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> | |
| <Default Extension="xml" ContentType="application/xml"/> | |
| <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/> | |
| <Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/> | |
| <Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/> | |
| </Types>''' | |
| # _rels/.rels | |
| rels_xml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
| <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> | |
| <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/> | |
| <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/> | |
| <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/> | |
| </Relationships>''' | |
| # word/_rels/document.xml.rels | |
| word_rels_xml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
| <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> | |
| </Relationships>''' | |
| # Create the DOCX file | |
| template_path = "template.docx" | |
| with zipfile.ZipFile(template_path, 'w', zipfile.ZIP_DEFLATED) as docx: | |
| # Add all the required files | |
| docx.writestr('[Content_Types].xml', content_types_xml) | |
| docx.writestr('_rels/.rels', rels_xml) | |
| docx.writestr('word/document.xml', document_xml) | |
| docx.writestr('word/_rels/document.xml.rels', word_rels_xml) | |
| docx.writestr('docProps/core.xml', core_xml) | |
| docx.writestr('docProps/app.xml', app_xml) | |
| print(f"✅ Created test template: {template_path}") | |
| return template_path | |
| def test_with_real_docx(): | |
| """Test the dynamic sizing system with a real DOCX file""" | |
| import sys | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| from app import ( | |
| validate_docx_structure, | |
| create_dynamic_font_sizing_rules, | |
| apply_dynamic_font_sizing, | |
| apply_template_font_settings | |
| ) | |
| # Create test template | |
| template_path = create_test_template_docx() | |
| try: | |
| print("\n🔍 Analyzing template structure...") | |
| docx_info = validate_docx_structure(template_path) | |
| print(f"📊 Analysis results:") | |
| print(f" • Placeholders found: {docx_info.get('placeholder_count', 0)}") | |
| print(f" • Has tables: {docx_info.get('has_tables', False)}") | |
| print(f" • RTL content: {docx_info.get('rtl_content_detected', False)}") | |
| print("\n🎯 Creating dynamic sizing rules...") | |
| dynamic_rules = create_dynamic_font_sizing_rules(template_path) | |
| if dynamic_rules: | |
| print(f"📏 Created rules for {len(dynamic_rules)} placeholders:") | |
| for placeholder, rules in dynamic_rules.items(): | |
| print(f" • {placeholder}: max_chars={rules['max_chars']}, context={rules['context']}") | |
| print("\n🔧 Applying dynamic font sizing...") | |
| processed_path = apply_dynamic_font_sizing(template_path, dynamic_rules) | |
| if processed_path != template_path: | |
| print(f"✅ Dynamic sizing applied successfully!") | |
| print(f" Original: {template_path}") | |
| print(f" Processed: {processed_path}") | |
| # Clean up processed file | |
| if os.path.exists(processed_path): | |
| os.unlink(processed_path) | |
| else: | |
| print("ℹ️ No changes were needed") | |
| else: | |
| print("❌ No dynamic rules were created") | |
| except Exception as e: | |
| print(f"❌ Error during testing: {e}") | |
| finally: | |
| # Clean up | |
| if os.path.exists(template_path): | |
| os.unlink(template_path) | |
| print(f"🧹 Cleaned up: {template_path}") | |
| if __name__ == "__main__": | |
| print("🚀 Creating and testing template.docx with dynamic font sizing\n") | |
| print("=" * 60) | |
| test_with_real_docx() | |
| print("\n" + "=" * 60) | |
| print("🎉 Template testing completed!") | |
| print("\n💡 The system is ready to handle:") | |
| print(" • ✅ Short names: محمد، علي، فاطمة") | |
| print(" • ✅ Medium names: محمد أحمد، فاطمة سعد") | |
| print(" • ✅ Long names: محمد عبدالله أحمد") | |
| print(" • ✅ Very long names: محمد عبدالله أحمد الخالدي") | |
| print(" • ✅ All while maintaining exact positioning and Arial font!") | |