Python 名称';openpyxl';未定义错误
还在这里学习python的来龙去脉。我在使用openpyxl库方面取得了一些小的成功,并启动了这个宠物项目,使用excel文件作为“画布”,从目录中读取一个1位bmp文件,然后通过删除指南在工作表上“复制显示”图像,减小每个单元格的大小,最后在每个单元格中添加一个小图像,其中包含来自Pizz的getdata方法的二进制数据列表 我还没有通读整个逻辑,但我在两个晚上都无法理解下面的代码,我仍然不知道为什么我在代码Python 名称';openpyxl';未定义错误,python,openpyxl,pillow,Python,Openpyxl,Pillow,还在这里学习python的来龙去脉。我在使用openpyxl库方面取得了一些小的成功,并启动了这个宠物项目,使用excel文件作为“画布”,从目录中读取一个1位bmp文件,然后通过删除指南在工作表上“复制显示”图像,减小每个单元格的大小,最后在每个单元格中添加一个小图像,其中包含来自Pizz的getdata方法的二进制数据列表 我还没有通读整个逻辑,但我在两个晚上都无法理解下面的代码,我仍然不知道为什么我在代码表的底部几乎是一行中出现了“openpyxl”未定义错误。添加_image(openp
表的底部几乎是一行中出现了“openpyxl”未定义错误。添加_image(openpyxl.drawing.image.image('square_blue.jpg'),colnum_string(col)+str row))
我成功地完成了类似的项目,使用了几乎相同的openpyxl和PIL库导入
from openpyxl import Workbook
import os
from PIL import Image
# for file in os.listdir():
# if file.endswith('bmp'):
os.chdir('c:/users/Sam/Desktop/py')
img = Image.open('trump.bmp')
img_width, img_height = img.size
pix_val = list(img.getdata())
wb = Workbook()
def colnum_string(n):
string = ""
while n > 0:
n, remainder = divmod(n - 1, 26)
string = chr(65 + remainder) + string
return string
r_height = 3
c_width = 3
sheet = wb.create_sheet("Mysheet")
for col in range(1, img_height):
sheet.row_dimensions[img_height].height = r_height
for row in range(1, img_width):
sheet.column_dimensions[colnum_string(col)] = c_width
sheet.add_image(openpyxl.drawing.image.Image('square_blue.jpg'), colnum_string(col)+str(row))
wb.save('out.xlsx')
有人能帮我吗?您只能从openpyxel导入“工作簿”子模块。尝试:
from openpyxel import Workbook, drawing # also import drawing namespace
[...]
sheet.add_image(drawing.image.Image('square_blue.jpg'), colnum_string(col)+str(row))
[...]
请提供您看到的完整错误消息。另外,请查看
openpyxl.utils
模块,了解数字列和字母列之间的转换。非常感谢。这确实很管用。我还是不明白为什么。在我以前的项目中,我必须从数百个项目编号的列表中将适当的图像放到一个单元格中,它工作得很好。现在,我有了一个应用程序,可以读取位图文件图像,将一个小jpg图像放入图像数据中对应于1值的每个单元格中,删除所有准则,并显示一个excel工作表画布,其中正确翻译了图像。再次感谢您的帮助@Sam,自从您从openpyxel导入绘图
名称空间后,这就可以工作了。如果不这样做,您的解释器甚至不知道您正在处理的图形是什么。这相当于:import openpyxl
,然后sheet.add_image(openpyxl.drawing.image…)。也许你可以接受这个答案?