gpt4 book ai didi

python - 将列添加到表的左侧 - Python docx

转载 作者:行者123 更新时间:2023-12-01 08:49:13 29 4
gpt4 key购买 nike

我在各个论坛上进行了多次搜索,但根本找不到答案。我在 docx 文件中有一个表,想使用 docx Python 模块来修改它。我需要在表格左侧添加一列。根据文档,使用 add_column() 函数将一列添加到表的右侧。我还尝试使用以下代码将表的方向性更改为 RTL 表:

import docx
from docx.enum.table import WD_TABLE_DIRECTION

file = test.docx
doc = docx.Document(file)
tbls = doc.tables #this gives me 3 tables in a list of table objects
test = tbls[1]
test.table_direction = WD_TABLE_DIRECTION.RTL
test.add_column(1)
doc.save(file)

打开生成的文件后,我发现代码仍然仅在左侧添加一列。有人知道如何在表格右侧添加一列吗?

非常感谢!

最佳答案

您可以尝试LTR,也可以使用Inches来定义列宽,以便添加的列能够正确显示。

import docx
from docx.enum.table import WD_TABLE_DIRECTION
from docx.shared import Cm, Inches

file = 'test.docx'
doc = docx.Document(file)
tbls = doc.tables
test = tbls[1]
test.table_direction = WD_TABLE_DIRECTION.LTR
test.add_column(Inches(1.0))
doc.save(file)

关于python - 将列添加到表的左侧 - Python docx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53197716/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com