首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从xlsx python读取映像将无法在关闭的文件上执行I/O操作。

从xlsx python读取映像将无法在关闭的文件上执行I/O操作。
EN

Stack Overflow用户
提问于 2022-09-06 15:21:55
回答 1查看 83关注 0票数 1

我试图从xlsx单元格中读取图像,应该是简单的,但它总是会产生错误。

代码语言:javascript
运行
复制
import openpyxl
from openpyxl_image_loader import SheetImageLoader


filename = 'media/planilhas/tabela_de_simulador.xlsx'
workbook = openpyxl.load_workbook(filename)

logos = workbook['operadoras']
image_loader = SheetImageLoader(logos)

image_loader.get('B2')

我确信在单元格上有一个图像,因为当我更改单元格时,错误信息显示没有图像。这是一个错误:

代码语言:javascript
运行
复制
ValueError                                Traceback (most recent call last)
Input In [108], in <cell line: 1>()
----> 1 image_loader.get('B2')

File ~/Documents/busqueplanodesaude/backend/venv/lib/python3.9/site-packages/openpyxl_image_loader/sheet_image_loader.py:32, in SheetImageLoader.get(self, cell)
     30     raise ValueError("Cell {} doesn't contain an image".format(cell))
     31 else:
---> 32     image = io.BytesIO(self._images[cell]())
     33     return Image.open(image)

File ~/Documents/busqueplanodesaude/backend/venv/lib/python3.9/site-packages/openpyxl/drawing/image.py:64, in Image._data(self)
     60 def _data(self):
     61     """
     62     Open image and write it to a buffer when saving the workbook
     63     """
---> 64     img = _import_image(self.ref)
     65     fp = None
     66     # don't convert these file formats

File ~/Documents/busqueplanodesaude/backend/venv/lib/python3.9/site-packages/openpyxl/drawing/image.py:33, in _import_image(img)
     30     raise ImportError('You must install Pillow to fetch image objects')
     32 if not isinstance(img, PILImage.Image):
---> 33     img = PILImage.open(img)
     35 return img

File ~/Documents/busqueplanodesaude/backend/venv/lib/python3.9/site-packages/PIL/Image.py:3096, in open(fp, mode, formats)
   3093     exclusive_fp = True
   3095 try:
-> 3096     fp.seek(0)
   3097 except (AttributeError, io.UnsupportedOperation):
   3098     fp = io.BytesIO(fp.read())

ValueError: I/O operation on closed file.

有人对此有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2022-11-15 13:44:34

问题在excel文件中。你的图像超出了细胞边界。确保图片在单元格边界内!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73624416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档