如何使用openpyxl查找包含图像的单元格 [英] How to find cells that contain images with openpyxl

查看:608
本文介绍了如何使用openpyxl查找包含图像的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历工作簿中的每个单元.由于某些原因,当openpyxl遍历包含图像的工作表时,该图像将被删除.如何解决这个问题?

I am looping through every cell in a workbook. For some reason, when openpyxl iterates through a worksheet that contains an image, the image is deleted. How does one fix this?

我正在寻找一种方法来传递具有图像的单元格.

I am looking for a way to pass over cells that have an image.

我不处于read_only模式,也不想这样做,因为我正在对工作表进行修改.

I am not in read_only mode, and do not desire to be, as I am making modifications to the worksheets.

这样的事情对我来说最有用:

Something like this would be most useful to me:

if cell is image:
    pass

推荐答案

文档明确指出会发生这种情况:

The documentation clearly states that this will happen:

警告
openpyxl当前无法读取Excel文件中的所有可能项目,因此如果打开并使用相同的名称保存它们,则会从现有文件中丢失图像和图表.

Warning
openpyxl does currently not read all possible items in an Excel file so images and charts will be lost from existing files if they are opened and saved with the same name.

[重点突出]

这与以下事实有关:图像和图表不是常规单元格内容,而是存储在文件的单独部分中.那里有写图像的功能,但是没有读取图像的功能.

This has to do with the fact that images and charts are not regular cell content and are stored in a separate part of the file. The functionality to write the images is there, but not to read them.

您的选择是等待开发人员实现该功能,使用其他库或自己提供补丁.

Your options are to wait for the developers to implement that feature, to use a different library, or to contribute a patch yourself.

一个更现实的选择可能是将您所做的修改写入其他文件,然后将非图像部分手动复制回原始文件.

A more realistic option might be to write the modifications you make to a different file, and then copy the non-image portions back to the original manually.

理想情况下,在您的情况下这似乎是正确的,您可以从电子表格的外部获得图像.在这种情况下,您可以在编写文件时将其重新插入.

Ideally, and this seems to be true in your case, you would have the images available from outside the spreadsheet. In that case, you could insert them back in when you write the file.

另一个可能的解决方法是将原始文件作为zip文件打开并从中提取图像(手动或以编程方式).

Yet another potential workaround would be to open the original file as a zip file and extract the images from it (either manually or programatically).

这篇关于如何使用openpyxl查找包含图像的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆