Issue
My Problem
I'm a newbie Python developper currently developping a small app that writes letters using the python-docx module. As a compulsive user of the justification tool in Word, my 'write_docx' function automatically justify the paragraphs. The whole thing works fine, but at the end the justification is too brutal (some very small lines are way too streched) which make the final *.docx ugly.
Here you can see an exemple of the bad justification (don't mind understanding : it's french)
The weirdest thing about it is that, when I just write the exact same text and justify it directly in Word, there isn't any justification issue (so I guess I'm missing something in the python-docx module).
Here you can see that the justification tool is justifying softly the exact same paragraph
What I tried
I started reading the python-docx documentation (more specifically the section about paragraph style, alignement and indent) and I found out the different alignement options :
paragraph = document.add_paragraph()
paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
or
paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
etc.
Obviously, I was already using the 'WD_ALIGN_PARAGRAPH.JUSTIFY' thing, but I tried some other one, such as :
paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY_MED
paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY_LOW
But none of them worked (it was every time pretty much the same result). So I started looking to indentation and tab stops options (here : https://python-docx.readthedocs.io/en/latest/user/text.html) but nothing was useful in my case either.
Specs
I'm using :
- Python 3.8.3
- python-docx 0.8.10 (that comes with lxml 4.5.1)
- Windows 10.0.14393
- Word 2016
Thanks for your time, I hope I have been clear enough :) (I don't think that adding some code here would be useful in this case).
P.S : Sory if you have any trouble understanding me, I'm not English.
Solution
The answer to my own question
I thought a lot about Dorian's solution which was :
There should be an
if
clause that stops your code from executing if it is the last line of a paragraph
But the main issue was that I couldn't find a way to identify lines inside a paragraph. And the other issue was that my 'paragraph' items sometime contains line break. So even the inside of a paragraph (and not just the last line) could be streched too much by the justification.
The hidden problem
So I figured out that the real problem was in fact that my paragraphs contains line break that weren't officially recognized as line break (at least not the same line break than the one that appears when you press 'enter' in Word) because I was getting them from an *.xml file. Therefore the justification couldn't identify those line break as 'line that shouldn't be justified'.
Once I got there, the solution was pretty easy to find out :
string_from_my_xml_file = get_xml(path,...)
for i in string_from_my_xml.split("\n"):
if i != "":
write_docx(path, i,...)
And my *.xml file looks like this:
<item>This text is on multiple lines:
- One line here
- Another one here
</item>
Answered By - Arthur
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.