Python Docx表行高 [英] Python Docx Table row height

查看:1489
本文介绍了Python Docx表行高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,使用一列中所有单元格的单元格宽度来完成列宽,例如:

So column width is done using cell width on all cells in one column ike this:

from docx import Document
from docx.shared import Cm

file = /path/to/file/
doc = Document(file)
table = doc.add_table(4,2)
for cell in table.columns[0].cells:
    cell.width = Cm(1.85)

但是,行高是使用行来完成的,但是我不记得上周是如何做到的.

however, the row height is done using rows, but I can't remember how I did it last week.

现在,我设法找到一种方法来引用表中的行,但似乎无法回到这种状态.可以使用add_row方法更改高度,但是您不能创建没有行的表,因此第一行始终是默认高度,大约为1.6cms.

Now I managed to find a way to reference the rows in a table, but can't seem to get back to that way. It is possible to change the height by using the add_row method, but you can't create a table with no rows, so the the top row will always be the default height, which about 1.6cms.

有一种无需使用add_paragraph即可访问段落的方法,有谁知道不使用add_row方法即可访问行的原因,因为那是我以前将表中的行高设置为默认值.

There is a way to access paragraphs without using add_paragraph, does anyone know how to access the rows without using the add_row method because it was that that I used to set row height in a table as a default.

我已经尝试过了,但是不起作用:

I have tried this but it doesn't work:

row = table.rows
row.height = Cm(0.7)

但是尽管这不会产生错误,但对高度也没有影响.

but although this does not give an error, it also has no effect on the height.

推荐答案

table.rows是一个集合,尤其是一个序列,因此您需要分别访问每一行:

table.rows is a collection, in particular a sequence, so you need to access each row separately:

for row in table.rows:
    row.height = Cm(0.7)

也请检出row.height_rule有关您可以访问的一些相关行为:
https://python-docx.readthedocs.io/zh_CN/latest/api/table.html#row-objects

Also check out row.height_rule for some related behaviors you have access to:
https://python-docx.readthedocs.io/en/latest/api/table.html#row-objects

当您分配给table.rows.height时,它只会添加一个不执行任何操作的.height实例属性.这是诸如Python之类的动态语言的副作用,您会遇到类似的神秘行为.但是,随着您获得更多经验,它消失了,至少对我来说是这样的:)

When you assign to table.rows.height, it just adds a .height instance attribute that does nothing. It's one of the side-effects of a dynamic language like Python that you encounter a mysterious behavior like this. It goes away as you gain more experience though, at least it has for me :)

这篇关于Python Docx表行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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