docx-python word文档分页符

docx-python word doc page break

我正在尝试使用 docx-python 库在文档中间添加分页符。

似乎在添加分页符时,分页符被添加到文档的末尾。有没有在特定位置添加分页符的方法?

这是我当前的代码。

from docx import Document
from docx.shared import Inches

demo='gm.docx'
document = Document(docx=demo)

for paragraph in document.paragraphs:
    if 'PUB' in paragraph.text:
        document.add_page_break()

document.save('gm.docx')

Run级别出现了各种形式的中断:
http://python-docx.readthedocs.io/en/latest/api/text.html#run-objects

所以像这样的东西应该可以解决问题:

from docx.enum.text import WD_BREAK

for paragraph in document.paragraphs:
    if 'PUB' in paragraph.text:
        run = paragraph.add_run()
        run.add_break(WD_BREAK.PAGE)