2015-09-07 49 views
2

我需要創建可以被填充的列的excel。具有過濾列的excel文件

寧願使用openpyxl,因爲我已經使用StyleFrame來設計excel。

example

+0

過濾器受openpyxl支持。 –

+1

class openpyxl.worksheet.filters.AutoFilter [link](https://openpyxl.readthedocs.org/en/2.0/openpyxl.worksheet.html#module-openpyxl.worksheet.filters) – PythonTester

+0

非常感謝你的工作! – AsafSH

回答

2

下面是使用StyleFrame的解決辦法,我只好用 「row_to_add_filters」 與指數0加入濾光片來列....

from StyleFrame import StyleFrame, colors 

excel_writer = StyleFrame.ExcelWriter('example.xlsx') 
sf = StyleFrame({'a': [1, 2, 3], 'b': [1, 1, 1], 'c': [2, 3, 4]}) 
sf.apply_column_style(cols_to_style=['a', 'c'], protection=True, style_header=True) 
sf.apply_style_by_indexes(indexes_to_style=2, cols_to_style='b', bg_color=colors.yellow, protection=True) 
sf.to_excel(excel_writer=excel_writer, row_to_add_filters=0, columns_and_rows_to_freeze='B2', allow_protection=True) 
excel_writer.save() 
-2

該代碼使用xlwt。 它寫道和colums。

import xlwt 

def writelineSimple(shet,line, lstLine): 
    col=0 
    for elem in lstLine: 
     shet.write(line, col, elem) 
     col=col+1 

def writecolum(shet,line,col, lstLine): 
    for elem in lstLine: 
     shet.write(line, col, elem) 
     line=line+1 

def writeline(shet,line,col, lstLine): 
    for elem in lstLine: 
     shet.write(line, col, elem) 
     col=col+1 


book = xlwt.Workbook(encoding="utf-8") 
sheet1 = book.add_sheet("Sheet 1") 

writelineSimple(sheet1,0,["col1","col2","col3","col4","col5"]) 
writelineSimple(sheet1,1,["writelineSimple1","writelineSimple2"]) 
writeline(sheet1,3,3,["writeline1","writeline2","writeline3"]) 
writecolum(sheet1,5,5,["writecolum1","writecolum2","writecolum3"]) 


book.save("myfirstgeneratedexcel.xls") 

myfirstgeneratedexcel.xls

col1 col2 col3 col4 col5  
writelineSimple1 writelineSimple2     

      writeline1 writeline2 writeline3 

        writecolum1 
        writecolum2 
        writecolum3 
+0

xlwt不支持XLSX文件。 –