有word文档(docx),使用python-docx获取到了某个段落,还需要获取段落所在页的页码
2条回答
关注- 这篇博客: 【python-docx 07】使用word样式中的 使用段落特定的样式属性 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
段落样式具有
next_paragraph_style属性,该属性指定应用在该段落之后插入的新段落的样式。当样式通常只在序列中出现一次(例如标题)时,这非常有用。在这种情况下,完成标题后,段落样式可以自动设置回主体样式。在最常见的情况(正文段落)中,后续段落应该与当前段落具有相同的样式。如果未指定下一个段落样式,则默认情况下通过应用相同样式来处理。
以下是如何将’标题1’样式的下一个段落样式更改为正文文本的示例:
from docx import Document document = Document() styles = document.styles styles['Heading 1'].next_paragraph_style = styles['Body Text']通过指定None或样式本身可以恢复默认:
heading_1_style = styles['Heading 1'] print(heading_1_style.next_paragraph_style.name) # 'Body Text' heading_1_style.next_paragraph_style = heading_1_style print(heading_1_style.next_paragraph_style.name) # 'Heading 1' heading_1_style.next_paragraph_style = None print(heading_1_style.next_paragraph_style.name) # 'Heading 1'
解决评论 打赏 举报无用 1- 这篇博客: 【python-docx 07】使用word样式中的 使用段落特定的样式属性 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读: