簡體   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