python docx在同一行上左右对齐 [英] python docx align both left and right on same line

查看:1081
本文介绍了python docx在同一行上左右对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我关于SO的第一个问题,在此先感谢大家的帮助。一般来说,我对python,python-docx和编程尚不熟悉。我正在使用GUI程序(使用PyQt)生成docx格式的合同。我的大部分工作都在工作,但这是我遇到的问题。我需要在同一行上左右对齐文本。换句话说,我相信这是通过更改为右缩进并单击选项卡,然后添加文本来完成的。但是,我无法弄清楚如何在python-docx中执行此操作。我尝试过:

This is my first question on SO and would like to thank you all in advance for any help. I'm pretty new to python, python-docx and programming in general. I am working on a GUI program (using PyQt) to generate a contract in docx format. I have most things working, but here is the problem I am having. I need to align text both left and right on the same line. In word, I believe this is done by changing to a right indent and hitting tab, then adding the text. However, I cannot figure out how to do this in python-docx. I tried:

paragraph = document.add_paragraph()
paragraph.add_run('SOME TEXT')
paragraph.alignment = 0
paragraph.add_run('SOME OTHER TEXT')
paragraph.alignment = 2

但这没用。我在文档中尝试了其他一些想法,例如WD_PARAGRAPH_ALIGNMENT枚举,但是没有用。

but this didn't work. I tried some other ideas per the documentation, like enum WD_PARAGRAPH_ALIGNMENT, but nothing worked.

在python-docx中可以做到这一点吗(使用0.8.5版)?

Is this possible to do in python-docx (im using version 0.8.5)?

感谢您的帮助!

推荐答案

不确定您是否仍然在寻找解决方案,但是...

Not sure if you're still looking for a solution, but...

我还与python-docx签订了合同,并且遇到了同样的问题。在添加右对齐制表位停止功能之前,我的解决方法是

I am also generating contracts with python-docx and came across this same issue. Until the right-aligned tab stop feature is added, my workaround is to format the line as a table, using a custom table style.


  • 首先,打开一个文档('custom_styles.docx')并添加一个带有1行和2列的表。

  • 选择表并添加新的表样式( CustomTable),其中第一列左对齐,最后一列右对齐。 (我还通过将边框设置为无或将其着色为白色来使边框不可见。)

  • 删除表格并保存文档,这将为您提供带有新的'

下面的方法应该用于创建看起来左对齐和右对齐的行:

The following should work for creating a line that appears to be both left-aligned and right-aligned:

from docx import Document

document = Document('custom_styles.docx')
table = document.add_table(1, 2, style='CustomTable')
table.cell(0,0).text = 'Left Text'
table.cell(0,1).text = 'Right Text'
document.save('new_document_name.docx')

对我来说最棘手的部分在弄清楚如何在Word中创建表格样式。

The trickiest part for me was figuring out how to create the table style in Word.

这篇关于python docx在同一行上左右对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆