我想使用包python-docx将样式应用到我的段落(列表、列表编号、列表编号2…等等)。我创建了我自己的msword模板,其中有一些我制作的特定样式。有时我还需要应用MS Word内置样式。我的代码是:elif to_U_sc:
to_write = to_U_sc.group(2)
paragraph = self.template.add_paragraph(to_write, style='List')
elif to_O_sc2:
to_write = to_O_sc2.group(2)
paragraph = self.template.add_paragraph(to_write, style='ListNumber')
这里,self.template是Document对象。无论使用哪种风格,都是错误的。比如:
^{pr2}$
我尝试使用代码打印模板中的所有样式:def all_styles(self):
styles = self._document.styles
for style in styles:
print('Name: {}\tType:{}'.format(style.name, style.type))
结果如下:Name: List Paragraph Type:PARAGRAPH (1)
Name: 浅色底纹 - 强调文字颜色 22 Type:TABLE (3)
Name: Light Shading Accent 2 Type:TABLE (3)
Name: 项目 Type:PARAGRAPH (1)
Name: 大结构 Type:PARAGRAPH (1)