from openpyxl import load_workbook
from openpyxl.drawing.image import Image #tupian
workbook = load_workbook(filename="2.xlsx")
sheet = workbook.active
one = Image("666.png")
one.height = 50
one.width = 50
sheet.add_image(one,"c1")
workbook.save(filename="2.xlsx")
在使用openpuxl对表格插入图片时报了这么一个错:ImportError: You must install PIL to fetch image objects
Traceback (most recent call last):
File "6.py", line 12, in <module>
one = Image("666.png")
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\openpyxl\drawing\image.py", line 32, in __init__
image = _import_image(img)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\openpyxl\drawing\image.py", line 13, in _import_image
raise ImportError('You must install Pillow to fetch image objects')
ImportError: You must install Pillow to fetch image objects
解决办法:在cmd窗口下输入:pip install pillow
,
pip install pillow
C:\Users\lenovo>pip install pillow
Collecting pillow
Downloading https://files.pythonhosted.org/packages/88/6b/66f502b5ea615f69433ae1e23ec786b2cdadbe41a5cfb1e1fabb4f9c6ce9/Pillow-7.0.0-cp37-cp37m-win_amd64.whl (2.0MB)
100% |████████████████████████████████| 2.0MB 232kB/s
Installing collected packages: pillow
Successfully installed pillow-7.0.0
You are using pip version 10.0.1, however version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
这样就可以正常运行啦
C:\Users\lenovo\Desktop>python 6.py