Search code examples
pythonpython-3.xpython-docx

Can't use RTL direction when using font.name or font.size in python-docx


I'm using python-docx for writing new ms words. Python 3.8 - Win10 x32
There is code:

doc = docx.Document()
style = doc.styles['Normal']
font = style.font
font.name = 'B Mitra'
font.size = docx.shared.Pt(13)
font.rtl = True
doc.add_paragraph("Hi in Persian= سلام")
doc.save("tst.docx")

without font.rtl=True both font.size and font.name will set on paragraph but when i use font.rtl=True the size and font won't set. just direction will set.
Any ideas?
Thanks for help


Solution

  • A style can have more than one font, like one for latin characters and another for cursive script (used for languages like Arabic and Persian). style.font is only settings for the latin font because that's the most common. There is no built-in python-pptx support for setting the cursive font, although if you search around you may find someone who has managed to get it done.

    This other question has some information: python-docx add_style with CTL (Complex text layout) language

    I found it by searching on python-docx arabic font.

    Also you can look into creating a "starting" or "template" .docx file that you load using

    document = docx.Document("my-template-document.docx")
    

    Then you can use Word to make all the paragraph styles and other document settings you want and just have python-docx use those styles where you want them.