Python Excel操作模块XlsxWriter之设置行worksheet.set_row()

worksheet.set_row()

set_row(row, height, ceel_format, options)
为一行单元格设置属性。

参数:
  • row(int) - 工作表行(索引从0开始计数)
  • height(float) - 行高
  • cell_format(Format) - 可选的格式对象
  • options(dict) - 可选的行参数:hidden, level, collapsed
set_row()方法用来改变行的默认属性。这个方法最常用的用法是改变行的高度:

worksheet.set_row(0, 20)  # 将行高从1改为20.
set_row()的其他常用用法是为行内的所有单元格设置格式:

cell_format = workbook.add_format({'bold': True})

worksheet.set_row(0, 20, cell_format)
如果你希望在不改变行高的情况下设置格式,你可以传递 None作为height的参数或者使用默认行高15:

worksheet.set_row(1, None, cell_format)
worksheet.set_row(1, 15,   cell_format)  # 与上面的写法等价.
cell_format参数会被应用到行内的所有没有格式的单元格。和Excel一样,它会被显式的单元格格式覆盖。例如:

worksheet.set_row(0, None, format1)      # 行1有format1格式.

worksheet.write('A1', 'Hello')           # 单元格A1默认为format1格式.
worksheet.write('B1', 'Hello', format2)  # 单元格B1变为format2格式.
options参数是可含有以下键的字典:
  • 'hidden'
  • 'level'
  • 'collapsed'
选项可以设置为以下这样:

worksheet.set_row(0, 20, cell_format, {'hidden': True})

# 或者其他属性使用默认值,只设置选项。
worksheet.set_row(0, None, None, {'hidden': True})
'hidden'选项用来隐藏行。这很有用,比如,在复杂的计算中隐藏中间步骤:

worksheet.set_row(0, 20, cell_format, {'hidden': True})
# 不了解这个功能,不好翻译,以下是文档原文
The  'level'  parameter is used to set the outline level of the row.  Adjacent rows with the same outline level are grouped together into a single outline.

以下例子将行的outline level设置为1:

worksheet.set_row(0, None, None, {'level': 1})
worksheet.set_row(1, None, None, {'level': 1})
worksheet.set_row(2, None, None, {'level': 1})
Excel最多允许7 outline levels。 'level' 参数应该在 0 <= level <= 7 范围内。
当与 'level'参数一起使用时, 'hidden'参数也可以用于隐藏collapsed outlined行:

worksheet.set_row(1, None, None, {'hidden': 1, 'level': 1})
worksheet.set_row(2, None, None, {'hidden': 1, 'level': 1})
The  'collapsed'  parameter is used in collapsed outlines to indicate which row has the collapsed  '+'  symbol:

worksheet.set_row(3, None, None, {'collapsed': 1})




  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值