z.rubi
z.rubi

Reputation: 327

Python: Unable to open excel file using openpyxl -- file exists and is in right location

I am trying to code a simple script to copy and paste data in an excel file. I am first changing the working directory to the directory with the excel file, and then attempting to open the only excel file in that folder with openpyxl. I am recieving an error in trying to do so. I will post my code, and the line of the openpyxl module that throws the error.

import openpyxl
import os

os.chdir('C:/Users/Emily Renda/Documents/ExcelWS/Driver Payroll/New Weeks')

wb = openpyxl.load_workbook("Python.xlsx")
sheet = wb.get_sheet_by_name("Payroll")

This code fails on the "wb = .." line and throws the following error:

File "C:\Users\Emily Renda\PycharmProjects\payrollExcel\venv\lib\site- 
packages\openpyxl\drawing\image.py", line 27, in _import_image import Image as PILImage
ModuleNotFoundError: No module named 'Image'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Emily Renda\PycharmProjects\payrollExcel\venv\lib\site- 
packages\openpyxl\drawing\image.py", line 29, in _import_image
    from PIL import Image as PILImage
ModuleNotFoundError: No module named 'PIL'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Emily 
Renda/PycharmProjects/payrollExcel/writeExcelFromJava.py", line 6, in 
 <module>
    wb = openpyxl.load_workbook("Python.xlsx")
   File "C:\Users\Emily Renda\PycharmProjects\payrollExcel\venv\lib\site- 
packages\openpyxl\reader\excel.py", line 275, in load_workbook
     charts, images = find_images(archive, rel.target)
   File "C:\Users\Emily Renda\PycharmProjects\payrollExcel\venv\lib\site- 
  packages\openpyxl\reader\drawings.py", line 47, in find_images
     image = Image(BytesIO(archive.read(dep.target)))
   File "C:\Users\Emily Renda\PycharmProjects\payrollExcel\venv\lib\site- 
 packages\openpyxl\drawing\image.py", line 51, in __init__
    image = _import_image(img)
   File "C:\Users\Emily Renda\PycharmProjects\payrollExcel\venv\lib\site- 
 packages\openpyxl\drawing\image.py", line 31, in _import_image
    raise ImportError('You must install PIL to fetch image objects')
 ImportError: You must install PIL to fetch image objects

The line of code that throws this error is:

     self.fp = io.open(file, filemode)  

within the openpyxl module. I am coming here as a last resort, I have debugged for about an hour and cannot figure out how this error is occuring (although I am new to python)

Upvotes: 0

Views: 1955

Answers (1)

user11588289
user11588289

Reputation: 26

Your excel file have an image. I had the same error code, deleted the images in the workbook and that worked out.

Upvotes: 1

Related Questions