docx-python word doc分页符

时间:2018-02-07 01:43:18

标签: 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')

1 个答案:

答案 0 :(得分:2)

各种形式的中断显示在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)