首页 > 解决方案 > Python-docx 很丑的理由

问题描述

我的问题

我是一个新手 Python 开发人员,目前正在开发一个使用 python-docx 模块写信的小应用程序。作为 Word 中对齐工具的强迫用户,我的“write_docx”功能会自动对齐段落。整个事情都很好,但最后的理由太残酷了(一些非常小的线条太拉长了),这使得最终的 *.docx 很难看。

在这里你可以看到一个不好的理由的例子(不要介意理解:它是法语)

最奇怪的是,当我只写完全相同的文本并直接在 Word 中证明它时,没有任何证明问题(所以我想我在 python-docx 模块中遗漏了一些东西)。

在这里,您可以看到对齐工具正在轻轻地对齐完全相同的段落

我试过的

我开始阅读 python-docx 文档(更具体地说是关于段落样式、对齐和缩进的部分),我发现了不同的对齐选项:

paragraph = document.add_paragraph()
paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT

或者

paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY

等等

显然,我已经在使用 'WD_ALIGN_PARAGRAPH.JUSTIFY' 东西,但我尝试了其他一些,例如:

paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY_MED
paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY_LOW

但他们都没有工作(每次都是几乎相同的结果)。所以我开始寻找缩进和制表位选项(这里:https ://python-docx.readthedocs.io/en/latest/user/text.html ),但在我的情况下也没有任何用处。

眼镜

我在用着 :

感谢您的时间,我希望我已经足够清楚:)(我认为在这种情况下添加一些代码不会有用)。

PS:对不起,如果你理解我有任何问题,我不是英语。

标签: pythonpython-3.xdocxpython-docx

解决方案


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>

推荐阅读