Export test Data to Excel by Pandas Posted on 2021-12-19 | In Python | Visitors 123456789101112131415161718def generateTestDataFile(path,testData_list): df=pd.DataFrame(testData_list) df_testdata=df[df['TransactionType'].isin(['NewCard Sale','MoneyPak Sale', 'GDC SwipeReload','GDC Unload','GDN SwipeReload'])] df_pinkey=df_testdata.filter(items=["PartnerName","TransactionType",'Pinkey']) df_pinkey=df_pinkey.loc[df_pinkey["Pinkey"]!=""] df_ls=[] writer = pd.ExcelWriter(path) df_testdata.to_excel(writer, sheet_name='TestData', index=False) df_pinkey.to_excel(writer, sheet_name='Pinkey', index=False) for column in df_testdata: column_length = max(df_testdata[column].astype(str).map(len).max(), len(column)) col_idx = df_testdata.columns.get_loc(column) writer.sheets['TestData'].set_column(col_idx, col_idx, column_length) for column in df_pinkey: column_length = max(df_pinkey[column].astype(str).map(len).max(), len(column)) col_idx = df_pinkey.columns.get_loc(column) writer.sheets['Pinkey'].set_column(col_idx, col_idx, column_length) writer.save()