A Python library for creating .docx files with Persian/Arabic RTL support
Project description
docxfa
docxfa is a Python library for creating .docx files with full support for Persian/Arabic RTL (right-to-left) text. It allows you to create Word documents from scratch with mixed RTL/LTR content, proper styling, and formatting.
Features
- Create .docx files from scratch
- Full support for RTL (Persian/Arabic) and LTR (English) text
- Mixed RTL/LTR text within a single paragraph
- Paragraph styling (alignment, bold, colors, font size, etc.)
- Rich text paragraphs with multiple styles in a single line
- Proper handling of Persian/Arabic fonts and text direction
- Generates OOXML-compliant files that open without recovery errors
Installation
pip install docxfa
Usage
Basic Usage
from docxfa import docx_editor
# Create a new document
doc = docx_editor()
# Create the document structure
doc.create_document()
# Add a simple paragraph
doc.add_paragraph("سلام دنیا!", properties={'font_name': 'Tahoma', 'font_size': 12, 'rtl': True})
# Save the document
doc.save("output.docx")
Advanced Usage with Mixed Text
from docxfa import docx_editor
doc = docx_editor()
doc.create_document()
# Add a paragraph with mixed RTL/LTR text
parts = [
("متن فارسی ", {'font_name': 'Tahoma', 'font_size': 12, 'rtl': True}),
("English text ", {'font_name': 'Arial', 'font_size': 12, 'rtl': False}),
("و دوباره فارسی", {'font_name': 'Tahoma', 'font_size': 12, 'rtl': True})
]
doc.add_rich_text_paragraph(parts)
doc.save("mixed_text.docx")
Paragraph Styling
from docxfa import docx_editor
doc = docx_editor()
doc.create_document()
# Add styled paragraph
properties = {
'font_name': 'Tahoma',
'font_size': 14,
'bold': True,
'color': 'FF0000', # Red color
'rtl': True,
'alignment': 'right' # Right alignment for RTL text
}
doc.add_paragraph("پاراگراف استایل دار", properties=properties)
doc.save("styled.docx")
API
docx_editor class
create_document()
Creates a new blank Word document with all necessary namespaces.
add_paragraph(text, properties=None)
Adds a new paragraph with a single style for the entire text.
text: The text content of the paragraphproperties: Dictionary of styling properties
add_rich_text_paragraph(parts, paragraph_properties=None)
Adds a paragraph with multiple text runs, each with its own style.
parts: A list of tuples, where each tuple is (text, properties_dict)paragraph_properties: A dictionary for paragraph-level properties like alignment
save(output_path)
Saves the document to a .docx file.
Properties
Available properties for text styling:
font_name: Font name (e.g., 'Tahoma', 'Arial')font_size: Font size in pointsbold: Boolean for bold textcolor: Hex color code (e.g., 'FF0000' for red)rtl: Boolean for right-to-left text directionalignment: Alignment ('left', 'center', 'right', 'justify')highlight: Highlight colorunderline: Boolean for underlined text
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file docxfa-1.0.0.tar.gz.
File metadata
- Download URL: docxfa-1.0.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
619a352818182c36e465113281a34cdb010ada049a9436ab04ca9d285fed4c4e
|
|
| MD5 |
e01fb8ce6f43f45c55245a3fd2ab964a
|
|
| BLAKE2b-256 |
f99168ba40badeb09e0a665c3cf887981ac75bf50d71c8e800b747f2db96a3d8
|
File details
Details for the file docxfa-1.0.0-py3-none-any.whl.
File metadata
- Download URL: docxfa-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18cf0820de01dbfce8fd204b554ab5f4ceaee461845acddb7199f7d4069f8f8d
|
|
| MD5 |
f6b92b0044eb113604c593309c42afd3
|
|
| BLAKE2b-256 |
db444a233c62f1df08696b8c512b45ea8655bacb452148f273f2fe40596ca3e5
|