Python openpyxl 写入列表到 Excel 错误
Python openpyxl write list to Excel error
我是 Python 的新手,正在做我的第一个项目。我正在尝试让我的代码从一个 spreadsheet 复制数据列并将其附加到主 sheet 中当前存在的数据。我能够捕获每个 sheet 中的数据并创建一个结合了两个数据集的新主列表,但我无法将其写入文件。当我测试打印组合列表时,它们看起来是正确的,但是当我添加代码以将列表写入文件时,它被挂断了。
您能提供的任何帮助都将非常有帮助!
下面是我的代码。这是我收到的错误
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")
您是否尝试过使用 extend
而不是 append
? append
将单个项目添加到列表末尾,而 extend
采用列表参数并添加每个项目。
您可能打算将 masterList1[rowNum]
用作值,而不是将每个单元格的值分配给 masterList1
:
for rowNum in range(2, len(masterList1)):
destSheet.cell(row=rowNum, column=1).value = masterList1[rowNum]
此外,正如@mgrant 指出的那样,您之前应该扩展(而不是附加)主列表:
masterList1.extend(newList1)
我是 Python 的新手,正在做我的第一个项目。我正在尝试让我的代码从一个 spreadsheet 复制数据列并将其附加到主 sheet 中当前存在的数据。我能够捕获每个 sheet 中的数据并创建一个结合了两个数据集的新主列表,但我无法将其写入文件。当我测试打印组合列表时,它们看起来是正确的,但是当我添加代码以将列表写入文件时,它被挂断了。
您能提供的任何帮助都将非常有帮助!
下面是我的代码。这是我收到的错误
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")
您是否尝试过使用 extend
而不是 append
? append
将单个项目添加到列表末尾,而 extend
采用列表参数并添加每个项目。
您可能打算将 masterList1[rowNum]
用作值,而不是将每个单元格的值分配给 masterList1
:
for rowNum in range(2, len(masterList1)):
destSheet.cell(row=rowNum, column=1).value = masterList1[rowNum]
此外,正如@mgrant 指出的那样,您之前应该扩展(而不是附加)主列表:
masterList1.extend(newList1)