我正在尝试编写一个简单的脚本,以将数据复制并粘贴到excel文件中。我首先将工作目录更改为带有excel文件的目录,然后尝试使用openpyxl打开该文件夹中的唯一excel文件。我在尝试这样做时收到错误。我将发布我的代码,以及引发错误的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")
此代码在“ 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
引发此错误的代码行是:
self.fp = io.open(file, filemode)
在openpyxl模块中。我来这里是万不得已的方法,我已经调试了大约一个小时,无法弄清楚这个错误是怎么发生的(尽管我是python的新手)
答案 0 :(得分:0)
您的excel文件有一个图像。我有相同的错误代码,删除了工作簿中的图像,然后解决了。