如何在python xlwings中查找excel的第一个数据行和列

问题描述 投票:0回答:1

我想在python xlwings中找到excel的第一个数据行和列。以下是示例:

enter image description here

enter image description here

python excel python-3.x row xlwings
1个回答
0
投票

我相信这样做的最安全方法是使用API属性,而只需使用VBA Range.Find方法。现在,我们可以使用Range.Find通配符搜索任何内容,以确保找到公式(可以显示*)和值。简化示例:

""

我们可以使用import xlwings as xw app = xw.App(visible=False, add_book=False) wb = app.books.add() ws = wb.sheets.active ws['C2'].value = 'check' row_cell = ws.api.Cells.Find(What="*", After=ws.api.Cells(1, 1), LookAt=xw.constants.LookAt.xlPart, LookIn=xw.constants.FindLookIn.xlFormulas, SearchOrder=xw.constants.SearchOrder.xlByRows, SearchDirection=xw.constants.SearchDirection.xlNext, MatchCase=False) column_cell = ws.api.Cells.Find(What="*", After=ws.api.Cells(1, 1), LookAt=xw.constants.LookAt.xlPart, LookIn=xw.constants.FindLookIn.xlFormulas, SearchOrder=xw.constants.SearchOrder.xlByColumns, SearchDirection=xw.constants.SearchDirection.xlNext, MatchCase=False) print((row_cell.Row, column_cell.Column)) 并从SearchDirection.xlNext开始。这是Cells(1,1)中的recommended方法,可以可靠地找到您最后使用的行/列,但也可以可靠地找到您的第一行/列。

以上内容只有在VBA为空的情况下才是正确的!

© www.soinside.com 2019 - 2024. All rights reserved.