openpyxl error

227
3
Jump to solution
08-04-2022 11:22 AM
RajeshPatnaik
New Contributor II

Hello,

ArcGIS Pro version: 3.0.0

openpyxl version: 3.0.9

I am trying to create an excel and edit it.

import openpyxl
layer = r"F:\Learning\Area.shp"
desc = arcpy.Describe(layer)
path = desc.path
target = path + '/' + 'CA.xlsx'
wb = openpyxl.Workbook(target)
wb.save(target)
ws = wb.active
ws.title = 'MySheet'

It is working fine till this line of code. Then when I try to save it again, it throws an error.

wb.save(target)

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\user\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\site-packages\openpyxl\workbook\workbook.py", line 407, in save
save_workbook(self, filename)
File "C:\Users\user\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\site-packages\openpyxl\writer\excel.py", line 293, in save_workbook
writer.save()
File "C:\Users\user\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\site-packages\openpyxl\writer\excel.py", line 275, in save
self.write_data()
File "C:\Users\user\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\site-packages\openpyxl\writer\excel.py", line 75, in write_data
self._write_worksheets()
File "C:\Users\user\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\site-packages\openpyxl\writer\excel.py", line 215, in _write_worksheets
self.write_worksheet(ws)
File "C:\Users\user\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\site-packages\openpyxl\writer\excel.py", line 203, in write_worksheet
self._archive.write(writer.out, ws.path[1:])
File "C:\Users\user\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\zipfile.py", line 1734, in write
zinfo = ZipInfo.from_file(filename, arcname,
File "C:\Users\user\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\lib\zipfile.py", line 501, in from_file
st = os.stat(filename)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\user\\AppData\\Local\\Temp\\ArcGISProTemp24588\\openpyxl.xhbljeyb'

Why am I getting this error when trying to save a second time??

1 Solution

Accepted Solutions
RajeshPatnaik
New Contributor II

Thanks @dabrooks for your reply

I tried by uninstalling 3.0 and reinstalling 2.9. The issue was not solved.

After lot of search, I came to know that once we save our created workbook, we can not write again to it. So, I did as follows and it works.

import openpyxl
layer = r"F:\Learning\Area.shp"
desc = arcpy.Describe(layer)
path = desc.path
target = path + '/' + 'CA.xlsx'
wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'MySheet'
#write here whatever needed

#save at the end
wb.save(target)

View solution in original post

3 Replies
dabrooks
New Contributor

I had a similar problem working with pandas creating an excel file based on an attribute table. It created the excel file but just running and running. Seeing that you had a such a similar issue makes me want to try without saving.

I solved the issue by uninstalling 3.0 and reinstalling 2.9

RajeshPatnaik
New Contributor II

Thanks @dabrooks for your reply

I tried by uninstalling 3.0 and reinstalling 2.9. The issue was not solved.

After lot of search, I came to know that once we save our created workbook, we can not write again to it. So, I did as follows and it works.

import openpyxl
layer = r"F:\Learning\Area.shp"
desc = arcpy.Describe(layer)
path = desc.path
target = path + '/' + 'CA.xlsx'
wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'MySheet'
#write here whatever needed

#save at the end
wb.save(target)

RachelGomez
New Contributor

The Python "ModuleNotFoundError: No module named 'openpyxl'" occurs when we forget to install the openpyxl module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install openpyxl command.

 

This may help you,

Rachel Gomez

0 Kudos