简体   繁体   English

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

[英]How to find first data row and column of excel in python xlwings

I want to find first data row and column of excel in python xlwings.我想在python xlwings中找到excel的第一个数据行和列。 Below are examples:以下是示例:

在此处输入图片说明

Or或者

在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM