【问题标题】:how to insert jpeg image into excel sheet in unix如何在unix中将jpeg图像插入到excel表中
【发布时间】:2014-12-06 00:17:33
【问题描述】:

我可以使用以下代码在 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')

这是将 bmp 图像正确插入工作表,但我担心 bmp 图像大约为 3MB,我无法在没有明显质量损失的情况下对其进行压缩。

有没有办法将 jpeg 图像插入到 unix 的工作表中?

【问题讨论】:

  • 您是否尝试过将 .jpg 图像传递给 insert_bitmap()?它可能会起作用,因为“位图”通常是图像文件的通用术语,并且不仅仅适用于存储在 .bmp 格式文件中的那些。
  • 我尝试过传递 .jpg 图片,但它不起作用。

标签: python excel unix xlwt


【解决方案1】:

从代码来看,xlwt 似乎只支持 24 位位图图像。

XlsxWriter Python 模块可以插入 PNG 图像(或 JPEG 或位图)。这是一个例子:

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()

输出:

有关详细信息,请参阅relevant section of the docs

【讨论】:

  • xlsxwriter 看起来很棒,但不幸的是它没有安装在我的 unix 系统中,我不允许安装任何东西。所以还在寻找其他选择
  • @sidharthcnadhan:由于包是纯 Python(没有二进制文件,没有要编译的 C 扩展等),因此在技术上不需要“安装”。它可以与您正在编写的程序位于同一目录中。
【解决方案2】:

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

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

worksheet.insert_image('B5', 'python.png', {'x_offset': 2, 'y_offset': 2, 'x_scale': 0.5, 'y_scale': 0.5})

这花了我一秒钟的时间才弄清楚,认为这可能会节省其他人一些时间

【讨论】:

    猜你喜欢
    • 2011-09-17
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2022-08-08
    • 1970-01-01
    相关资源
    最近更新 更多