Skip to main content

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 paragraph
  • properties: 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 points
  • bold: Boolean for bold text
  • color: Hex color code (e.g., 'FF0000' for red)
  • rtl: Boolean for right-to-left text direction
  • alignment: Alignment ('left', 'center', 'right', 'justify')
  • highlight: Highlight color
  • underline: Boolean for underlined text

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docxfa-1.0.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docxfa-1.0.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

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

Hashes for docxfa-1.0.0.tar.gz
Algorithm Hash digest
SHA256 619a352818182c36e465113281a34cdb010ada049a9436ab04ca9d285fed4c4e
MD5 e01fb8ce6f43f45c55245a3fd2ab964a
BLAKE2b-256 f99168ba40badeb09e0a665c3cf887981ac75bf50d71c8e800b747f2db96a3d8

See more details on using hashes here.

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

Hashes for docxfa-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 18cf0820de01dbfce8fd204b554ab5f4ceaee461845acddb7199f7d4069f8f8d
MD5 f6b92b0044eb113604c593309c42afd3
BLAKE2b-256 db444a233c62f1df08696b8c512b45ea8655bacb452148f273f2fe40596ca3e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page