Search code examples
pythonexcelxlwtpyexcelerator

How to create an excel file with an autofilter in the first row with xlwt?


I am using Python 2.6 + xlwt module to generate excel files.

Is it possible to include an autofilter in the first row with xlwt or pyExcelerator or anything else besides COM?

Thanks


Solution

  • AFAIK xlwt doesn't allow you to add a filter.

    However you can add a filter using Mark Hammond's Python Win32 Extensions. Download for 2.6 here.
    Something like this should work (tested in Python 2.5.4):

    from win32com.client import DispatchEx
    xl = DispatchEx("Excel.Application")
    xl.Workbooks.Open("c:/excel_file.xls")
    xl.ActiveWorkbook.ActiveSheet.Columns(1).AutoFilter(1)
    xl.ActiveWorkbook.Close(SaveChanges=1)
    xl.Quit()
    del xl # ensure excel.exe process ends