python - 使用 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”对象没有属性“_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:")
这是该项目中可用的其他功能之一。
推荐阅读
- javascript - 遍历数组时不断变得未定义
- html - 如何修复 Bootstrap 中的下拉黎明按钮
- angular - 在角度数据表中实现服务器端分页
- javascript - How do I set a value as a string React-Native?
- react-native - 程序在浏览器中显示正常但无法在电子窗口中加载资源
- reactjs - 使用 redux 将对象添加到数组
- xamarin.forms - 从 xamarin 表单中的视图模型导航
- html - 如何使用嵌入
- javascript - 如何从父母更新孩子的(从创建的列表中)道具
- c++ - 使用 Media Foundation 对通过桌面复制获得的 D3D 表面进行编码