Python openpyxl错误
问题描述:
我想使用Python打开一个文件并将查询结果从Oracle中粘贴到特定工作表中。我找到了一种与xlswriter一起使用的方法,但这不是该工作的正确工具。Python openpyxl错误
我可以让我的查询执行并追加到列表中。结果中我有两个字符串和整数。我无法将其转移到Excel文件中。任何帮助都会很棒。
我得到的错误是:
line 201, in _bind_value
raise ValueError("Cannot convert {0} to Excel".format(value))
ValueError: Cannot convert ('20 GA GV-CS-CT-DRY-G90 60xC COIL', 2, 848817, 982875, 1.15793510261929) to Excel
代码:
import cx_Oracle
import openpyxl
con = cx_Oracle.connect('example', 'example', "example")
cur = con.cursor()
heatmap_data = []
statement = """ select * from example"""
cur.arraysize = 2000
cur.execute(statement)
for result in cur:
heatmap_data.append(result)
con.close()
file = "path/Test.xlsx"
wb = openpyxl.load_workbook(filename=file)
ws = wb.get_sheet_by_name("Sheet1")
row = 1
col = 1
for rowNum in range(2, len(heatmap_data)):
ws.cell(row=row, column=col).value = heatmap_data[rowNum]
row =+ 1
wb.save(file)
答
解决该问题与下面的代码:
row = 1
for i in (heatmap_data):
print(i[0], i[1], i[2], i[3], i[4])
ws.cell(row=row, column=1).value = (i[0])
ws.cell(row=row, column=2).value = (i[1])
ws.cell(row=row, column=3).value = (i[2])
ws.cell(row=row, column=4).value = (i[3])
ws.cell(row=row, column=5).value = (i[4])
row += 1
答
也许openpyxl不转换iterables(这是什么样子传递)到ws.cell.value
。
尝试:for rowNum, data in enumerate(heatmap_data): ws.cell(row=rowNum + 2, column=col).value = ", ".join(data[rowNum]) # Noticed the range you're choosing skips the first 2 values of your data. # Looks like you're not incrementing the column. Meh. Guess not.
嘿。
并不需要打印()命令。 –