[英]How to find first data row and column of excel in python xlwings
I believe the safest to do this would be to use API
property and simply use VBA
Range.Find
method.我相信最安全的方法是使用API
属性并简单地使用VBA
Range.Find
方法。 We can now search for anything using a *
wildcard to make sure you find formulas (that could show ""
) AND values.我们现在可以使用*
通配符搜索任何内容,以确保找到公式(可以显示""
)和值。 A simplified example:一个简化的例子:
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(ws.api.Rows.Count, ws.api.Columns.Count), 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(ws.api.Rows.Count, ws.api.Columns.Count), 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))
We can use SearchDirection.xlNext
and start in the very last Excel
cell.我们可以使用SearchDirection.xlNext
并从最后一个Excel
单元格开始。 This is a recommended way in VBA
to reliably find your last used row/column, but can also reliably find your first row/column.这是VBA
推荐的一种可靠地找到上次使用的行/列的方法,但也可以可靠地找到第一行/列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.