I am able to insert bmp images using insert_bitmap
command of the xlwt
module in python using the following code:
我可以使用以下代码在python中使用xlwt模块的insert_bitmap命令插入bmp图像:
import xlwt
from PIL import Image
book = xlwt.Workbook()
sheet3 = book.add_sheet('diagrams')
Image.open('violations.png').convert("RGB").save('violations.bmp')
sheet3.insert_bitmap('violations.bmp',5,13)
book.save('simple.xls')
This is correctly inserting the bmp image into the sheet but my concern is that the bmp image is around 3MB and I am unable to compress it without significant quality loss.
这是正确地将bmp图像插入到工作表中,但我担心的是bmp图像大约是3MB,我无法在没有明显质量损失的情况下压缩它。
Is there some way to insert jpeg images into a worksheet in unix ?
有没有办法将jpeg图像插入到unix中的工作表中?
2 个解决方案
#1
7
From looking at the code it looks like xlwt only supports 24bit bitmap images.
从查看代码看起来xlwt只支持24位位图图像。
The XlsxWriter Python module can insert PNG images (or JPEG or Bitmap). Here is an example:
XlsxWriter Python模块可以插入PNG图像(或JPEG或Bitmap)。这是一个例子:
from xlsxwriter.workbook import Workbook
# Create an new Excel file and add a worksheet.
workbook = Workbook('images.xlsx')
worksheet = workbook.add_worksheet()
# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 30)
# Insert an image.
worksheet.write('A2', 'Insert an image in a cell:')
worksheet.insert_image('B2', 'python.png')
workbook.close()
Output:
输出:
See the relevant section of the docs for further information.
有关详细信息,请参阅文档的相关部分。
#2
1
from http://xlsxwriter.readthedocs.org/en/latest/example_images.html
来自http://xlsxwriter.readthedocs.org/en/latest/example_images.html
if you need to offset -and- scale an image in one insert:
如果您需要在一个插入中偏移 - 并 - 缩放图像:
worksheet.insert_image('B5', 'python.png', {'x_offset': 2, 'y_offset': 2, 'x_scale': 0.5, 'y_scale': 0.5})
worksheet.insert_image('B5','python.png',{'x_offset':2,'y_offset':2,'x_scale':0.5,'y_scale':0.5})
this took me a second to figure out, thought it might save others some time
这花了我一秒钟才弄明白,认为这可能会节省一些时间
#1
7
From looking at the code it looks like xlwt only supports 24bit bitmap images.
从查看代码看起来xlwt只支持24位位图图像。
The XlsxWriter Python module can insert PNG images (or JPEG or Bitmap). Here is an example:
XlsxWriter Python模块可以插入PNG图像(或JPEG或Bitmap)。这是一个例子:
from xlsxwriter.workbook import Workbook
# Create an new Excel file and add a worksheet.
workbook = Workbook('images.xlsx')
worksheet = workbook.add_worksheet()
# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 30)
# Insert an image.
worksheet.write('A2', 'Insert an image in a cell:')
worksheet.insert_image('B2', 'python.png')
workbook.close()
Output:
输出:
See the relevant section of the docs for further information.
有关详细信息,请参阅文档的相关部分。
#2
1
from http://xlsxwriter.readthedocs.org/en/latest/example_images.html
来自http://xlsxwriter.readthedocs.org/en/latest/example_images.html
if you need to offset -and- scale an image in one insert:
如果您需要在一个插入中偏移 - 并 - 缩放图像:
worksheet.insert_image('B5', 'python.png', {'x_offset': 2, 'y_offset': 2, 'x_scale': 0.5, 'y_scale': 0.5})
worksheet.insert_image('B5','python.png',{'x_offset':2,'y_offset':2,'x_scale':0.5,'y_scale':0.5})
this took me a second to figure out, thought it might save others some time
这花了我一秒钟才弄明白,认为这可能会节省一些时间