Python openpyxl 错误

Python openpyxl error

我想使用 Python 打开一个文件并将查询结果从 Oracle 粘贴到特定的 sheet。我找到了一种使用 xlswriter 的方法,但这不是完成这项工作的正确工具。

我可以让我的查询执行并追加到列表中。结果中有字符串和整数。我无法将其传输到 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)

也许 openpyxl 不会将可迭代对象(看起来要传递的内容)转换为 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.

呵呵.

解决了以下代码的问题:

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