某某截图
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 删除。