Search code examples
pythonpython-docx

python-docx - deleting first paragraph


When I create a new document with python-docx and add paragraphs, it starts at the very first line. But if I use an empty document (I need it because of the user defined styles) and add paragraphes the document would always start with an empty line. Is there any workaround?


Solution

  • You can call document._body.clear_content() before adding the first paragraph.

    document = Document('my-document.docx')
    document._body.clear_content()
    # start adding new paragraphs and whatever ...
    

    That will leave the document with no paragraphs, so when you add new ones they start at the beginning.

    It does, however, leave the document in a technically invalid state. So if you didn't add any new paragraphs and then tried to open it with Word, you might get a repair error on loading.

    But if the next thing you're doing is adding paragraphs of your own, this should work just fine.

    Also, note that this is technically an "internal" method and is not part of the documented API. So there's no guarantee this method's name won't change in a future release. But frankly I can't see any reason to change or remove it, so I expect it's safe enough :)