6

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

jbochi
  • 28,816
  • 16
  • 73
  • 90

3 Answers3

4

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
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • Hi, thanks for your answer, but my server is running on Linux so I am unable to use the COM. :-( – jbochi Dec 22 '09 at 21:16
  • 1
    @jbochi: No problem. You may want to update the question with this additional piece of information. – mechanical_meat Dec 22 '09 at 21:29
  • this is not working for me. please help me out . i am getting this error xl = DispatchEx("Excel.Application") File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 113, in DispatchEx dispatch = pythoncom.CoCreateInstanceEx(clsid, None, clsctx, serverInfo, (pythoncom.IID_IDispatch,))[0] pywintypes.com_error: (-2147221005, 'Invalid class string', None, None) – Rajiv Sharma Jun 06 '16 at 09:28
2

I found this message in a Google group. It looks like it's not possible, unfortunately.

jbochi
  • 28,816
  • 16
  • 73
  • 90
2

I have the same issue, running a linux server.

i'm going to check creating an ODS or XLSX file with auto-filter by other means, and then convert them with a libreoffice command line to "xls".

Berry Tsakala
  • 15,313
  • 12
  • 57
  • 80