【问题标题】:How to add text in superscript or subscript with python docx如何使用python docx在上标或下标中添加文本
【发布时间】:2017-04-06 10:55:38
【问题描述】:
在 python docx 快速入门指南 (https://python-docx.readthedocs.io/en/latest/) 中,您可以看到可以使用 add_run-command 并将粗体文本添加到句子中。
document = Document()
document.add_heading('Document Title', 0)
p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
我会使用相同的 add_run-command,而是添加带有上标或下标的文本。
这有可能实现吗?
非常感谢任何帮助!
/V
【问题讨论】:
标签:
python
unicode
docx
subscript
superscript
【解决方案1】:
对add_run() 的调用会返回一个Run 对象,您可以使用它来更改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')