首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >截图Excel

截图Excel

原创
作者头像
用户12501860
发布2026-05-25 19:47:16
发布2026-05-25 19:47:16
210
举报

某某截图

import os

import xlwings as xw

from PIL import ImageGrab

# ========= 自行修改这两个文件夹路径 =========

excel_dir = r"C:\Users\86150\Desktop\测试"

img_save_dir = r"C:\Users\86150\Desktop\logs"

# ===========================================

os.makedirs(img_save_dir, exist_ok=True)

def capture_excel(file_path, out_path):

try:

# visible=True 能确保截图正常(不会出现0KB、打不开)

with xw.App(visible=True, add_book=False) as app:

wb = app.books.open(file_path)

ws = wb.sheets[0]

# 自动识别数据区域

data_range = ws.used_range

range_addr = data_range.address

print(f"【{os.path.basename(file_path)}】识别区域:{range_addr}")

# 高清复制图片

ws.range(range_addr).api.CopyPicture(Appearance=1, Format=2)

# 从剪贴板获取图片(稳定版)

img = ImageGrab.grabclipboard()

if img:

img.save(out_path)

print(f"✅ 已保存:{os.path.basename(out_path)}\n")

else:

print(f"❌ 截图失败:剪贴板为空\n")

wb.close()

except Exception as err:

print(f"❌ 出错:{os.path.basename(file_path)} → {str(err)}\n")

if __name__ == "__main__":

total = 0

for file in os.listdir(excel_dir):

suffix = file.lower()

if suffix.endswith(".xlsx") or suffix.endswith(".xls"):

total += 1

excel_full_path = os.path.join(excel_dir, file)

name_without_suffix = os.path.splitext(file)[0]

png_file = f"{name_without_suffix}.png"

save_full_path = os.path.join(img_save_dir, png_file)

capture_excel(excel_full_path, save_full_path)

print(f"批量处理完成,共处理 {total} 个文件")

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档