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

时间:2012-01-05 11:26:18

标签: python google-app-engine xlwt

我需要在Excel文件中插入一个Bitmap图像(使用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)

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

干杯! ,
NN

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:3)

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

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

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

  

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

     
    

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

  

偏移以像素为单位

没有xlsxwriter

  • pip install xlsxwriter

没有pip?这就像easy_install

  • 的centos /红帽

    yum install python-pip 要么 yum install pip

  • debian

    apt-get install pip