ValueError: I/O Operation on closed file on workbook.save(<filename>)

Issue #1164 closed
Divij Sehgal
created an issue

Appending result to intermediate file: /home/divij/Desktop/Divij/Personal/Email_finder_and_sender/Email_finder_and_sender/12_AU_BETA.xlsx Image File to include: /home/divij/Desktop/Divij/Personal/Email_finder_and_sender/Email_finder_and_sender/Prints/Dentist Sydney-data.png

Traceback (most recent call last):
  File "email-finder-refactored_beta.py", line 700, in <module>
    apppendIntermediateResultsToFile(filesPath["excel"], local_output, googleSearch.printsFolder)
  File "email-finder-refactored_beta.py", line 536, in apppendIntermediateResultsToFile
    workbook.save(filePath)
  File "/home/divij/Desktop/Divij/Personal/venv/lib/python3.6/site-packages/openpyxl/workbook/workbook.py", line 391, in save
    save_workbook(self, filename)
  File "/home/divij/Desktop/Divij/Personal/venv/lib/python3.6/site-packages/openpyxl/writer/excel.py", line 284, in save_workbook
    writer.save(filename)
  File "/home/divij/Desktop/Divij/Personal/venv/lib/python3.6/site-packages/openpyxl/writer/excel.py", line 266, in save
    self.write_data()
  File "/home/divij/Desktop/Divij/Personal/venv/lib/python3.6/site-packages/openpyxl/writer/excel.py", line 85, in write_data
    self._write_images()
  File "/home/divij/Desktop/Divij/Personal/venv/lib/python3.6/site-packages/openpyxl/writer/excel.py", line 122, in _write_images
    self._archive.writestr(img.path[1:], img._data())
  File "/home/divij/Desktop/Divij/Personal/venv/lib/python3.6/site-packages/openpyxl/drawing/image.py", line 64, in _data
    img = _import_image(self.ref)
  File "/home/divij/Desktop/Divij/Personal/venv/lib/python3.6/site-packages/openpyxl/drawing/image.py", line 33, in _import_image
    img = PILImage.open(img)
  File "/home/divij/Desktop/Divij/Personal/venv/lib/python3.6/site-packages/PIL/Image.py", line 2638, in open
    fp.seek(0)
ValueError: I/O operation on closed file.

I am getting the above error while trying to save workbook to file. In the save command, I have used the exact statements required to save a workbook to an excel file, but I get this everytime.

Google suggests that a file must be open. But I cannot pass the file object to workbook.save(<filename>) function as it can only take a filename so that has turned out futile.

Any suggestions what might be going wrong here?

Comments (3)

  1. CharlieC

    I'm not really sure what you're trying to do but the problem is related to the image file you've added to the worksheet. This is not really to do with openpyxl but with your file system.

    Please do not use the bug tracker to ask questions, that's what the mailing list is for.

  2. Divij Sehgal reporter

    Apologies. This surely seemed like a bug to me, since I could not work with a worksheet without the image where I should've been able to. Thanks for nudging me in the right direction.

  3. Log in to comment