Search code examples
python-docx

How to apply custom (proprietary) font for paragraph in python docx?


I am a bit lost with reading the python docx documentation.

I read the file with a proprietary font inside.

document_original = docx.Document('doc_orig.docx')
styles = document_original.styles
propitiatory_default = styles['Default']

I check that indeed the style has the font I need.

print(proprietary_default.font.name)

I create a new document with text with the style and font I need.

doc = docx.Document()
para = doc.add_paragraph()
para.style = proprietary_default
para_run = para.add_run('this is a text')
file_new = 'font_test_2'
doc.save(  file_new+ '.docx')

However if I open the file, I do not see the proprietary_default style.

document_new = docx.Document(file_new+'.docx')
styles = document_new.styles
for s in styles:
    print(s.name)

Do you know how can I solve this issue?


Solution

  • Font should be applied not to style, but to paragraphs' font name:

    doc = docx.Document()
    para = doc.add_paragraph()
    para_run = para.add_run('this is a text')
    para_run.font.name=proprietary_default.font.name