在GAE中的Excel文件(xlwt)中插入位图图像



我需要在Excel文件中插入位图图像(使用xlwt创建)。我试图使用insert_bimap()方法插入,但它返回IO错误。

错误:

Traceback (most recent call last):  
File "C:Program FilesGooglegoogle_appenginegoogleappengineextwebapp_webapp25.py", line 701, in __call__    
   handler.get(*groups)  
File "C:appstest.py", line 44, in get
ws0.insert_bitmap('images/logo.gif', 2, 2)
File "C:appsxlwtWorksheet.py", line 1034, in insert_bitmap
bmp = Bitmap.ImDataBmpRecord(filename)
File "C:appsxlwtBitmap.py", line 255, in __init__
self.width, self.height, self.size, data = _process_bitmap(filename)
File "C:appsxlwtBitmap.py", line 195, in _process_bitmap
fh = file(bitmap, "rb")
File "C:Program FilesGooglegoogle_appenginegoogleappenginetoolsdev_appserver_import_hook.py", line 578, in __init__
raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: 'images/logo.gif'

代码:

  class MainHandler(webapp.RequestHandler):        
     def get(self):
        wb = Workbook()
        ws0 = wb.add_sheet('Sheet 1')
        ws0.write(0, 2, "chg wid: none")
        ws0.insert_bitmap('images/logo.gif', 2, 2)
        self.response.headers['Content-Type'] = 'application/ms-excel'
        self.response.headers['Content-Transfer-Encoding'] = 'Binary'
        self.response.headers['Content-disposition'] = 'attachment; filename="Sample.xls"'
        wb.save(self.response.out)

请告诉我是否有解决办法?

干杯
NN

似乎insert_bitmap()只能使用bmp格式。打开你的gif文件,以bmp格式保存一份副本,并用insert_bitmap('images/logo.bmp',2,2)调用它,它就可以工作了。

我确信这个项目已经完成很久了,但请查看xlsxwriter

http://xlsxwriter.readthedocs.org/en/latest/example_images.html

它在插入图像方面比xlwt好得多,支持jpegs和png文件,而不仅仅是bmp

如果你需要在一个插入中偏移和缩放图像:

worksheet.insert_image('B5','/python/reports/garmentsspreadsheet/Images/Garments/6702RD-WH.jpg',{'x_offset':2,'y_offset':2,'x_scale':0.5,'y_scale':0.5})

偏移以像素为单位

没有xlsxwriter

  • pip安装xlsxwriter

没有pip?这就像easy_install

  • centos/rehat

    百胜安装python-pip或百胜安装pip

  • debian

    apt-get安装pip

最新更新