You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用borb库设置PDF表格的行高?

解决borb中FlexibleColumnWidthTable行高设置问题

在FlexibleColumnWidthTable中,单个TableCell的preferred_height参数会被表格的自动布局逻辑忽略,要设置行高可以试试以下两种方法:

方法1:设置TableRow的min_height属性

FlexibleColumnWidthTable的行高由对应的TableRow控制,直接给行设置min_height可以强制行达到指定高度,单个单元格会自动适配行高。

示例代码:

from borb.pdf import FlexibleColumnWidthTable, TableRow, TableCell, Paragraph
from decimal import Decimal

# 初始化2列表格
table = FlexibleColumnWidthTable(number_of_columns=2)

# 创建行并指定最小高度
target_row = TableRow(min_height=Decimal(200))
# 添加单元格,宽度设置依然生效
target_row.add(TableCell(Paragraph("第一列内容"), preferred_width=Decimal(150)))
target_row.add(TableCell(Paragraph("第二列内容")))

table.add(target_row)

方法2:通过内容撑开单元格高度

如果需要单独控制某个单元格的高度,可以给单元格内的内容添加上下内边距,或者嵌套一个固定高度的容器来间接撑开高度。

示例1:给Paragraph添加上下padding

from borb.pdf import TableCell, Paragraph
from decimal import Decimal

# 给段落设置上下内边距,撑开单元格
p = Paragraph("单元格内容", padding_top=Decimal(80), padding_bottom=Decimal(80))
cell = TableCell(p, preferred_width=Decimal(150))

示例2:嵌套FixedColumnWidthTable设置固定高度

from borb.pdf import TableCell, Paragraph, FixedColumnWidthTable
from decimal import Decimal

# 创建内部固定高度表格
inner_table = FixedColumnWidthTable(number_of_columns=1, number_of_rows=1)
inner_table.set_padding_on_all_cells(Decimal(0))
inner_table.add(TableCell(Paragraph("单元格内容")))
inner_table.set_table_height(Decimal(200))

# 将内部表格作为单元格内容
cell = TableCell(inner_table, preferred_width=Decimal(150))

内容的提问来源于stack exchange,提问作者topic2k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 09:55:27