python设置单元格宽度_Python-docx,如何在表格中设置单元格宽度?

使用Python-docx库创建表格时,设置单元格宽度可能遇到Word不响应的情况。解决办法是直接设置每个单元格的宽度。示例代码显示了如何通过遍历单元格并设定宽度实现这一目标。此外,当表格包含合并单元格时,情况会更复杂,Word可能因为合并单元格的不确定性而忽略列宽度。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

How to set cell width in tables?, so far I got:

from docx import Document

from docx.shared import Cm, Inches

document = Document()

table = document.add_table(rows=2, cols=2)

table.style = 'TableGrid' #single lines in all cells

table.autofit = False

col = table.columns[0]

col.width=Inches(0.5)

#col.width=Cm(1.0)

#col.width=360000 #=1cm

document.save('test.docx')

No mater what number or units I set in col.width, its width does not change.

解决方案

Short answer: set cell width individually.

for cell in table_columns[0].cells:

cell.width = Inches(0.5)

python-docx does what you tell it to do when you set column width. The problem is that Word ignores it. Other clients, like LibreO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值