使用 openpyxl 将图像插入 Excel [英] Insert an image into Excel with openpyxl

查看:117
本文介绍了使用 openpyxl 将图像插入 Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的电脑崩溃了,所以我不得不重新安装我所有的库;安装完成后,我意识到一些库更新到了新版本,我的代码不再有效(由于 openpyxl 的新版本).

My PC crashed and so I had to reinstall all of my libraries; after finishing the install, I realized that some libraries updated to a new version and my code no longer works (due to a new version of openpyxl).

我正在尝试将图像插入 Excel 文件,但我不明白出现的错误消息.其他问题似乎是针对旧版本的 openpyxl(我的原始代码对其有效),但不适用于当前版本的 openpyxl.感谢您帮助我了解如何修复我的代码.:)

I am trying to insert an image into an Excel file, but I do not understand the error messages that are occurring. Other questions seem to be for older versions of openpyxl (for which my original code worked), but do not work for the current version of openpyxl. Your assistance in helping me understand how to fix my code is appreciated. :)

原始代码(有效):

import openpyxl
wb = openpyxl.load_workbook(filename)
ws = wb.get_sheet_by_name(sheet_name)
img = openpyxl.drawing.image.Image(img_name)
img.anchor(ws['D2'])
ws.add_image(img)
wb.save(filename)

当前代码(不起作用):

Current code (which doesn't work):

import openpyxl
wb = openpyxl.load_workbook(filename)
ws = wb[sheet_name]
img = openpyxl.drawing.image.Image(img_name)
img.anchor(ws.cell(row=2,column=4))
ws.add_image(img)
wb.save(filename)

错误信息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-9efc1289cc73> in <module>()
----> 1 img.anchor(ws.cell(row=2,column=4))

TypeError: 'str' object is not callable

有什么提示吗?

谢谢

显然,img.anchor 现在是字符串;我不知道它曾经是什么,但显然不是字符串(因为没有错误消息.现在更改为以下内容设置了锚点,但我收到了不同的错误消息.

Apparently, img.anchor is now the string; I have no idea what it used to be but apparently was not a string (since there wasn't an error message. Changing to the following now sets the anchor, but I get a different error message.

设置锚点:

img.anchor = ws.cell(row=2,column=4)
ws.add_image(img)

但是现在尝试保存时崩溃了:

But it now crashes when trying to save:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-2cfd938ccf60> in <module>()
      1 img.anchor = ws.cell(row=2,column=4)
      2 ws.add_image(img)
----> 3 wb.save(filename)

AttributeError: 'Cell' object has no attribute 'upper'

推荐答案

有趣的东西.显然,在我拥有的任何版本的 openpyxl 和我现在拥有的版本(2.5.5)之间,img.anchor 改变了类型.它现在是一个字符串而不是工作表位置,因此您只需将其设置为位置(在我的情况下:'D2')而不是工作表位置(不要使用 ws['D2']).总结一下,当尝试用openpyxl 2.5.5插入图片时,使用如下:

Fun stuff. Apparently, between whatever version of openpyxl I had and the one I have now (2.5.5), img.anchor changed types. It is now a string instead of a worksheet location, so you need to simply set it to the location (in my case: 'D2') instead of the worksheet location (DON'T USE ws['D2']). To sum up, when trying to insert an image with openpyxl 2.5.5, use the following:

import openpyxl
wb = openpyxl.load_workbook(filename)
ws = wb[sheet_name]
img = openpyxl.drawing.image.Image(img_name)
img.anchor = 'D2' # Or whatever cell location you want to use.
ws.add_image(img)
wb.save(filename)

这篇关于使用 openpyxl 将图像插入 Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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