Skip to main content
 首页 » 编程设计

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

2025年05月04日18qq78292959

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

错误:

Traceback (most recent call last):   
File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\_webapp25.py", line 701, in __call__     
   handler.get(*groups)   
File "C:\apps\test.py", line 44, in get 
ws0.insert_bitmap('images/logo.gif', 2, 2) 
File "C:\apps\xlwt\Worksheet.py", line 1034, in insert_bitmap 
bmp = Bitmap.ImDataBmpRecord(filename) 
File "C:\apps\xlwt\Bitmap.py", line 255, in __init__ 
self.width, self.height, self.size, data = _process_bitmap(filename) 
File "C:\apps\xlwt\Bitmap.py", line 195, in _process_bitmap 
fh = file(bitmap, "rb") 
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_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) 

如果有任何解决方法,请告诉我?

干杯! ,
神经元

请您参考如下方法:

insert_bitmap() 似乎只适用于 bmp 格式。打开您的 gif 文件,以 bmp 格式保存一个副本并使用 insert_bitmap('images/logo.bmp',2,2) 调用它,它将起作用。