2009-12-22 13 views

回答

4

AFAIK xlwt不允許您添加過濾器。

但是,您可以使用Mark Hammond's Python Win32 Extensions添加過濾器。下載2.6 here
像這樣的東西應該工作(在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 
+0

嗨,謝謝你的回答,但我的服務器在Linux上運行,所以我無法使用COM。 :-( – jbochi 2009-12-22 21:16:44

+1

@jbochi:沒問題,你可能想用這個附加信息更新這個問題 – bernie 2009-12-22 21:29:55

+0

這不適用於我,請幫助我,我得到這個錯誤xl = DispatchEx(「Excel.Application 「) DispatchEx dispatch = pythoncom.CoCreateInstanceEx(clsid,None,clsctx,serverInfo,(pythoncom.IID_IDispatch,第123行)中的文件」C:\ Python27 \ lib \ site-packages \ win32com \ client \ __ init__.py「 ))[0] pywintypes.com_error:(-2147221005,'無效的類字符串',None,None) – 2016-06-06 09:28:22

2

我有同樣的問題,運行Linux服務器。

我要檢查創建ODS或XLSX文件與自動過濾器通過其他方式,然後將它們與libreoffice命令行轉換爲「xls」。

+1

您可以使用XlsxWriter添加自動篩選器。請參閱此示例(http://xlsxwriter.readthedocs.org/en/)。 latest/example_autofilter.html#ex-autofilter)。 – jmcnamara 2013-12-30 21:02:40

+0

@jmcnamara SORRY此頁面不存在。 – 2016-06-06 09:30:03

+0

請嘗試此鏈接:https://xlsxwriter.readthedocs.io/working_with_autofilters.html – jmcnamara 2016-06-06 10:16:52

相關問題