PythonDocx-如何对标题进行编号?

PythonDocx-如何对标题进行编号?,python,python-docx,Python,Python Docx,对于pythondocx有一个很好的解决方案 我使用了多个文档。添加标题('xxx',级别=Y),当我用MS Word打开生成的文档时,可以看到级别是正确的 我没有看到的是编号,比如1,1.1,1.1.1等等,我只看到标题文本 如何使用Docx显示标题编号?字母数字标题前缀会根据标题的大纲样式和级别自动创建。设置轮廓样式并插入正确的标高,您将获得编号 编号样式尚未实现。从文件: _编号样式对象 类docx.styles.style.\u NumberingStyle[来源] 编号方式。尚未实施

对于pythondocx有一个很好的解决方案

我使用了多个
文档。添加标题('xxx',级别=Y)
,当我用MS Word打开生成的文档时,可以看到级别是正确的

我没有看到的是编号,比如1,1.1,1.1.1等等,我只看到标题文本


如何使用Docx显示标题编号?

字母数字标题前缀会根据标题的大纲样式和级别自动创建。设置轮廓样式并插入正确的标高,您将获得编号

编号样式尚未实现。从文件: _编号样式对象 类docx.styles.style.\u NumberingStyle[来源] 编号方式。尚未实施


但是,如果设置了标题(例如,段落.style=document.style['Heading 1']),则应默认为该标题的潜在编号样式。

字母数字标题前缀将根据标题的大纲样式和级别自动创建。设置轮廓样式并插入正确的标高,您将获得编号

编号样式尚未实现。从文件: _编号样式对象 类docx.styles.style.\u NumberingStyle[来源] 编号方式。尚未实施


但是,如果设置标题(例如,段落.style=document.style['Heading 1']),则应默认为该标题的潜在编号样式。

此答案将真正帮助您

首先,您需要创建一个新的无编号标题,如下所示

paragraph = document.add_paragraph()
paragraph.style = document.styles['Heading 4']
<w:pPr>
<w:pStyle w:val="4"/>
</w:pPr>
然后您将有如下xml单词

paragraph = document.add_paragraph()
paragraph.style = document.styles['Heading 4']
<w:pPr>
<w:pStyle w:val="4"/>
</w:pPr>

最后,打开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        
首先,您需要创建一个新的无编号标题,如下所示

paragraph = document.add_paragraph()
paragraph.style = document.styles['Heading 4']
<w:pPr>
<w:pStyle w:val="4"/>
</w:pPr>
然后您将有如下xml单词

paragraph = document.add_paragraph()
paragraph.style = document.styles['Heading 4']
<w:pPr>
<w:pStyle w:val="4"/>
</w:pPr>

最后,打开word文件,你将得到你想要的

我编写了这段代码,试图按照你说的做,但我无法设置pSyle,因为它没有。从docx import Document Document=Document()Document.add_heading(“Hi!”,level=1)element=Document.styles[“heading 1”]。element print(element.pPr.pStyle)#没有我让这段代码试图按照你说的做,但我无法设置pSyle,因为没有。来自docx import Document Document=Document()Document.add_heading(“Hi!”,level=1)element=Document.styles[“heading 1”]。element print(element.pPr.pStyle)#虽然这段代码可能解决了这个问题,但它如何以及为什么解决这个问题会真正有助于提高帖子的质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。虽然此代码可能会解决此问题,但如何以及为什么解决此问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
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