close

how to append data using openpyxl python to excel file from a specified row?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about how to append data using openpyxl python to excel file from a specified row in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

how to append data using openpyxl python to excel file from a specified row?

  1. how to append data using openpyxl python to excel file from a specified row?

    openpyxl has many different methods to be precise but ws.append in previous answers is strong enough to answer your demands.

  2. append data using openpyxl python to excel file from a specified row

    openpyxl has many different methods to be precise but ws.append in previous answers is strong enough to answer your demands.

Method 1


openpyxl
 has many different methods to be precise but ws.append in previous answers is strong enough to answer your demands. Consider you have written your data to a new 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:

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)

Method 2

Try using:

sheet.max_row 

It will return the last row value, you can start writing the new values from there:

max = ws.max_row
for row, entry in enumerate(data1, start=1):
   st.cell(row=row+max, column=1, value=entry)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read