import pandas as pd

# 读取WPS表格文件(.xlsx格式)
input_file_path = 'test.xlsx' # 替换为你的WPS表格文件路径
sheet_name = 'Sheet1' # 替换为你的工作表名称,如果不知道可以省略,pandas会默认读取第一个工作表
df = pd.read_excel(input_file_path, sheet_name=sheet_name)

# 遍历偶数行(注意:在pandas中,行索引是从0开始的,所以偶数索引对应偶数行)
for index in range(0, len(df), 2):
# 互换A列和B列的值
df.iat[index, 0], df.iat[index, 1] = df.iat[index, 1], df.iat[index, 0] # 使用.iat进行基于位置的赋值,速度更快
# 互换C列和D列的值
df.iat[index, 2], df.iat[index, 3] = df.iat[index, 3], df.iat[index, 2]

# 将修改后的数据保存回一个新的WPS表格文件
output_file_path = 'modified_wps_file.xlsx' # 输出文件路径
df.to_excel(output_file_path, index=False, sheet_name=sheet_name, engine='openpyxl')

print(f"文件已保存为 {output_file_path}")