File size: 10,301 Bytes
86fce4f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
#!/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!")
|