使用python-docx编辑一个docx文档时, 对添加的方法使用很多, 但有时候也会用到删除和修改
python-docx中并没有提供delete()方法, github上给出了解决方法:
https://github.com/python-openxml/python-docx/issues/33
1 2 3 4 5 | def delete_paragraph(paragraph): p = paragraph._element p.getparent().remove(p) # p._p = p._element = None paragraph._p = paragraph._element = None |
经试验, 此方法对删除段落,表格,标题, 图片都是管用的:
1 2 3 4 5 6 7 8 9 10 11 12 13 | from docx import Document docx = Document('word_file.docx') def delete_docx_prefix_description(docx): delete_paragraph(docx.tables[0]) # 删除word中第一个table for p in docx.paragraphs: delete_paragraph(p) if ''.join(p.text.split(' ')).lower()=='header_keyword': break for p in docx.paragraphs: if p.text.lower()=='': # 删除word中在开始部分的空白段落 delete_paragraph(p) else: break |