我有一个只包含文本的Docx文件,我想用python Docx修改它。
我想删除空行和具有特定样式的行。
以下是我尝试过的:
import docx
doc = docx.Document('sample.docx')
lines = doc.paragraphs
for line in lines:
#delete empty lines
if len(line.text) == 0:
lines.remove(line)
continue
#delete line if it has this specifc style
if line.runs[0].font.name == 'Formata-Regular' and line.runs[0].font.size.pt == float(8.0):
line.clear()
doc.save('output.docx')
line.clear()
工作-它只删除行的内容,但不删除它。
lines.remove(line)
什么都不做。
所以我找到了这段代码(由scanny在这里编写(,它完成了
def delete_paragraph(paragraph):
p = paragraph._element
p.getparent().remove(p)
p._p = p._element = None
以下是我的代码现在的样子:
import docx
def delete_paragraph(paragraph):
p = paragraph._element
p.getparent().remove(p)
p._p = p._element = None
doc = docx.Document('sample.docx')
lines = doc.paragraphs
for line in lines:
#delete empty lines
if len(line.text) == 0:
delete_paragraph(line)
continue
#delete line if it has this specifc style
if line.runs[0].font.name == 'Formata-Regular' and line.runs[0].font.size.pt == float(8.0):
delete_paragraph(line)
doc.save('output.docx')
所有空行(或具有特定样式的行(都将被删除。