2016-11-22 191 views
1

在python docx快速入门指南(https://python-docx.readthedocs.io/en/latest/)中,您可以看到可以使用add_run-命令并将粗体文本添加到句子中。如何使用python docx在上标或下标中添加文本

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命令,而是添加上标或下标的文本。

这可能实现吗?

任何帮助非常感谢!

/V

回答

2

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') 

enter image description here

+0

谢谢你的回答快!它正在工作! – viktortl

相关问题