【问题标题】:docx-python word doc page breakdocx-python word 文档分页符
【发布时间】:2018-02-07 01:43:18
【问题描述】:

我正在尝试使用 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')

【问题讨论】:

    标签: python split python-docx


    【解决方案1】:

    它们的各种形式的中断出现在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)
    

    【讨论】:

      猜你喜欢
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 2015-07-01
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多