spire.xls for Python produces AttributeError: 'NoneType' object has no attribute 'Workbook_Dispose'

1.5k Views Asked by At

I am trying to write a simple program in Python that saves an excel file to a pdf. I am using code I found with a google search. I ran

pip install Spire.XLS-for-Python

and

pip install plum-dispatch==2.1.1

which was successful . This is the code which I copied wholesale from the internet, changing only the filenames:

from spire.xls import *
from spire.common import *


inputFile = "/Users/myname/Desktop/XYZ.xlsm"
outputFile = "/Users/myname/Library/Mobile      Documents/com~apple~CloudDocs/Development/CellRangeToPDF.pdf"

#create a workbook
workbook = Workbook()
#load a excel document
workbook.LoadFromFile(inputFile)
workbook.ConverterSetting.SheetFitToPage = True
#convert to PDF file
workbook.SaveToFile(outputFile, FileFormat.pdf)
workbook.Dispose()

It produces the following error (I don't include the full traceback but can if needed of course):

AttributeError: 'NoneType' object has no attribute 'Workbook_Dispose'

I couldn't find a satisfying answer in any of the usual forums

I am using Python 3.11 and PyCharm 2023.2. I reinstalled both Python and PyCharm to no avail.

I am new at this and have limited Python coding experience so please bear with me

Any advice is extremely welcome

3

There are 3 best solutions below

1
Dheeraj Malik On

Change "workbook.SaveToFile(outputFile, FileFormat.pdf)" to "workbook.SaveToFile(outputFile, FileFormat.PDF)".

0
Huguito On

I've got the same error and this is because it's not yet impletemented for Linux distribution (source : https://www.e-iceblue.com/forum/docker-linux-python-workbook-instantiation-failing-t12450.html?sid=0fa267b6af5d02b2fbdbf013e1e50a4c)

0
Luis Fernando Cetina Montejo On

They already updated,

Try this

pip install Spire.Xls

this worked for me