当我尝试将来自 python 中的 url 的图像插入 excel 时"coercing to Unicode: need string or buffer, cStringIO.StringO fo



我正在尝试从网址将图像插入 excel 中,但出现此错误:

coercing to Unicode: need string or buffer, cStringIO.StringO found

这是我的代码:

import cStringIO
from PIL import Image
import urllib
wb = pyExcelerator.Workbook()
ws = wb.add_sheet('sheet 1')
url= 'this url contains the url of an image'
f = urllib.urlopen(url)
buf = f.read()
fileIO = cStringIO.StringIO(buf)
img = Image.open(fileIO).convert("RGB")
img.thumbnail((71, 100), Image.ANTIALIAS)
img_bmp = cStringIO.StringIO()
img.save(img_bmp, 'BMP')
img_bmp.seek(0)
ws.insert_bitmap(img_bmp, 0, 1)

我必须保存图像并转换为bmp。

import cStringIO
from PIL import Image
import urllib
wb = pyExcelerator.Workbook()
ws = wb.add_sheet('sheet 1')
url= 'this url contains the url of an image'
dir= 'It is a directory location'
f = urllib.urlopen(url)
buf = f.read()
fileIO = cStringIO.StringIO(buf)
img = Image.open(fileIO).convert("RGB")
img.thumbnail((71, 100), Image.ANTIALIAS)
img_bmp = cStringIO.StringIO()
#this is the one that converts the image to bmp
img.save(img_bmp, 'BMP') 
with open('{}/prova_img.bmp'.format(dir), 'w') as file_handle:
file_handle.write(img_bmp.getvalue())
ws.insert_bitmap('{}/the_bitmap_image.bmp'.format(dir), 0, 1)

最新更新