简体   繁体   English

Python:无法使用openpyxl打开excel文件-文件存在且位于正确的位置

[英]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. 我正在尝试编写一个简单的脚本来将数据复制并粘贴到Excel文件中。 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. 我首先将工作目录更改为带有excel文件的目录,然后尝试使用openpyxl打开该文件夹中的唯一excel文件。 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. 我将发布我的代码,以及引发错误的openpyxl模块行。

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: 此代码在“ wb = ..”行上失败,并引发以下错误:

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. 在openpyxl模块中。 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) 我来这里是万不得已的方法,我已经调试了大约一个小时,无法弄清楚此错误是如何发生的(尽管我是python的新手)

Your excel file have an image. 您的excel文件中有图片。 I had the same error code, deleted the images in the workbook and that worked out. 我有相同的错误代码,删除了工作簿中的图像,然后解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM