53 lines
2.0 KiB
Python
53 lines
2.0 KiB
Python
|
import pandas as pd
|
|||
|
|
|||
|
|
|||
|
def compare_excel_columns(file_a, file_b, sheet_a=0, sheet_b=0):
|
|||
|
"""
|
|||
|
对比两个Excel文件中指定列的数据是否完全相同
|
|||
|
|
|||
|
参数:
|
|||
|
file_a: 表A的文件路径
|
|||
|
file_b: 表B的文件路径
|
|||
|
sheet_a: 表A的工作表索引或名称(默认为第一个工作表)
|
|||
|
sheet_b: 表B的工作表索引或名称(默认为第一个工作表)
|
|||
|
"""
|
|||
|
try:
|
|||
|
# 读取Excel文件
|
|||
|
df_a = pd.read_excel(file_a, sheet_name=sheet_a, header=None)
|
|||
|
df_b = pd.read_excel(file_b, sheet_name=sheet_b, header=None)
|
|||
|
|
|||
|
# 提取F列从F4(即索引3行)开始的数据(注意: pandas列F是第5列,索引为4)
|
|||
|
col_a = df_a.iloc[3:, 5].reset_index(drop=True)
|
|||
|
# 提取G列从G4(即索引3行)开始的数据(注意: pandas列G是第6列,索引为6)
|
|||
|
col_b = df_b.iloc[3:, 5].reset_index(drop=True)
|
|||
|
|
|||
|
# 比较长度
|
|||
|
if len(col_a) != len(col_b):
|
|||
|
print(f"数据长度不一致: 表A有{len(col_a)}行,表B有{len(col_b)}行")
|
|||
|
return False
|
|||
|
|
|||
|
# 比较内容
|
|||
|
comparison = col_a == col_b
|
|||
|
if comparison.all():
|
|||
|
print("两列数据完全相同")
|
|||
|
return True
|
|||
|
else:
|
|||
|
# 找出不同的行
|
|||
|
diff_indices = comparison[comparison == False].index
|
|||
|
print(f"发现{len(diff_indices)}处不同:")
|
|||
|
for idx in diff_indices:
|
|||
|
print(f"行 {idx + 4}: 表A值='{col_a[idx]}', 表B值='{col_b[idx]}'")
|
|||
|
return False
|
|||
|
|
|||
|
except Exception as e:
|
|||
|
print(f"发生错误: {str(e)}")
|
|||
|
return False
|
|||
|
|
|||
|
|
|||
|
# 使用示例
|
|||
|
if __name__ == "__main__":
|
|||
|
file_a = "4_2/全国统计系统会议接送表 准1 - 副本(1).xls" # 替换为你的表A文件路径
|
|||
|
file_b = "4_2/(4.2)全国统计系统办公室工作会议参会人员报名表 - 副本(1).xls" # 替换为你的表B文件路径
|
|||
|
|
|||
|
result = compare_excel_columns(file_a, file_b)
|
|||
|
print("对比结果:", "相同" if result else "不同")
|