Python Forum
Facing issue while saving workbook
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Facing issue while saving workbook
#1
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!")
Reply
#2
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.
Reply
#3
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
Reply
#4
save format is: xl.writer.excel.save_workbook(workbook, filename)
see: https://openpyxl.readthedocs.io/en/stabl...e_workbook
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to copy work sheet data one workbook to other? sayyedkamran 2 691 Nov-03-2023, 09:10 AM
Last Post: Larz60+
  Facing issue in python regex newline match Shr 6 1,271 Oct-25-2023, 09:42 AM
Last Post: Shr
  xlwings error when reading a workbook Mishal0488 1 1,098 Aug-01-2023, 02:05 AM
Last Post: deanhystad
  Facing problem with Pycharm - Not getting the expected output amortal03 1 854 Sep-09-2022, 05:44 PM
Last Post: Yoriz
  Help in opening and editing an excel workbook from a web-browser test 4 1,433 Aug-10-2022, 02:31 PM
Last Post: test
  Facing Problem while opening a file through command prompt vlearner 4 1,899 Jan-30-2022, 08:10 AM
Last Post: snippsat
  Add a new column when I extract each sheet in an Excel workbook as a new csv file shantanu97 0 2,226 Mar-24-2021, 04:56 AM
Last Post: shantanu97
  Facing error while executing below Python code ramu4651 1 5,718 Jan-26-2021, 06:40 PM
Last Post: ibreeden
  I want to create small multiples grouped bar plot, and facing some aesthetics issuess dev_kaur 0 1,610 Dec-05-2020, 08:49 AM
Last Post: dev_kaur
  [split] openpyxl workbook module not callable yosoyebj 1 2,777 Sep-14-2020, 10:18 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020