What i am trying is get the excel data content with some color formatting and take screenshot and save the image.
My Business logic will look like this
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 method is having the following code -
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)
If you noticed i am getting the row, column from the user . So i want the coordinates dynamically based on the non empty cells. I dont need to mention the row, column every time . Because the excel files data are dynamic so i need a method to get the total rows and total columns is having only data.
Attached is my sample data, If you see the data here i need the coordinate of 5 rows and 7 columns from a program so that i can pass those data to my "capture_image" method. Instead of passing manually like this
capture_image("REPORT_3_NONCOLOR_TEST.xlsx","ERCD_TREND.jpeg",5,7)
but expected is
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
Now the expected method calling will be like this -
capture_image("REPORT_3_NONCOLOR_TEST.xlsx","ERCD_TREND.jpeg",get_row(excel_file),get_column(excel_file))
Following code solved my problem -
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))
Now i can call the method like this -
capture_image("REPORT_3_NONCOLOR_TEST.xlsx","ERCD_TREND.jpeg",wb.worksheets[0].max_row,wb.worksheets[0].max_column)