我试图找到包含数据的列中的最后一行。替换vba函数:LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
我正在尝试这个,但这会拉入Excel中的所有行。我怎样才能得到最后一行。
from xlwings import Workbook, Range
wb = Workbook()
print len(Range('A:A'))
答案 0 :(得分:3)
可以使用通过api属性公开的VBA Find函数(使用它来查找带有星号的任何内容,并从第一个单元格开始搜索)。
示例:
row_cell = s.api.Cells.Find(What="*",
After=s.api.Cells(1, 1),
LookAt=xlwings.constants.LookAt.xlPart,
LookIn=xlwings.constants.FindLookIn.xlFormulas,
SearchOrder=xlwings.constants.SearchOrder.xlByRows,
SearchDirection=xlwings.constants.SearchDirection.xlPrevious,
MatchCase=False)
column_cell = s.api.Cells.Find(What="*",
After=s.api.Cells(1, 1),
LookAt=xlwings.constants.LookAt.xlPart,
LookIn=xlwings.constants.FindLookIn.xlFormulas,
SearchOrder=xlwings.constants.SearchOrder.xlByColumns,
SearchDirection=xlwings.constants.SearchDirection.xlPrevious,
MatchCase=False)
print((row_cell.Row, column_cell.Column))
此处概述的其他方法似乎不需要数据之间的空行/列。
来源:https://gist.github.com/Elijas/2430813d3ad71aebcc0c83dd1f130e33
答案 1 :(得分:2)
您可以尝试使用Direction
从最底部开始然后向上移动:
import xlwings
from xlwings.constants import Direction
wb = xlwings.Workbook(r'data.xlsx')
print(wb.active_sheet.xl_sheet.Cells(65536, 1).End(Direction.xlUp).Row)
答案 2 :(得分:2)
这与crazymachu的回答非常相似,只是包含在一个函数中。从xlwings的0.9.0版开始,你可以这样做:
import xlwings as xw
def lastRow(idx, workbook, col=1):
""" Find the last row in the worksheet that contains data.
idx: Specifies the worksheet to select. Starts counting from zero.
workbook: Specifies the workbook
col: The column in which to look for the last cell containing data.
"""
ws = workbook.sheets[idx]
lwr_r_cell = ws.cells.last_cell # lower right cell
lwr_row = lwr_r_cell.row # row of the lower right cell
lwr_cell = ws.range((lwr_row, col)) # change to your specified column
if lwr_cell.value is None:
lwr_cell = lwr_cell.end('up') # go up untill you hit a non-empty cell
return lwr_cell.row
直观地,该函数通过在工作簿中找到最极端的右下角单元开始。然后它会移动到您选择的列,然后向上移动直到它到达第一个非空单元格。
答案 3 :(得分:1)
综合上面的答案,您可以一行完成:
wb.sheet.range(column + last cell value).Get End of section going up[non blank assuming the last cell is blank].row
示例代码:
import xlwings as xw
from xlwings import Range, constants
wb = xw.Book(r'path.xlsx')
wb.sheets[0].range('A' + str(wb.sheets[0].cells.last_cell.row)).end('up').row
答案 4 :(得分:0)
我们可以使用Range对象找到最后一行和/或最后一列:
NVARCHAR(1-4000)
答案 5 :(得分:0)
python 3.6,xlwings 0.11
要找到包含数据的最后一行,您应该在水平和垂直方向上做一些工作。您必须遍历每一列以确定哪一行是最后一行。
/modules
我添加了 lastRow 和 lastColumn。为了使程序更有效,您可以根据您正在处理的数据的大致形状来设置这些参数。
xlwings 很荣幸成为 pywin32 的包装器。我不知道您的情况是否允许使用键盘或鼠标。如果是这样,请先按 ctrl+tab 切换到工作簿,然后按 ctrl+a 选择包含数据的区域,然后调用 import xlwings
workbook_all = xlwings.Book(r'path.xlsx')
objectiveSheet = workbook_all .sheets['some_sheet']
# lastCellContainData(), inspired of Stefan's answer.
def lastCellContainData(objectiveSheet,lastRow=None,lastColumn=None):
lastRow = objectiveSheet.cells.last_cell.row if lastRow==None else lastRow
lastColumn = objectiveSheet.cells.last_cell.column if lastColumn==None else lastColumn
lastRows,lastColumns = [],[]
for col in range(1,lastColumn):
lastRows.append(objectiveSheet.range((lastRow, col)).end('up').row)
# extract last row of every column, then max(). Or you can compare the next
# column's last row number to the last column's last row number. Here you get
# the last row with data, you can also go further get the last column with data:
for row in range(1,lastRow):
lastColumns.append(objectiveSheet.range((row, lastColumn)).end('left').column)
return max(lastRows),max(lastColumns)
lastCellContainData(objectiveSheet,lastRow=5000,lastColumn=300)
。
另一种方式:
当您隐约知道数据右下角单元格的位置时,例如 AAA10000,只需调用 workbook_all.selection.rows.count
答案 6 :(得分:-1)
试试这个:
import xlwings as xw
cellsDown = xw.Range('A1').vertical.value
cellsRight = xw.Range('A1').horizontal.value
print len(cellsDown)
print len(cellsRight)