简体   繁体   English

使用 openpyxl 在现有 excel 文件中写入一列

[英]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.

相关问题 使用 Openpyxl 从特定列和行开始的现有工作表中写入现有的 excel 文件 - Write to an existing excel file using Openpyxl starting in existing sheet starting at a specific column and row 使用openpyxl读写多个excel数据到一个excel文件 - Read and Write multiple excel data into one excel file using openpyxl 使用openpyxl将工作表追加到现有的Excel文件中 - Append a sheet to an existing excel file using openpyxl 在 Python 中使用 Openpyxl 修改现有 Excel 文件 - Modify an existing Excel file using Openpyxl in Python 如何使用 openpyxl 将一个 excel 文件的列值与 Python 中另一个 excel 文件的列值进行比较? - How to compare column values of one excel file to the column values of another excel file in Python using openpyxl? 如何在不破坏openpyxl公式的情况下写入现有的excel文件? - How to write to an existing excel file without breaking formulas with openpyxl? 如何使用 Openpyxl 更新现有的 Excel.xlsx 文件? - How to update existing Excel .xlsx file using Openpyxl? 在 openpyxl 中使用 add_table() 方法会破坏现有的 excel 文件 - Using add_table() method in openpyxl corrupts an existing excel file Openpyxl - 如何在 Python 中从 Excel 文件中仅读取一列? - Openpyxl - How to read only one column from Excel file in Python? 如何使用 openpyxl 将 csv 文件写入 Excel 工作表? - How can i write a csv file to an excel sheet using openpyxl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM