Search code examples
pythonpython-docx

python-docx add horizontal line


Is it possible to add a single horizontal line spanning the entire page width after a paragraph?

By using the add_header such a line is created after the header:

  document.add_heading('Test', 0)

I'd need such a line between two paragraphs in the document, so is it possible to just add the line by itself?


Solution

  • This is not supported directly yet by the python-docx API.

    However, you can achieve this effect by creating a paragraph style that has this border setting in the "template" document you open to create a new document, e.g.

    document = Document('template-with-style.docx')
    

    Then you can apply that style to a new paragraph in that document using python-docx.