1

我需要在 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)

请让我知道是否有任何解决方法?

干杯! ,
神经网络

4

2 回答 2

3

我确定这个项目早就完成了,但请查看 xlsxwriter

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

插入图片比xlwt好很多,支持jpegs和png文件,不只是bmp

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

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

偏移量以像素为单位

没有 xlsxwriter

  • 点安装 xlsxwriter

没有点子?就像easy_install

  • centos/红帽

    yum install python-pip 或 yum install pip

  • Debian

    apt-get 安装点子

于 2014-12-05T16:10:52.817 回答
3

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

于 2012-03-29T08:00:18.450 回答