Python Forum
Facing issue while saving workbook - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Facing issue while saving workbook (/thread-29102.html)



Facing issue while saving workbook - Abhisht - Aug-18-2020

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.

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!")



RE: Facing issue while saving workbook - Larz60+ - Aug-18-2020

Quote:While executing, the code is failing with error "AttributeError: 'Cell' object has no attribute 'upper'".
Please, always post complete, unaltered error message in bbcode error tags. There is valuable information contained therein that usually will immediately explain why the error occurred.


RE: Facing issue while saving workbook - Abhisht - Aug-19-2020

Apologies for not submitting the full error message. Below is the same.
Error:
H:\Python\Projects\read_excel\venv\Scripts\python.exe H:/Python/Projects/read_excel/process_excel.py Enter the Excel name- sample_excel.xlsx Traceback (most recent call last): File "H:/Python/Projects/read_excel/process_excel.py", line 30, in <module> processexcel.doexcel(excelname) File "H:/Python/Projects/read_excel/process_excel.py", line 22, in doexcel workbook.save(excelname) File "H:\Python\Projects\read_excel\venv\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save save_workbook(self, filename) File "H:\Python\Projects\read_excel\venv\lib\site-packages\openpyxl\writer\excel.py", line 293, in save_workbook writer.save() File "H:\Python\Projects\read_excel\venv\lib\site-packages\openpyxl\writer\excel.py", line 275, in save self.write_data() File "H:\Python\Projects\read_excel\venv\lib\site-packages\openpyxl\writer\excel.py", line 75, in write_data self._write_worksheets() File "H:\Python\Projects\read_excel\venv\lib\site-packages\openpyxl\writer\excel.py", line 218, in _write_worksheets self._write_drawing(ws._drawing) File "H:\Python\Projects\read_excel\venv\lib\site-packages\openpyxl\writer\excel.py", line 141, in _write_drawing self._archive.writestr(drawing.path[1:], tostring(drawing._write())) File "H:\Python\Projects\read_excel\venv\lib\site-packages\openpyxl\drawing\spreadsheet_drawing.py", line 281, in _write anchor = _check_anchor(obj) File "H:\Python\Projects\read_excel\venv\lib\site-packages\openpyxl\drawing\spreadsheet_drawing.py", line 223, in _check_anchor row, col = coordinate_to_tuple(anchor.upper()) AttributeError: 'Cell' object has no attribute 'upper' Process finished with exit code 1



RE: Facing issue while saving workbook - Larz60+ - Aug-19-2020

save format is: xl.writer.excel.save_workbook(workbook, filename)
see: https://openpyxl.readthedocs.io/en/stable/api/openpyxl.writer.excel.html?highlight=save#openpyxl.writer.excel.save_workbook