如何从 excel sheet 中查找非空数据的行和列坐标?

How to find row and column coordinates from an excel sheet for non-empty data?

我正在尝试的是获取具有某种颜色格式的 excel 数据内容并截取屏幕截图并保存图像。

我的业务逻辑将如下所示

def capture_multiple_images():
    capture_image("REPORT_1_NONCOLOR_TEST.xlsx","RSP_TIME.jpeg",13,12)  #upto 13 rows, 12 columns
    capture_image("REPORT_2_NONCOLOR_TEST.xlsx","FID_REPORT.jpeg",6,10)  #upto 6 rows, 10 columns
    capture_image("REPORT_3_NONCOLOR_TEST.xlsx","ERCD_TREND.jpeg",5,7)  #upto 5 rows, 7 columns

Capture_image 方法具有以下代码 -

def capture_image(EXCEL_FILE,IMAGE_NAME,row,column):
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    workbook = excel.Workbooks.Open(os.path.join(Path.cwd(),EXCEL_FILE))
    ws = workbook.Worksheets['Sheet1']
    ws.Columns.AutoFit()
    ws.Range(ws.Cells(1,1),ws.Cells(row,column)).CopyPicture(Format= win32.constants.xlBitmap)  
    img = ImageGrab.grabclipboard()
    cwd = Path.cwd()
    imgFile = os.path.join(cwd,IMAGE_NAME)
    print(imgFile)
    img.save(imgFile)

如果您注意到我正在从用户那里获取行、列。所以我想要基于非空单元格动态坐标。我不需要每次都提到行,列。因为 excel 文件数据是动态的,所以我需要一种方法来获取总行数和总列数只有数据。

附件是我的示例数据,如果您在此处看到数据,我需要来自程序的 5 行和 7 列的坐标,以便我可以将这些数据传递给我的“capture_image”方法。而不是像这样手动传递

capture_image("REPORT_3_NONCOLOR_TEST.xlsx","ERCD_TREND.jpeg",5,7)

但预期是

def get_row(excel_file):
    ######
    program to get the total number of rows is having non empty data
    return row_number

def get_column(excel_file):
    ######
    program to get the total number of columns is having non empty data
    return column_number

现在预期的方法调用将是这样的-

capture_image("REPORT_3_NONCOLOR_TEST.xlsx","ERCD_TREND.jpeg",get_row(excel_file),get_column(excel_file))

以下代码解决了我的问题-

from openpyxl import load_workbook
wb = load_workbook("REPORT_3_NONCOLOR_TEST.xlsx")

print(wb.worksheets[0].max_row)
print( (wb.worksheets[0].max_column))

现在我可以像这样调用方法了 -

capture_image("REPORT_3_NONCOLOR_TEST.xlsx","ERCD_TREND.jpeg",wb.worksheets[0].max_row,wb.worksheets[0].max_column)