[英]Write a one column in existing excel file using openpyxl
I want to write the values of the list values
only on column "F" of the existing excel file, for example:我只想将列表values
的值写入现有 excel 文件的“F”列,例如:
values = [5, 7, 1]
wb = Workbook(write_only=True)
ws = wb.create_sheet()
items = [i for i in values]
for item in items:
ws.append([item])
wb.save('newfile.xlsx')
ID Name View #RightSwipe #LeftSwipe
145 abc 5 2 1
146 xyz 8 3 6
147 pqr 3 4 3
add one column in last that's name "Order"在最后添加一列名为“订单”
Order
5
7
1
append
adds new rows at the end of the file. append
在文件末尾添加新行。 You want to add data to existing rows.您想要将数据添加到现有行。 Simply iterate over the list and add the values in their respective rows, at the last column:只需遍历列表并在最后一列添加各自行中的值:
ws.cell(row=1, column=ws.max_column+1, value="Order")
for row_num, value in enumerate(values, start=2):
ws.cell(row=row_num, column=ws.max_column, value=value)
You can also use the iter_rows
to iterate the rows while zip
ping the values:您还可以在zip
ping 值时使用iter_rows
迭代行:
for row, value in zip(ws.iter_rows(min_row=2, min_col=ws.max_column, max_col=ws.max_column), values):
row[0].value = value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.