Python为文件夹下所有Excel工作簿的所有sheet截图

from PIL import ImageGrab  # 需安装Pillow包
import xlwings as xw
import pandas as pd
import os


def main(*args, **kwargs):
    """
    为文件夹下所有Excel工作簿的所有sheet截图
    """
    exl_dir = kwargs.get('exl_dir')

    for file_name in os.listdir(exl_dir):
        if '.xlsx' in file_name:
            file_path = os.path.join(exl_dir, file_name)
            app = xw.App(visible=True, add_book=False)
            wb = app.books.open(file_path)
            sht_names = list(pd.read_excel(file_path, sheet_name=None))

            for shot_sheetname in sht_names:
                sheet = wb.sheets(shot_sheetname)
                used_range = sheet.used_range
                used_range.api.CopyPicture()

                try:  # 防止工作表有密码保护无法粘贴
                    sheet.api.Paste()
                    pic = sheet.pictures[0]
                    pic.api.Copy()
                except:
                    xw.sheets.add(name='中转表', before=None, after=None)
                    sheet_new = wb.sheets('中转表')
                    sheet_new.api.Paste()
                    pic = sheet_new.pictures[0]
                    pic.api.Copy()
                    sheet_new.delete()
                finally:
                    img = ImageGrab.grabclipboard()
                    img_path = os.path.join(r'D:\桌面\截图', file_name + '-' + shot_sheetname + '.png')
                    img.save(img_path)

            wb.close()
            app.quit()


if __name__ == '__main__':
    main(exl_dir=r'D:\桌面')

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值