如何使用openpyxl python将数据附加到指定行的excel文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37182528/
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 append data using openpyxl python to excel file from a specified row?
提问by Fuat Can
I have different Python list variables(data1, data2, data3 ect) containing data which I want to put into an already existing excel sheet. Presently My loop goes like this.
我有不同的 Python 列表变量(data1、data2、data3 等),其中包含我想放入现有 Excel 工作表中的数据。目前我的循环是这样的。
for row, entry in enumerate(data1,start=1):
st.cell(row=row, column=1, value=entry)
work.save('sample.xlsx')
for row, entry in enumerate(data2,start=1):
st.cell(row=row, column=2, value=entry)
work.save('sample.xlsx')
for row, entry in enumerate(data3,start=1):
st.cell(row=row, column=3, value=entry)
work.save('sample.xlsx')
for row, entry in enumerate(data4,start=1):
st.cell(row=row, column=4, value=entry)
work.save('sample.xlsx')
for row, entry in enumerate(data5,start=1):
st.cell(row=row, column=5, value=entry)
work.save('sample.xlsx')
for row, entry in enumerate(data6,start=1):
st.cell(row=row, column=6, value=entry)
work.save('sample.xlsx')
for row, entry in enumerate(data7,start=1):
st.cell(row=row, column=7, value=entry)
work.save('sample.xlsx')
Once my Python script runs, It will store the data from the 1st row. If I am again running the script I want the new data to come below the available data
一旦我的 Python 脚本运行,它将存储第一行的数据。如果我再次运行脚本,我希望新数据低于可用数据
How to do so?
怎么做?
回答by Strik3r
Try using
尝试使用
sheet.max_row
it will return the last row value, You can start writing the new values from here.
它将返回最后一行值,您可以从这里开始写入新值。
max = ws.max_row
for row, entry in enumerate(data1,start=1):
st.cell(row=row+max, column=1, value=entry)
Hope it helps.Happy Coding :)
希望它有所帮助。快乐编码:)
回答by Fuat Can
openpyxlhas many different methods to be precise but ws.appendin previous answers is strong enough to answer your demands. Consider you have written your data to a new sample.xlsx:
openpyxl有许多不同的方法,但之前答案中的ws.append足以满足您的需求。考虑您已将数据写入新的sample.xlsx:
from openpyxl.workbook import Workbook
headers = ['Company','Address','Tel','Web']
workbook_name = 'sample.xlsx'
wb = Workbook()
page = wb.active
page.title = 'companies'
page.append(headers) # write the headers to the first line
# Data to write:
companies = [['name1','address1','tel1','web1'], ['name2','address2','tel2','web2']]
for info in companies:
page.append(info)
wb.save(filename = workbook_name)
Now, to append new lines you must first open an existing book with load_workbook:
现在,要追加新行,您必须首先使用load_workbook打开一本现有的书:
from openpyxl import load_workbook
workbook_name = 'sample.xlsx'
wb = load_workbook(workbook_name)
page = wb.active
# New data to write:
new_companies = [['name3','address3','tel3','web3'], ['name4','address4','tel4','web4']]
for info in new_companies:
page.append(info)
wb.save(filename=workbook_name)
回答by Charlie Clark
ws.append()
will always add rows below any existing data.
ws.append()
将始终在任何现有数据下方添加行。
回答by Sarang M K
You can use the append() method to append values to the existing excel files. Example:
您可以使用 append() 方法将值附加到现有的 excel 文件中。例子:
workbook_obj = openpyxl.load_workbook(excelFilePath)
sheet_obj = workbook_obj.active
col1 = 'NewValueCol1'
col2 = 'NewValueCol2'
sheet_obj.append([col1, col2])
workbook_obj.save(excelFilePath)
here the col1 and col2 values will be added with the existing data in the excel sheet.
此处 col1 和 col2 值将与 Excel 工作表中的现有数据一起添加。