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)
相关推荐
天***88961 天前
js封装一个双精度算法实现
开发语言·前端·javascript
.小小陈.1 天前
数据结构2:单链表
c语言·开发语言·数据结构·笔记·学习方法
ERROR_LESS1 天前
【ADS-1】【python基础-2】基本语法与数据结构(列表、字典、集合)
python
Camel卡蒙1 天前
数据结构——二叉搜索树Binary Search Tree(介绍、Java实现增删查改、中序遍历等)
java·开发语言·数据结构
2401_841495641 天前
【数据结构】基于Floyd算法的最短路径求解
java·数据结构·c++·python·算法··floyd
Algebraaaaa1 天前
什么是前端、后端与全栈开发,Qt属于什么?
开发语言·前端·qt
立志成为大牛的小牛1 天前
数据结构——二十三、并查集的终极优化(王道408)
开发语言·数据结构·笔记·学习·程序人生·考研
一晌小贪欢1 天前
Python爬虫第6课:Selenium自动化浏览器与动态内容抓取
爬虫·python·selenium·网络爬虫·python基础·python3·pathon爬虫
番石榴AI1 天前
自己动手做一款ChatExcel数据分析系统,智能分析 Excel 数据
人工智能·python·数据挖掘·excel
纵有疾風起1 天前
C++模版:模板初阶及STL简介
开发语言·c++·经验分享·开源