gpt4 book ai didi

python - openpyxl : ValueError: I/O operation on closed file

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:03 24 4
gpt4 key购买 nike

我使用此代码将图像 logo.png 放入单元格 A1 的 logo.xlsx 中:

from openpyxl import Workbook
from openpyxl.drawing.image import Image
wb = Workbook()
ws = wb.active
img = Image('logo.png')
ws.add_image(img, 'A1')
wb.save('logo.xlsx')

现在我尝试使用 load_workbook 将此文件作为现有工作簿读取,使用以下代码:

from openpyxl import load_workbook
wb = load_workbook('./logo.xlsx')
wb.save('logo_new.xlsx')

但我收到以下错误:

Traceback (most recent call last):
File "c:\Users\Sarmad Gulzar\Documents\Visual Studio Code\Python\Excel\test.py", line 3, in <module>
wb.save("new3.xlsx")
File "C:\Python37\lib\site-packages\openpyxl\workbook\workbook.py", line 391, in save
save_workbook(self, filename)
File "C:\Python37\lib\site-packages\openpyxl\writer\excel.py", line 284, in save_workbook
writer.save(filename)
File "C:\Python37\lib\site-packages\openpyxl\writer\excel.py", line 266, in save
self.write_data()
File "C:\Python37\lib\site-packages\openpyxl\writer\excel.py", line 85, in write_data
self._write_images()
File "C:\Python37\lib\site-packages\openpyxl\writer\excel.py", line 122, in _write_images
self._archive.writestr(img.path[1:], img._data())
File "C:\Python37\lib\site-packages\openpyxl\drawing\image.py", line 64, in _data
img = _import_image(self.ref)
File "C:\Python37\lib\site-packages\openpyxl\drawing\image.py", line 33, in _import_image
img = PILImage.open(img)
File "C:\Python37\lib\site-packages\PIL\Image.py", line 2638, in open
fp.seek(0)
ValueError: I/O operation on closed file.

P.S:我尝试对没有图像的文件执行相同的操作。效果很好。

最佳答案

我使用的是 openpyxl 版本 2.5.14。降级到 2.5.11 修复了这个问题。更多信息here .

关于python - openpyxl : ValueError: I/O operation on closed file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54847209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com