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)
相关推荐
码上淘金2 小时前
【Python】Python常用控制结构详解:条件判断、遍历与循环控制
开发语言·python
Brilliant Nemo2 小时前
四、SpringMVC实战:构建高效表述层框架
开发语言·python
2301_787552873 小时前
console-chat-gpt开源程序是用于 AI Chat API 的 Python CLI
人工智能·python·gpt·开源·自动化
懵逼的小黑子3 小时前
Django 项目的 models 目录中,__init__.py 文件的作用
后端·python·django
Y3174293 小时前
Python Day23 学习
python·学习
Ai尚研修-贾莲4 小时前
Python语言在地球科学交叉领域中的应用——从数据可视化到常见数据分析方法的使用【实例操作】
python·信息可视化·数据分析·地球科学
格林威4 小时前
Baumer工业相机堡盟工业相机的工业视觉中为什么偏爱“黑白相机”
开发语言·c++·人工智能·数码相机·计算机视觉
橙子199110164 小时前
在 Kotlin 中什么是委托属性,简要说说其使用场景和原理
android·开发语言·kotlin
androidwork4 小时前
Kotlin Android LeakCanary内存泄漏检测实战
android·开发语言·kotlin
qq_508576095 小时前
if __name__ == ‘__main__‘
python