使用Python自动调整Excel工作表的所有列

时间:2014-06-03 19:36:20

标签: python excel

有没有人知道任何Python - > Excel库包含一个方法来自动确定给定Excel文件名的工作表中的所有列?

2 个答案:

答案 0 :(得分:2)

您可以使用pywin32库:

from win32com.client import Dispatch

excel = Dispatch('Excel.Application')
wb = excel.Workbooks.Open("D:\\output.xlsx")

#Activate second sheet
excel.Worksheets(2).Activate()

#Autofit column in active sheet
excel.ActiveSheet.Columns.AutoFit()

#Save changes in a new file
wb.SaveAs("D:\\output_fit.xlsx")

#Or simply save changes in a current file
#wb.Save()

wb.Close()

答案 1 :(得分:0)

您可能对styleframe感兴趣。

from styleframe import StyleFrame, Styler, utils

sf2 = StyleFrame(<your dataframe>,styler_obj=Styler(bg_color=None, bold=False, font='Arial', font_size=10.0, font_color=None, number_format='General', protection=False, underline=None ,border_type='thin', horizontal_alignment='left', vertical_alignment='center', wrap_text=True, shrink_to_fit=True, fill_pattern_type='solid', indent=0.0, comment_author=None, comment_text=None, text_rotation=0))

sf2.to_excel(sf2,index=False,float_format='%.2f',sheet_name="Sheet1",best_fit=list(df2.columns.values))

更多信息:

https://styleframe.readthedocs.io/en/latest/installation.html