Home > other >  Using the python - docx delete the blank line of the word document
Using the python - docx delete the blank line of the word document

Time:09-17

How to use the python - docx delete the blank line of the word document (line is only a newline), I said is deleted in the original document to delete, not take out a text line, generating a new document, because the original document with the python - docx can't handle content, will remain the same,

CodePudding user response:

 
The from docx import Document
Def delete_paragraph (com.lowagie.text.paragraph) :
P=com.lowagie.text.paragraph. _element
P.g etparent (). Remove (p)
P. _p=p. _element=None
# create a blank document, and set up the style
MyDocument=Document (' test1. Docx)
# set a blank style
For num, paragraphs in enumerate (myDocument. Paragraphs) :
If paragraphs. The text=="" :
Print (' first {} is a blank line section. The format (num))
# remove text paragraphs. The clear (), does not delete the paragraph, the run can also,
Delete_paragraph (paragraphs)
MyDocument. Save (' test2. Docx)


Online to find the answer, is like this

CodePudding user response:

Wrong delete the situation, the only figure in the line without words, has been deleted
  • Related