亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從Excel工作表中查找非空數據的行和列坐標?

如何從Excel工作表中查找非空數據的行和列坐標?

慕容3067478 2024-01-16 10:47:54
我正在嘗試的是獲取具有某種顏色格式的 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 columnscapture_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_numberdef 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))
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

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)



查看完整回答
反對 回復 2024-01-16
  • 1 回答
  • 0 關注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號