如何使用python docx在上标或下标中添加文本

vik*_*rtl 3 python unicode docx superscript subscript

在 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
Run Code Online (Sandbox Code Playgroud)

我会使用相同的 add_run-command 而是添加带有上标或下标的文本。

这有可能实现吗?

非常感谢任何帮助!

/V

Wil*_*son 5

调用add_run()返回一个Run可用于更改字体选项的对象

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')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明