📝 面试求职: 「面试试题小程序」 ,内容涵盖 测试基础、Linux操作系统、MySQL数据库、Web功能测试、接口测试、APPium移动端测试、Python知识、Selenium自动化测试相关、性能测试、性能测试、计算机网络知识、Jmeter、HR面试,命中率杠杠的。(大家刷起来…)
📝 职场经验干货:
环境准备
确保安装了以下依赖库:
pandas:用于读取 Excel 文件。
excel2img:用于生成 Excel 表格的截图。
可以通过以下命令安装这些库:
pip install pandas excel2img
实现代码
以下是完整的 Python 脚本,用于批量生成 Excel 表格中所有工作表的截图。
import os
import excel2img
import pandas as pd
def out_img_for_all_sheets(excel_file):
try:
print(f"开始处理文件:{excel_file},截图请耐心等待...")
# 使用pandas读取Excel文件
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
详细解释
导入必要的库
import os
import excel2img
import pandas as pd
定义 out_img_for_all_sheets 函数
读取 Excel 文件:使用 pandas 读取 Excel 文件。
获取所有工作表名称:使用 pd.ExcelFile 和 sheet_names 属性。
生成截图:使用 excel2img.export_img 生成每个工作表的截图。
def out_img_for_all_sheets(excel_file):
try:
print(f"开始处理文件:{excel_file},截图请耐心等待...")
# 使用pandas读取Excel文件
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}")
定义 batch_process_excel_files 函数
遍历文件夹中的所有文件:使用 os.listdir 遍历指定文件夹中的所有文件。
处理符合条件的 Excel 文件:检查文件扩展名是否为 .xls, .xlsx, 或 .xlsm,然后调用 out_img_for_all_sheets 函数。
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)
示例用法
指定文件夹路径:指定包含 Excel 文件的文件夹路径。
调用 batch_process_excel_files 函数:批量处理文件夹下的所有 Excel 文件。
folder_path = '/path/to/excel/files'
batch_process_excel_files(folder_path)
核心优势
批量处理:一键处理文件夹下所有 Excel 文件的所有表格,无需手动干预。
高效率:从数千分钟缩短至几秒钟,极大提升工作效率。
完整性:保证大型表格的完整性展示,无遗漏。
高清晰度:截图清晰,便于阅读和分享。
视觉统一:统一的截图风格,提升阅读体验。
总结
通过上述 Python 脚本,你可以轻松实现 Excel 表格的整表截图,彻底告别繁琐的手动截图,大幅提升工作效率。无论是在日常工作中还是项目管理中,这种方法都非常实用。
最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】


450

被折叠的 条评论
为什么被折叠?



