Python docx library text align

…衆ロ難τιáo~ 提交于 2019-12-01 22:29:06
Levent Altunöz

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 right, 2 center, 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 
p = document.add_paragraph('A plain paragraph having some ',style='BodyText', breakbefore=False, jc='left')# @param string jc: Paragraph alignment, possible values:left, center, right, both (justified), ...

for reference see this reference at def paragraph read the documentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!