2017-07-07 49 views
0

我是編程新手。 我正在學習用python語言編寫。我目前正試圖將現有的字典放入一個新的excel文件中。我有一種感覺,因爲我有一本字典可能是問題? 我使用的是openpyxl。openpyxl的問題:試圖將字典插入新的excel文件

每當我嘗試插入字典時,都會收到一條錯誤消息(AttributeError:'WriteOnlyWorksheet'對象沒有屬性'單元格')。不過,我可以寫一個沒有問題的簡單單元格。 這是當試圖填充大量數據到列中,我得到的錯誤。

#Writing a new excel file with key information 
    print("Would you like to create a new excel document?") 
    b_input = input("Type: 'Y' or 'N' -> ").lower() 
    if b_input == "y": 
     wb = Workbook(write_only=True) 
     ws = wb.create_sheet() 

     print("I can only put in the contract status and serial numbers together in a file.") 
     c_input = input("Would you like to do that? Type: 'Y' or 'N' -> ") 
     if c_input == 'y': 
      ws.cell(row=r, column=1).value = excel_info 


    wb.save('new_test_book.xlsx') 
+0

我沒有,我會看看! 編輯:對象沒有屬性寫入 :P –

回答

0

write_only mode.docs

In a write-only workbook, rows can only be added with append(). It is not possible to write (or read) cells at arbitrary locations with cell() or iter_rows().

這就是爲什麼錯誤告訴你,沒有屬性細胞因此使用append()代替。

您也可以參考此topic以獲得更多幫助和理解。

希望這有幫助。

+1

非常感謝你!這對我很有幫助 –

+0

完全沒問題,希望我可以幫助更多@JulianSilvestri –