使用xlwings和python在单个列中隔离一系列单元格

时间:2018-08-22 06:45:57

标签: python excel python-3.x xlwings

所以我已经很接近解决这个问题了,我已经在这里找到了我需要的大部分内容。为了找到列中的最后一个单元格,我使用了Stefan's solution

我遇到的问题是,我需要的信息部分总是从第16行开始,并且只有最后一个不同的单元格。

这是我到目前为止所拥有的

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 - 1              # go up one more cell to miss out Total*

wb = xw.Book('workbook1')

l1 = LastRow(idx=0, workbook = wb)
l2 = LastRow(idx=1, workbook = wb)

ws1 = wb.sheets('Sheet1')
ws2 = wb.sheets('Sheet2')

ws1.range('A16', 'A52').value

我想用l1代替A52,但是返回一个错误,我不确定如何解决。我想补充一点,这是我第一次尝试python。这是项目的第一步,我会很乐意解决其余的问题。

谢谢

0 个答案:

没有答案