如何附加列而不是行openpyxl?
How to append columns instead of rows openpyxl?
目前正在使用 Openpyxl。我知道 append 会在 excel 行中打印输出。我想弄清楚是否有一个函数可以在列中打印输出.eg:
headings = ['Name','Fruits']
ws.append(headings)
Name = ['John','Ben','Lily','Sarah']
Fruits = ['Orange','Apple','Grape','Peach']
excel中的输出:
A B
1 Name Fruits
2 John Orange
3 Ben Apple
4 Lily Grape
5 Sarah Peach
显示了您要查找的信息here。该示例显示了如何遍历列和行,这最终只是简单的 grid/matrix 逻辑。
from itertools import cycle
c = cycle([1, 2])
for idx in range(len(Name)):
ws.cell(column=next(c), row=idx, value=Name[idx])
ws.cell(column=next(c), row=idx, value=Fruits[idx])
# Column 1, Row 0 John
# Column 2, Row 0 Orange
# Column 1, Row 1 Ben
# Column 2, Row 1 Apple
# etc...
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
Name = ['John','Ben','Lily','Sarah']
Fruits = ['Orange','Apple','Grape','Peach']
data=zip(Name,Fruits)
ws.append(['Name','Fruits'])
[ws.append(row_i) for row_i in list(data) ]
wb.save('fileaname')
目前正在使用 Openpyxl。我知道 append 会在 excel 行中打印输出。我想弄清楚是否有一个函数可以在列中打印输出.eg:
headings = ['Name','Fruits']
ws.append(headings)
Name = ['John','Ben','Lily','Sarah']
Fruits = ['Orange','Apple','Grape','Peach']
excel中的输出:
A B
1 Name Fruits
2 John Orange
3 Ben Apple
4 Lily Grape
5 Sarah Peach
显示了您要查找的信息here。该示例显示了如何遍历列和行,这最终只是简单的 grid/matrix 逻辑。
from itertools import cycle
c = cycle([1, 2])
for idx in range(len(Name)):
ws.cell(column=next(c), row=idx, value=Name[idx])
ws.cell(column=next(c), row=idx, value=Fruits[idx])
# Column 1, Row 0 John
# Column 2, Row 0 Orange
# Column 1, Row 1 Ben
# Column 2, Row 1 Apple
# etc...
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
Name = ['John','Ben','Lily','Sarah']
Fruits = ['Orange','Apple','Grape','Peach']
data=zip(Name,Fruits)
ws.append(['Name','Fruits'])
[ws.append(row_i) for row_i in list(data) ]
wb.save('fileaname')