xlwings 使用 api.autofill 如何将范围作为参数传递给 range.autofill 方法

xlwings using api.autofill how to pass a range as argument for the range.autofill method

我可以使用 VBA 和 range.autofill 方法轻松地用 Excel 上的公式填充一列:

Range("A2").AutoFill Destination:=Range("A2:A10"), Type:=xlFillDefault

这会将单元格 A2(或范围)上的 formula/content 扩展到 A10。

查看 MSDN 帮助我看到:https://msdn.microsoft.com/en-us/library/office/ff195345.aspx

和:https://msdn.microsoft.com/en-us/library/office/ff838605.aspx

在 xlwings 上我可以做:

import xlwings as xw
rp = xw.Book(myFile)
rp.sheets('mySheet').range('A2').api.autofill(range, 0)

可是我不知道怎么传range。我不能简单地键入 "A2:A10",我需要传递一个范围对象。

我尝试这样做:rp.sheets('mySheet').range('A2').api.autofill(rp.sheets('mySheet').range('A2:A10'), 0) 但这简直是自吹自擂 Python!

有什么想法吗?谢谢!

您两次都需要通过 api 使用基础 Range 对象。假设您在 Windows,这将有效:

import xlwings as xw
from xlwings.constants import AutoFillType

wb = xw.Book('Book1')
sheet = wb.sheets(1)
sheet.range('A2').api.AutoFill(sheet.range("A2:A10").api,
                               AutoFillType.xlFillDefault)

这将适用于 Mac:

import xlwings as xw
from xlwings.constants import AutoFillType

wb = xw.Book('euromillions.csv')
ws = wb.sheets('euromillions')

ws.range("A2").api.autofill(destination = ws.range("A2:A5").api, type = AutoFillType.xlFillDefault)