办公自动化,想对一个docx文件部分段落进行删除,并进行保存。得出结果是另存的文件还是删除前的内容,代码如下。 | python | python 技术论坛-jiuyou九游娱乐官方


you cannot delete a paragraph by following statement because paragraph is a new list created by docx, not the paragraph in the document.
del paragraph[10]
do it by
def delete_paragraph(paragraph):
p = paragraph._element
p.getparent().remove(p)
p._p = p._element = none
if len(paragraph) > 10:
try:
delete_paragraph(paragraph[10])
paragraph = doc.paragraphs
print(f"length after deleted:{len(paragraph)}")
except valueerror as e:
print("error:{e}")
else:
print("the length of paragraph is less than 11 !")
感谢大神给予的帮助!!
请登录
you cannot delete a paragraph by following statement because
paragraphis a new list created by docx, not the paragraph in the document.do it by