我正在嘗試的是獲取具有某種顏色格式的 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)
添加回答
舉報
0/150
提交
取消