Managing Rows and Columns
When working with spreadsheets, you often have to add or remove rows and columns.
openpyxl makes this easy by adding a set of methods to the sheet class:
They can each take two arguments: an index to perform the insert or delete, and then how many rows to insert or delete. So, let’s go back to the hello world! example and mess around with these new methods. I’m going to open up bpython, I’m going to
then print the
row. So if you go to
print_rows(), you should see
('hello', 'world!'), again. Note that all the changes we did in previous videos were not saved into this workbook, so it’s still like it’s always been. All right.
Keep in mind that inserting occurs before the index, so just like if you were to highlight a cell in Excel and insert by right-clicking. So, when you did this line of code here and inserted three rows at index
1, it put those right at the start here.
02:55 So the first three rows are the new rows and the fourth row is the original one that was added. All right, now that you can handle managing your rows and columns, let’s move on to managing individual sheets.
Become a Member to join the conversation.