我正在尝试查找包含数据的列中的最后一行。替换 vba 功能:LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
我正在尝试这个,但这会拉入 Excel 中的所有行。我怎样才能得到最后一行。
from xlwings import Workbook, Range
wb = Workbook()
print len(Range('A:A'))
我正在尝试查找包含数据的列中的最后一行。替换 vba 功能:LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
我正在尝试这个,但这会拉入 Excel 中的所有行。我怎样才能得到最后一行。
from xlwings import Workbook, Range
wb = Workbook()
print len(Range('A:A'))
合并上面的答案,您可以在一行中完成:
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
这与 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
直观地说,该函数首先找到工作簿中最右下角的单元格。然后它移动到您选择的列,然后向上移动,直到它到达第一个非空单元格。
我们可以使用 Range 对象来查找最后一行和/或最后一列:
import xlwings as xw
# open raw data file
filename_read = 'data_raw.csv'
wb = xw.Book(filename_read)
sht = wb.sheets[0]
# find the numbers of columns and rows in the sheet
num_col = sht.range('A1').end('right').column
num_row = sht.range('A1').end('down').row
# collect data
content_list = sht.range((1,1),(num_row,num_col)).value
print(content_list)
您可以尝试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)
尝试这个:
import xlwings as xw
cellsDown = xw.Range('A1').vertical.value
cellsRight = xw.Range('A1').horizontal.value
print len(cellsDown)
print len(cellsRight)
可以使用通过 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
蟒蛇 3.6,xlwings 0.11
要找到数据的最后一行,您应该在水平和垂直方向上做一些工作。您必须遍历每一列以确定哪一行是最后一行。
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)
我添加了 lastRow 和 lastColumn。为了使程序更有效,您可以根据您正在处理的数据的大致形状设置这些参数。
xlwings 很荣幸成为 pywin32 的包装器。我不知道您的情况是否允许使用键盘或鼠标。如果是这样,首先你 ctrl+tab 切换到工作簿,然后 ctrl+a 选择包含数据的区域,然后调用workbook_all.selection.rows.count
.
另一种方式:当您隐约知道数据右下角单元格的位置时,例如 AAA10000,只需调用objectiveSheet.range('A1:'+'AAA10000').current_region.rows.count