How to add text in superscript or subscript with python docx

℡╲_俬逩灬. 提交于 2019-12-08 04:58:41

问题


In the python docx quickstart guide (https://python-docx.readthedocs.io/en/latest/) you can see that it is possible to use the add_run-command and add bold text to a sentence.

document = Document()
document.add_heading('Document Title', 0)
p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True

I would to use the same add_run-command but instead add text that is superscripted or subscripted.

Is this possible to achieve?

Any help much appreciated!

/V


回答1:


The call to add_run() returns a Run object that you can use to change font options.

from docx import Document
document = Document()

p = document.add_paragraph('Normal text with ')

super_text = p.add_run('superscript text')
super_text.font.superscript = True

p.add_run(' and ')

sub_text = p.add_run('subscript text')
sub_text.font.subscript = True

document.save('test.docx')



来源:https://stackoverflow.com/questions/40747398/how-to-add-text-in-superscript-or-subscript-with-python-docx

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