python实现Excel转图片

目录

使用spire.xls库

使用excel2img库


使用spire.xls库

安装:pip install spire.xls -i https://pypi.tuna.tsinghua.edu.cn/simple

支持选择行和列截图,不好的一点就是商业库,转出来的图片有水印。

python 复制代码
from spire.xls import Workbook


def excel_image(excel_file):
    '''
    ToImage 中可指定需要生成图片的行和列
    :param excel_file:
    :return:
    '''
    save_image_path = rf'{excel_file.split('.')[0]}.png'
    work = Workbook()
    work.LoadFromFile(excel_file)

    # 指定第一张表生成数据
    # sheet = work.Worksheets.get_Item(0)
    # # 移除页边距
    # pagesetup = sheet.PageSetup
    # pagesetup.TopMargin = 0
    # pagesetup.BottomMargin = 0
    # pagesetup.LeftMargin = 0
    # pagesetup.RightMargin = 0
    # image = sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn)
    # image.Save(save_image_path)
    # work.Dispose()

    # 将所有表单生成图片
    for i, sheet in enumerate(work.Worksheets):
        # image = sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn)
        image = sheet.ToImage(sheet.FirstRow, sheet.FirstColumn, 5, 2)
        image.Save(rf'{excel_file.split('.')[0]}{i}.png')

    work.Dispose()

使用excel2img库

安装:pip install excel2img -i https://pypi.tuna.tsinghua.edu.cn/simple

python 复制代码
import excel2img


def out_img(excel_file, sheet_list, output_dir):
    try:
        print("截图中,请等待...")
        for sheet in sheet_list:
            excel2img.export_img(excel_file, f"{output_dir}/{sheet}.png", sheet)
        print("截图完成!")
    except Exception as e:
        print("截图失败!", e)


if __name__ == '__main__':
    file_name = r'C:\Users\Administrator\Desktop\test\成绩表.xls'
    sheet_list = ['Sheet1', 'Sheet2']  # 需要截图sheet名称
    output_dir = r'C:\Users\Administrator\Desktop'
    out_img(file_name, sheet_list, output_dir)
相关推荐
高洁019 小时前
打造行业知识图谱三步走
python·深度学习·数据挖掘·知识图谱
装不满的克莱因瓶10 小时前
使用 PyTorch Tensor 的相关数据处理
人工智能·pytorch·python·深度学习·机器学习·ai
如烟花的信页10 小时前
易盾点选逆向分析
javascript·爬虫·python·js逆向
ZC跨境爬虫10 小时前
跟着 MDN 学 JavaScript day_2:JavaScript 初体验
开发语言·前端·javascript·学习·ecmascript
金銀銅鐵10 小时前
用 Tkinter 实现一个简单的罗马数字转化工具
后端·python
ckjoker10 小时前
四大AI Agent架构拆解:我手敲了一个迷你版,发现了7条可迁移的设计原则
python·agent
小二·10 小时前
Python 异步编程深度解析:Async/Await 实战
网络·python·github
Jun62610 小时前
QT(3)-线程中使用控件
开发语言·qt
xiaoshuaishuai810 小时前
C# AvaloniaUI ProgressBar用法
开发语言·c#
咋吃都不胖lyh10 小时前
LangGraph标准构建示例
开发语言·python