Streamskill and loading a workbook using openpyxl

Viewed 308

I'm new to the Stackoverflow community and I'm currently trying to figure out how to load a workbook into openpyxl using IDLE 3.9.1.

I'm trying to follow a tutorial that I purchased from Streamskill which was actually pretty good until I ran into this problem and have spent multiple hours trying to figure out where I went wrong.

Thought it had something to do with a file location but it is saved in my Python39 folder. the xlsx file in question is just a blank template from Google Sheets titled "Monthly Budget" I've attached a picture of it if that helps.

Thanks and hope to hear from you soon,

Ben Monthly Budget

>>> import openpyxl
>>> import os
>>> os.getcwd()
'C:\\Users\\Ben\\AppData\\Local\\Programs\\Python\\Python39'
>>> from openpyxl import load_workbook
>>> workbook = load_workbook(filename='budget.xlsx')
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    workbook = load_workbook(filename='budget.xlsx')
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\reader\excel.py", line 317, in load_workbook
    reader.read()
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\reader\excel.py", line 282, in read
    self.read_worksheets()
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\reader\excel.py", line 228, in read_worksheets
    ws_parser.bind_all()
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\worksheet\_reader.py", line 443, in bind_all
    self.bind_merged_cells()
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\worksheet\_reader.py", line 380, in bind_merged_cells
    self.ws._clean_merge_range(mcr)
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\worksheet\worksheet.py", line 607, in _clean_merge_range
    mcr.format()
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\worksheet\merge.py", line 109, in format
    if side.style is None:
AttributeError: 'NoneType' object has no attribute 'style'
2 Answers

The traceback says that openpyxl found the .xlsx file, opened it, and read it. It had a problem in interpreting it to display it. In particular, the variable side has the Python value None, which means that side.style does not exist. Some possibilities:

  1. The file is buggy in having data that openpyxl interprets as None. To check this, try to open the file with Microsoft Excel or some other programs that can read Excel .xlsx files. For PCs, the Microsoft Store has a free XLSX Opener program. https://www.microsoft.com/en-us/p/xlsx-viewer-free/9nblggh6hbf4?activetab=pivot:overviewtab

  2. openpyxl is buggy in interpreting the .xlsx data as None.

  3. openpyxl is buggy in thinking that side must not be None and in either preventing or not catching the AttributeError. You could try changing
    File "C:\Users\Ben\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\worksheet\merge.py", line 109 by adding side is not None or after if to change if side.style is None: to if side is not None or side.style is None:.

(The IDE you use to send code to Python should not affect any of this. To make absolutely sure, enter the same statement in the Python command line shell.)

Thank you to @Terry and @Grijesh I was able to change the version of openpyxl I was using in cmd pip install openpyxl==3.0.4 and follow along the tutorial from Streamskill.

Thanks for your help!

Related