Appending values

In this section, we are going to append values in Excel. For that, we are going to use the append() method. We can add a group of values at the bottom of the current sheet in which we want to put the values. Create a script called append_values.py and write the following content in it:

from openpyxl import Workbook 

book_obj = Workbook()
excel_sheet = book_obj.active
rows = (
(11, 12, 13),
(21, 22, 23),
(31, 32, 33),
(41, 42, 43)
)
for values in rows:
excel_sheet.append(values)
print()

print("values are successfully appended")
book_obj.save('test.xlsx')wb.save('append_values.xlsx')

Run the script and you will get the following output:

student@ubuntu:~/work$ python3 append_values.py

Following is the output:

values are successfully appended

In the preceding example, we appended three columns of data in the append_values.xlsx files sheet. The data we stored was in a tuple of tuples and to append that data we went through the container row by row and inserted it using the append() method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset