4

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")
0

2 Answers 2

4

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
  • Thanks to both alecxe and mgrant. Both answers got me on the right track. There is one small correction to the suggestion above. Using extend required the following syntax: masterList1.extend([newList]). When not using brackets my values were broken into individual characters, rather than full cell values.
    – R Fuller
    Jul 7, 2015 at 2:39
  • @RFuller extend takes a sequence as a parameter. Strings are sequences of individual characters. This is a common gotcha in Python. Jul 7, 2015 at 8:06
-1

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.

2
  • 1
    openpyxl worksheets are not lists. They do have an append method which is similar to the append method for lists but has specific syntax and semantics. The exception is raised when trying to assign a list to a single cell. Jul 6, 2015 at 7:06
  • @CharlieClark, you are right. I meant to say that extend was needed in the sense that @alecxe pointed out, when building masterList1, etc. But I agree that the assignment was the real problem.
    – mgrant
    Jul 7, 2015 at 1:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.