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)
相关推荐
Li emily33 分钟前
解决港股实时行情数据 API 接入难题
人工智能·python·fastapi
wfeqhfxz25887821 小时前
农田杂草检测与识别系统基于YOLO11实现六种杂草自动识别_1
python
mftang1 小时前
Python 字符串拼接成字节详解
开发语言·python
0思必得01 小时前
[Web自动化] Selenium设置相关执行文件路径
前端·爬虫·python·selenium·自动化
石去皿1 小时前
大模型面试通关指南:28道高频考题深度解析与实战要点
人工智能·python·面试·职场和发展
jasligea2 小时前
构建个人智能助手
开发语言·python·自然语言处理
kokunka2 小时前
【源码+注释】纯C++小游戏开发之射击小球游戏
开发语言·c++·游戏
测试秃头怪2 小时前
面试大厂就靠这份软件测试八股文了【含答案】
自动化测试·软件测试·python·功能测试·面试·职场和发展·单元测试
测试杂货铺2 小时前
软件测试面试题大全,你要的都在这。。
自动化测试·软件测试·python·功能测试·面试·职场和发展·测试用例
测试大圣2 小时前
软件测试基础知识总结(超全的)
软件测试·python·功能测试·测试工具·职场和发展·单元测试·测试用例