gpt4 book ai didi

Python - 将项目发送到 Word 中的下一页

转载 作者:行者123 更新时间:2023-11-28 03:15:22 25 4
gpt4 key购买 nike

我正在尝试使用 Python 将文本发送到 .docx 文件中的下一页。到目前为止,我已经编写了一些代码来定位 .docx 文件中的某个单词(使用 python-docx):

from docx import *    
document = Document('temp.docx')

for paragraph in document.paragraphs:
if 'Hello' in paragraph.text:
print (paragraph.text)

现在我想将每次出现的“Hello”发送到一个新页面,以便我的 Word 文档中的每个页面都以“Hello”一词开头。

Current Doc: Hello blahblahblahbalh Hello sampletextsampletext

Output:

Hello blahblahblah

New Page

Hello sampletextsampletext

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

这可能看起来不是很漂亮(或不专业),但它完成了工作:

from docx import Document
from docx.enum.text import WD_BREAK
import re

doc = Document('temp.docx')
new_doc = Document()
word = 'Hello'

'''Get the whole document text from all paragraphs'''
whole_text=''
for p in doc.paragraphs:
if whole_text!='':
whole_text+='\n'
whole_text += p.text

'''To split whole_text but include Hello in the created list
put parentheses around the word and use re.split'''
split_p = re.split(word.join('()'),whole_text)

'''Now if 'Hello' was the first word of whole_text re.split will create
['','Hello','...']
if that's the case, remove first '' to avoid unwanted page_break at the
start'''
if split_p[0]=='':
split_p.remove('')

i = 0
while i<len(split_p):
if split_p[i] == word:
'''We don't want to add break at the start of document'''
if len(new_doc.paragraphs)>0:
'''new_doc.add_page_break() creates a newline with page break on it
but the below command will put page break on the last paragraph
so there won't be additional empty lines

If you don't want to have an additional import line
then just put 7 instead of WD_BREAK.PAGE'''
new_doc.paragraphs[-1].add_run().add_break(WD_BREAK.PAGE)
'''Add 'Hello' + the text coming after it'''
new_doc.add_paragraph(split_p[i]+split_p[i+1])
i+=2
else:
'''If the first Hello is not at the start of document just add the
text'''
new_doc.add_paragraph(split_p[i])
i+=1

new_doc.save('hello.docx')

关于Python - 将项目发送到 Word 中的下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45502083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com