What I’m trying to accomplish:
- Create a paragraph style in python-docx with user defined Persian font and size (a CTL language)
Problem:
I can do this with non-CTL languages like English:
from docx import Document from docx.enum.style import WD_STYLE_TYPE from docx.shared import Pt user_font_name = 'FreeMono' user_font_size = 14 doc = Document() my_style = doc.styles.add_style('style_name',WD_STYLE_TYPE.PARAGRAPH) my_font = my_style.font my_font.name = user_font_name my_font.size = Pt(user_font_size) p = doc.add_paragraph('some text',my_style) # persian_p = doc.add_paragraph('نوشته',my_style) # FreeMono supports Persian language so the problem is not the font doc.save('file.docx')
However if I change the text to a Persian text, its font won’t change to the specified font.
Why this happens:
- My specified font only changes western font family of style and doesn’t do anything to CTL font family
How I know this:
- If I open the docx file with LibreOffice and open the style and go into the font section, I can see that my specified font and size are there in “Western Text Font Family” but not in “CTL Font Family”. And as a result my CTL text font becomes the default font.
Additional info:
- I’m using LibreOffice on Linux
- Changing the default style doesn’t do me any good in this situation because I want the user to specify font name and size.
- I have no experience in changing xml files (let alone docx xml files)
- python-docx version is 0.8.6