pu369com

Python pandas 保存Excel自动调整列宽的方法及示例代码

方法1.   用pd.ExcelWriter 和  worksheet.set_column  需要安装xlsxwriter

方法2:使用StyleFrame自动调整

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

 

posted on   pu369com  阅读(2209)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示