繁体   English   中英

docx-python word doc 分页符

[英]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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM