Xlwings 插入没有格式的新行

Xlwings Insert new row with no formatting

我在 python 中使用 xlwings,我正在尝试插入一个新行,该行不会复制相邻行的格式。目前我有:

ws.range("4:4").api.Insert(InsertShiftDirection.xlShiftToRight)

根据 excel 文档 (https://docs.microsoft.com/en-us/office/vba/api/excel.range.insert) CopyOrigin 的默认选项是 xlFormatFromLeftOrAbove 并且没有不格式化的选项,但是可以使用清除格式方法来实现:

With Range("B2:E5")
.Insert xlShiftDown
.ClearFormats

以上是 VB 代码,但我不确定如何在 python 中使用 ClearFormats 方法,我试过:

ws.range("4:4").api.Insert(InsertShiftDirection.xlShiftToRight.ClearFormats)

和其他添加 ClearFormats 的方法,但无法使其工作。谢谢

使用 xlwings api:

ws = xw.apps.active.books.active.sheets.active
ws.range('8:8').insert(shift='down', copy_origin='format_from_left_or_above')
ws.range('8:8').clear()  # clears content and formatting
  • shift - 'down''right'
  • copy_origin - 'format_from_left_or_above''format_from_right_or_below'

如果您还想使用原生 excel api:

ws = xw.apps.active.books.active.sheets.active
ws.range('I:I').api.Insert(Shift=-4161, CopyOrigin=1) # shift right, format from right or below
ws.range('I:I').api.ClearFormats()

下面Shift and CopyOrigin的相应值:

  • Shift参数
Name            Value
xlShiftDown     -4121
xlShiftToRight  -4161
  • CopyOrigin参数
Name                    Value
xlFormatFromLeftOrAbove     0
xlFormatFromRightOrBelow    1