Search code examples
pythonms-wordpython-docx

How to fill a line to match paragraph justification with python-docx


I need to put text in a dull "official formatting" where the entire document must end as single justified paragraph with ALL text but some lines must begins with specifics and bold sentences...

This block of text is an example where "x-x-x-x-x-x" are the filling characters

Is there a way to fill the line until the right-margin? or estimate a printed line size (run) to fill it?


Solution

  • python-docx can't help you with that directly. It has no idea how the text is going to lay out onto a page. That job belongs to a rendering engine, the part of Word or whatever other client (e.g. LibreOffice, Google Docs, etc.) that displays the document on screen or sends it to a printer.

    If you were ambitious you could attempt to approximate the layout using an imaging library like Pillow but I think that exercise would end in frustration (ask me how I know :).