Python Script for Batch Generating Screenshots of All Sheets in Excel Files
This guide explains how to install required libraries and use a Python script that leverages pandas and excel2img to automatically capture high‑resolution screenshots of every worksheet in multiple Excel files, streamlining batch processing and boosting productivity.
Environment Preparation : Ensure the required libraries pandas and excel2img are installed. Use the command pip install pandas excel2img to install them.
Implementation Code : The following Python script reads an Excel file, iterates through all worksheets, and generates a screenshot for each sheet using excel2img.
import os
import excel2img
import pandas as pd
def out_img_for_all_sheets(excel_file):
try:
print(f"开始处理文件:{excel_file},截图请耐心等待...")
xls = pd.ExcelFile(excel_file)
sheet_list = xls.sheet_names
for sheet_name in sheet_list:
output_file = f"{os.path.splitext(os.path.basename(excel_file))[0]}_{sheet_name}.png"
excel2img.export_img(excel_file, output_file, sheet_name, None)
except Exception as e:
print(f"文件 {excel_file} 截图失败!错误信息:{e}")
def batch_process_excel_files(folder_path):
print("批量处理文件夹下所有Excel文件...")
for file in os.listdir(folder_path):
if file.endswith(('.xls', '.xlsx', '.xlsm')):
file_path = os.path.join(folder_path, file)
out_img_for_all_sheets(file_path)
folder_path = '/path/to/excel/files'
batch_process_excel_files(folder_path)Explanation : The script imports necessary modules, defines out_img_for_all_sheets to handle a single file, and batch_process_excel_files to process all Excel files in a directory.
Example Usage : Set folder_path to the directory containing Excel files and call batch_process_excel_files(folder_path) .
Core Advantages : Batch processing of all worksheets, high efficiency, completeness, high‑resolution screenshots, and consistent visual style.
Conclusion : This Python script enables effortless full‑sheet screenshots of Excel workbooks, dramatically improving productivity in routine tasks and project management.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.