使用python文档拆分



我希望修改当前使用python-docx将文本从.txt文件导入到.docx文件的特定部分的程序。目前我使用find_replace功能。我在github上发现了一个很棒的项目,但我很难弄清楚我到目前为止做错了什么。项目如下:

https://github.com/alllexx88/python-docx-split-run

这是我写的:

def insert_run_after(par, run, txt=''):
"""Insert a new run with text {txt} into paragraph after given {run}.
Returns the newly created run.
"""
run_2 = par.add_run(txt)
run._r.addnext(run_2._r)
return run_2
document = Document('Psychevaltemplate2.docx')
par = document.paragraphs[0]
run = par.runs[0]
background = input("what is the location of the background file?")
input_doc = Document(background)
insert_run_after(par, 5, 'TEST RESULTS:')
output_doc.save("sampleoutput2.docx")
exit()

,这里是错误:

run._r.addnext (run_2._r)

AttributeError: 'int' object没有属性'_r'

任何帮助都将是非常感激的。

insert_run_after(par, 5, 'TEST RESULTS:')行中,您将5作为run参数传递。

也许你是这个意思?

insert_run_after(par, par.runs[5], "TEST RESULTS:")

或:

insert_run_at_position(par, 5, "TEST RESULTS:")

,它是该项目中可用的其他功能之一。

最新更新