Python openpyxl write list to Excel error

扶醉桌前 提交于 2019-12-06 05:56:01

问题


I am new to Python and working on my first project. I am trying to get my code to copy columns of data from one spreadsheet and append it to data that currently exists in a master sheet. I am able to capture the data in each of the sheets and create a new master list which combines both data sets, but I am having trouble writing it to a file. When I test print the combined lists they appear correct, but when I add the code to write the lists to a file it gets hung up.

Any assistance you can provide will be extremely helpful!

Below is my code. Here's the error I'm getting

Traceback (most recent call last): File "/Path/Path/Path/extractData.py", line 50, in destSheet.cell(row=rowNum, column=1).value = masterList1

File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/openpyxl/cell/cell.py", line 313, in value self._bind_value(value)

File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/openpyxl/cell/cell.py", line 217, in _bind_value raise ValueError("Cannot convert {0} to Excel".format(value))

ValueError: Cannot convert ['Reinsurer', 'Market 1', 'Market 2', 'Market 3', 'Market 4', 'Market 5', 'Market 1', 'Market 2', 'Market 3', 'Market 4', 'Market 5', ['Market 1', 'Market 2', 'Market 3', 'Market 4', 'Market 5']] to Excel

import openpyxl

#Open source data file
sourceFile = openpyxl.load_workbook('/Path/Path/Path/testAuth.xlsx')
sourceSheet = sourceFile.get_sheet_by_name('Sheet1')

#read data from source file and create lists of new data
newList1 = []
newList2 = []
newList3 = []

for row in range(2, sourceSheet.get_highest_row()):
    data1 = sourceSheet['A' + str(row)].value
    newList1.append(data1)
    data2 = sourceSheet['B' + str(row)].value
    newList2.append(data2)
    data3 = sourceSheet['C' + str(row)].value
    newList3.append(data3)

#open destination workbook that includes the master database
destFile = openpyxl.load_workbook('/Path/Path/Path/testHist.xlsx')
destSheet = destFile.get_sheet_by_name('Sheet1')

#create empty lists for copying the historical data already in the workbook
masterList1 = []
masterList2 = []
masterList3 = []

#grab master spreadsheet data and write to list
for row in range(1, destSheet.get_highest_row()+1):
    masterData1 = destSheet['A'+ str(row)].value
    masterList1.append(masterData1)
    masterData2 = destSheet['B'+ str(row)].value
    masterList2.append(masterData2)
    masterData3 = destSheet['C'+ str(row)].value
    masterList3.append(masterData3)

#append new data to the history list
masterList1.append(newList1)
masterList2.append(newList2)
masterList3.append(newList3)

#write new master list to a new file
for rowNum in range(2, len(masterList1)):
    destSheet.cell(row=rowNum, column=1).value = masterList1

destSheet.save("updatedTest.xlsx")

回答1:


Instead of assigning the value of each cell to masterList1, you probably meant to use the masterList1[rowNum] as a value:

for rowNum in range(2, len(masterList1)):
    destSheet.cell(row=rowNum, column=1).value = masterList1[rowNum]

Plus, as @mgrant pointed out, you should've extended (not appended) the master list previously:

masterList1.extend(newList1)



回答2:


Have you tried using extend instead of append? append adds a single item to the end of a list, whereas extend takes a list argument and adds each item.



来源:https://stackoverflow.com/questions/31236282/python-openpyxl-write-list-to-excel-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!