示例:
开发需求:解析Excel 文件中的内容并检查是否包含 "Fail" 字符,若没有则返回True,若有则返回False
实现代码:
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File : check_excel_for_fail.py
@Time : 2024/02/1 11:30:13
@Author : jly
@Version : 1.0
@Software: Visual Studio Code
'''
import pandas as pd
import openpyxl
def check_excel_for_fail(file_path):
try:
# 读取 Excel 文件
df = pd.read_excel(file_path)
# 检查 DataFrame 中是否包含 "Fail" 字符
if "Fail" not in df.to_string():
return True
else:
return False
except Exception as e:
print(f"Error reading Excel file: {e}")
return False
# 用法示例
file_path = "result.xls