Python 如何使用 openpyxl 将列表写入 xlsx
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15004838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a list to xlsx using openpyxl
提问by Thomas Becker
I have a list of some values in Python and want to write them into an Excel-Spreadsheet using openpyxl.
我在 Python 中有一些值的列表,并想使用 openpyxl 将它们写入 Excel 电子表格。
So far I tried, where lstStat is a list of integers that needs to be written to the Excel file:
到目前为止,我尝试过,其中 lstStat 是需要写入 Excel 文件的整数列表:
for statN in lstStat:
for line in ws.range('A3:A14'):
for cell in line:
cell.value(statN)
I'm getting a TypeError: 'NoneType' object is not callable for the last line in the code snipet.
我收到一个 TypeError: 'NoneType' object is not callable for the last line in the code snipet。
Can you help me out how to write my data to the Excel file?
你能帮我解决如何将我的数据写入 Excel 文件吗?
Cheers Thomas
干杯托马斯
采纳答案by John Y
To assign a value to a cell, use =:
要为单元格分配值,请使用=:
cell.value = statN
You also need to fix your loops. Notice that right now, for eachelement in lstStat, you are writing the entire range. Besides not being what you intended, it also is less flexible: What happens if lstStathas more or fewer elements?
您还需要修复循环。请注意,现在,对于 中的每个元素lstStat,您正在编写整个范围。除了不是你想要的,它也不太灵活:如果lstStat有更多或更少的元素会发生什么?
What you want to do is just loop over lstStatand increment the row number as you go. Something like
您想要做的只是循环lstStat并在您进行时增加行号。就像是
r = 3
for statN in lstStat:
ws.cell(row=r, column=1).value = statN
r += 1
You could also use Python's enumeratefunction:
你也可以使用 Python 的enumerate函数:
for i, statN in enumerate(lstStat):
ws.cell(row=i+3, column=1).value = statN
(Note that A1 is referenced as cell(row=1, column=1)as of OpenPyXL version 2.0.0; in earlier versions, A1 was cell(row=0, column=0).)
(请注意,cell(row=1, column=1)从OpenPyXL 版本 2.0.0 开始引用 A1 ;在早期版本中, A1 是cell(row=0, column=0)。)
回答by darrenvba
Openpyxl does not allow you to write lists in excel cells, however if you convert the list into a string you can paste it into excel.
Openpyxl 不允许您在 excel 单元格中编写列表,但是如果您将列表转换为字符串,则可以将其粘贴到 excel 中。
e.g. This would cause an error
例如这会导致错误
my_list = ['1','2','3']
ws.cell(row=r, column=1).value = my_list
my_list = ['1','2','3']
ws.cell(row=r, column=1).value = my_list
However this would paste the string of the list into excel
但是,这会将列表的字符串粘贴到 excel 中
my_list = ['1','2','3']
new_list = str(my_list)
ws.cell(row=r, column=1).value = new_list
my_list = ['1','2','3']
new_list = str(my_list)
ws.cell(row=r, column=1).value = new_list
回答by Sarkis Derderian
from openpyxl import load_workbook,Workbook
wb=load_workbook('Book1.xlsx')
ws1=wb.get_sheet_by_name('Sheet1')
shs=wb.get_sheet_names()
print(type(shs))
# shs is list
for r in range(0,len(shs)):
ws1.cell(row=r+1,column=1).value=shs[r]
wb.save('Book1.xlsx')

