2017-08-16 78 views
0

設置:我使用的筆記本jupyter,Python版本3.6.2,和Excel版本15.36如何複製列表的內容脫穎而出

任務:我創建了一個列表,並希望它的內容,以複製到一列空白Excel文件的第一行。

這裏是我的代碼:

for rowOfCellObjects in strauss_sheet(1, list_length): 
    for cellObj in rowOfCellObjects: 
     for item in noreplist: 
      sheet.cellObj(row=1, column=colNum).value = item 

我得到一個錯誤,因爲「工作表」對象是不可調用的。 我有我的列表長度存儲在list_length,我的列表是noreplist

我是python的新手,很想聽聽執行此任務的好方法。

+0

你使用[openpyxl](http://pypi.python.org/pypi/openpyxl),你更喜歡使用特定的python包來做到這一點嗎? – davedwards

+0

@downshift是的,我使用的是完美工作的openpyxl – user101981

回答

0

這可以很容易地與xlwt來完成:

import xlwt 

wb = xlwt.Workbook() 
ws = wb.add_sheet('Sheet 1') 

listdata = ['write', 'list', 'to', 'excel', 'row'] 

first_row = 0 

# write each item in the list to consecutive columns on the first row 
for index, item in enumerate(listdata): 
     ws.write(first_row, index, item) 

wb.save('example.xls') 

產生的excel文件,其內容:

write list to excel row

希望這有助於。

+0

!謝謝您的幫助! – user101981