Python 如何";插入“运行”;而不是",;添加“运行”;到段落末尾

Python 如何";插入“运行”;而不是",;添加“运行”;到段落末尾,python,python-docx,Python,Python Docx,我是PythonDocx新手,发现段落.add_run()总是在段落末尾添加文本。但我需要做的是在段落中插入一个句子。更具体地说: 我有一个如下所示的文档文件: 我想让它看起来像这样: 谢谢 在段落上没有.insert_run()方法,如果您仔细考虑一下,它可能不足以完成此任务,因为无法保证每个句子都在运行边界上结束。如果你想要的话,你需要自己做句子分析 第一个简单的实现可能如下所示: >>> paragraph = document.paragraphs[2] >&

我是PythonDocx新手,发现
段落.add_run()
总是在段落末尾添加文本。但我需要做的是在段落中插入一个句子。更具体地说:

我有一个如下所示的文档文件:

我想让它看起来像这样:


谢谢

段落
上没有
.insert_run()
方法,如果您仔细考虑一下,它可能不足以完成此任务,因为无法保证每个句子都在运行边界上结束。如果你想要的话,你需要自己做句子分析

第一个简单的实现可能如下所示:

>>> paragraph = document.paragraphs[2]
>>> paragraph.text
"This is the first sentence. This is the second sentence."
>>> sentences = paragraph.text.split(". ")
>>> sentences
["This is the first sentence", "This is the second sentence."]
>>> sentences.insert(1, "And I insert a sentence here")
>>> paragraph.text = ". ".join(sentences)
>>> paragraph.text
"This is the first sentence. And I insert a sentence here. This is the second sentence."