【发布时间】:2018-12-20 14:19:29
【问题描述】:
Python Docx 有一个很好的example。
我使用了多个document.add_heading('xxx', level=Y),当我在MS Word中打开生成的文档时,可以看到级别是正确的。
我看不到的是编号,比如 1、1.1、1.1.1 等,我只看到标题文本。
如何使用 Docx 显示标题编号?
【问题讨论】:
标签: python python-docx
Python Docx 有一个很好的example。
我使用了多个document.add_heading('xxx', level=Y),当我在MS Word中打开生成的文档时,可以看到级别是正确的。
我看不到的是编号,比如 1、1.1、1.1.1 等,我只看到标题文本。
如何使用 Docx 显示标题编号?
【问题讨论】:
标签: python python-docx
字母数字标题前缀是根据标题的大纲样式和级别自动创建的。设置轮廓样式并插入正确的层级即可得到编号。
来自文档:
_NumberingStyle 对象类 docx.styles.style._NumberingStyle[source] 编号样式。还没有 实施。
但是,如果你这样设置标题:
paragraph.style = document.styles['Heading 1']
那么它应该默认为该标题的潜在编号样式。
【讨论】:
这个答案对你有帮助
首先你需要像这样新建一个没有数字的标题
paragraph = document.add_paragraph()
paragraph.style = document.styles['Heading 4']
那么你就会有这样的xml字
<w:pPr>
<w:pStyle w:val="4"/>
</w:pPr>
然后您可以访问 xml word "pStyle" 属性并使用代码更改它
header._p.pPr.pStyle.set(qn('w:val'), u'4FDD')
最后,打开word文件你会得到你想要的!!!
【讨论】:
def __str__(self):
if self.nivel == 1:
return str(Level.count_1)+'.- '+self.titulo
elif self.nivel==2: #Imprime si es del nivel 2
return str(Level.count_1)+'.'+str(Level.count_2)+'.- '+self.titulo
elif self.nivel==3: #Imprime si es del nivel 3
return str(Level.count_1)+'.'+str(Level.count_2)+'.'+str(Level.count_3)+'.- '+self.titulo
【讨论】: