Python pandas 保存Excel自动调整列宽的方法及示例代码
方法1. 用pd.ExcelWriter 和 worksheet.set_column 需要安装xlsxwriter
方法2:使用StyleFrame自动调整
from styleframe import StyleFrame import pandas as pd columns = ['aaaaaaaaaaa', 'bbbbbbbbb', 'ccccccccccc', ] df = pd.DataFrame(data={ 'aaaaaaaaaaa': [1, 2, 3, ], 'bbbbbbbbb': [1, 1, 1, ], 'ccccccccccc': [2, 3, 4, ], }, columns=columns, ) excel_writer = StyleFrame.ExcelWriter('example.xlsx') sf = StyleFrame(df) sf.to_excel( excel_writer=excel_writer, best_fit=columns, columns_and_rows_to_freeze='B2', row_to_add_filters=0, ) excel_writer.save()
参考:
https://www.cjavapy.com/article/2287/
https://blog.csdn.net/weixin_42098295/article/details/121009446
https://blog.csdn.net/weixin_46279624/article/details/127856542
https://blog.csdn.net/qq_39691492/article/details/119757362