Aug-18-2020, 01:12 PM
Hi all,
I am a beginner in Python. I started with using a BarChart and have written below code. While executing, the code is failing with error "AttributeError: 'Cell' object has no attribute 'upper'". The source excel file used to open perfectly fine, however, after the process failure my excel is getting corrupted and couldn't be opened. If I remove and replace another source excel file, it opens fine before execution but gets corrupted post failure. I am keeping the excel in my project location which is "H:\Python\Projects\read_excel" where "read_excel" is my project. While debugging as much I could, the failure is happening while executing "workbook.save(excelname)" statement. Please help.
I am a beginner in Python. I started with using a BarChart and have written below code. While executing, the code is failing with error "AttributeError: 'Cell' object has no attribute 'upper'". The source excel file used to open perfectly fine, however, after the process failure my excel is getting corrupted and couldn't be opened. If I remove and replace another source excel file, it opens fine before execution but gets corrupted post failure. I am keeping the excel in my project location which is "H:\Python\Projects\read_excel" where "read_excel" is my project. While debugging as much I could, the failure is happening while executing "workbook.save(excelname)" statement. Please help.
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
class ProcessExcel:
def doexcel(self, excelname):
workbook = xl.load_workbook(excelname)
sheet = workbook["Sheet1"]
max_row = sheet.max_row
bar_chart_value_range = Reference(sheet, min_col=1, max_col=1, min_row=1, max_row=max_row)
bar_chart = BarChart()
bar_chart.add_data(bar_chart_value_range)
insert_bar_chart_loc = sheet.cell(max_row + 1, 1)
# print("insert_bar_chart_loc : ", insert_bar_chart_loc)
sheet.add_chart(bar_chart, insert_bar_chart_loc)
workbook.save(excelname)
print("Step 8 Saved the BarChart... !")
# cell = sheet.cell(max_row + 1, 1)
# print(cell.value)
excelname = input("Enter the Excel name- ")
processexcel = ProcessExcel()
processexcel.doexcel(excelname)
print("Closing the program...Bye!")
