Python docx library text align

后端 未结 2 1403
栀梦
栀梦 2021-01-19 15:16

I am using python docx library to manipulate a word document. However I can\'t find how to align a line to the center in the documents page of that library. I can\'t find by

2条回答
  •  情歌与酒
    2021-01-19 15:38

    With the new version of python-docx 0.7 https://github.com/python-openxml/python-docx/commit/158f2121bcd2c58b258dec1b83f8fef15316de19 Add feature #51: Paragraph.alignment (read/write) Now it is possible to align a paragraph as here: http://python-docx.readthedocs.org/en/latest/dev/analysis/features/par-alignment.html

     paragraph = document.add_paragraph("This is a text")
     paragraph.alignment = 0 # for left, 1 for center, 2 right, 3 justify ....
    

    edit from comments

    actually it is 0 for left, 1 for center, 2 for right

    edit 2 from comments

    You shouldn't hard code magic numbers like this. Use WD_ALIGN_PARAGRAPH.CENTER to get the correct value for centering, etc. To do this use the following import

    from docx.enum.text import WD_ALIGN_PARAGRAPH 
    

提交回复
热议问题