docx-python word doc 分页符

Rob*_*ley 6 python split python-docx

我正在尝试使用 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 Code Online (Sandbox Code Playgroud)

sca*_*nny 10

它们的各种形式的中断出现在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)
Run Code Online (Sandbox Code Playgroud)