openpyxl支持自动调整Excel列宽吗?
import pandas as pdfrom openpyxl import load_workbook
df = pd.DataFrame({'档案号':['0092018360733000001','0092018360733000005','0092018360733000008','0092018360733000011','0092018360733000012','0092018360733000015'],
'姓名':['肖健','糜修尘','兰希航','周杨子','李梦雯','陈领'],
'级别':['三级', '三级', '三级', '三级', '三级', '二级'],
'专业':['三级英语笔译', '三级英语笔译', '三级英语笔译', '三级英语笔译', '三级英语笔译', '二级英语笔译'],
'工作单位':['江西理工大学','自由职业','无','南昌大学','无','江西财经大学']})
writer = pd.ExcelWriter(r'D:\test\录取名单.xlsx')
df.to_excel(writer, '明细', index=0)
writer.save()
wb = load_workbook(filename = r'D:\test\录取名单.xlsx')
sht = wb["明细"]
sht.dimensions.ColumnDimension(bestFit=True)
wb.save(r'D:\test\录取名单.xlsx')
# 我希望实现自动调整列宽,代码执行过程中报错,请指教,谢谢。
# https://openpyxl.readthedocs.io/en/stable/api/openpyxl.worksheet.dimensions.html
# class openpyxl.worksheet.dimensions.ColumnDimension(worksheet, index='A', width=0, bestFit=False,
# hidden=False, outlineLevel=0, outline_level=None, collapsed=False, style=None, min=None, max=None,
# customWidth=False, visible=None, auto_size=None) 设置自动适应的那句应该是
openpyxl.worksheet.dimensions.ColumnDimension(表格,bestFit=True)
#表格是所需要的设置的表格
页:
[1]