我在 python 中使用 xlwings,我試圖插入一個不從相鄰行復制格式的新行。目前我有:
ws.range("4:4").api.Insert(InsertShiftDirection.xlShiftToRight)
根據 excel 檔案 ( https://docs.microsoft.com/en-us/office/vba/api/excel.range.insert ) CopyOrigin 的 dafault 選項是 xlFormatFromLeftOrAbove 并且沒有不格式化的選項,但它可以使用清除格式方法實作:
With Range("B2:E5")
.Insert xlShiftDown
.ClearFormats
以上是VB代碼,但我不確定如何在python中使用ClearFormats方法,我嘗試過:
ws.range("4:4").api.Insert(InsertShiftDirection.xlShiftToRight.ClearFormats)
和其他添加 ClearFormats 但無法使其作業的方法。謝謝
uj5u.com熱心網友回復:
使用 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
- 轉移 -
'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和CopyOrigin以下的相應值:
Shift引數
Name Value
xlShiftDown -4121
xlShiftToRight -4161
CopyOrigin引數
Name Value
xlFormatFromLeftOrAbove 0
xlFormatFromRightOrBelow 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/384776.html
上一篇:基于兩列條件插入新行的vba代碼
