pandas修改.xlsx文件后保留文件原样式

import pandas as pd
def trade(file):
    df = pd.read_excel(file)
    datas = df.loc[:, ["交易日期", "客户简称"]].values.tolist()
    flag = ["否"] * len(datas)
    for index, values in enumerate(datas):
        for index2, data in enumerate(datas[index + 1:len(datas)]):
            if data == values:
                flag[index2 + index + 1] = "是"
    df["是否重复"] = flag
    # df.to_excel(file, sheet_name="Sheet1", index=False)
    with pd.ExcelWriter(file, engine="openpyxl", mode='a', if_sheet_exists='overlay') as writer:
        df.to_excel(writer, sheet_name='Sheet1', index=False) # engine="openpyxl"


if __name__ == '__main__':
    file=r"D:\Users\admin\Desktop\交易确认书台账-个股-20221024.xlsx"  
    trade(file)

你可能感兴趣的:(pandas学习,python,pandas)