使用python-docx編輯一個docx文檔時, 對添加的方法使用很多, 但有時候也會用到刪除和修改
python-docx中並沒有提供delete()方法, github上給出瞭解決方法:
https://github.com/python-openxml/python-docx/issues/33
def delete_paragraph(paragraph):
p = paragraph._element
p.getparent().remove(p)
# p._p = p._element = None
paragraph._p = paragraph._element = None
經試驗, 此方法對刪除段落,表格,標題, 圖片都是管用的:
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