I'm looking for the best approach for inserting a row into a spreadsheet using openpyxl.
Effectively, I have a spreadsheet (Excel 2007) which has a header row, followed by (at most) a few thousand rows of data. I'm looking to insert the row as the first row of actual data, so after the header. My understanding is that the append function is suitable for adding content to the end of the file.
Reading the documentation for both openpyxl and xlrd (and xlwt), I can't find any clear cut ways of doing this, beyond looping through the content manually and inserting into a new sheet (after inserting the required row).
Given my so far limited experience with Python, I'm trying to understand if this is indeed the best option to take (the most pythonic!), and if so could someone provide an explicit example. Specifically can I read and write rows with openpyxl or do I have to access cells? Additionally can I (over)write the same file(name)?
Adding an answer applicable to more recent releases, v2.5+, of
openpyxl
:There's now an
insert_rows()
andinsert_cols()
.Answering this with the code that I'm now using to achieve the desired result. Note that I am manually inserting the row at position 1, but that should be easy enough to adjust for specific needs. You could also easily tweak this to insert more than one row, and simply populate the rest of the data starting at the relevant position.
Also, note that due to downstream dependencies, we are manually specifying data from 'Sheet1', and the data is getting copied to a new sheet which is inserted at the beginning of the workbook, whilst renaming the original worksheet to 'Sheet1.5'.
EDIT: I've also added (later on) a change to the format_code to fix issues where the default copy operation here removes all formatting:
new_cell.style.number_format.format_code = 'mm/dd/yyyy'
. I couldn't find any documentation that this was settable, it was more of a case of trial and error!Lastly, don't forget this example is saving over the original. You can change the save path where applicable to avoid this.
To insert row into Excel spreadsheet using openpyxl in Python
Below code can help you :-
For inserting column also openpyxl have similar function i.e.insert_cols(idx, amount=1)
Openpyxl Worksheets have limited functionality when it comes to doing row or column level operations. The only properties a Worksheet has that relates to rows/columns are the properties
row_dimensions
andcolumn_dimensions
, which store "RowDimensions" and "ColumnDimensions" objects for each row and column, respectively. These dictionaries are also used in function likeget_highest_row()
andget_highest_column()
.Everything else operates on a cell level, with Cell objects being tracked in the dictionary,
_cells
(and their style tracked in the dictionary_styles
). Most functions that look like they're doing anything on a row or column level are actually operating on a range of cells (such as the aforementionedappend()
).The simplest thing to do would be what you suggested: create a new sheet, append your header row, append your new data rows, append your old data rows, delete the old sheet, then rename your new sheet to the old one. Problems that may be presented with this method is the loss of row/column dimensions attributes and cell styles, unless you specifically copy them, too.
Alternatively, you could create your own functions that insert rows or columns.
I had a large number of very simple worksheets that I needed to delete columns from. Since you asked for explicit examples, I'll provide the function I quickly threw together to do this:
I pass it the worksheet that I'm working with, and the column number I want deleted, and away it goes. I know it isn't exactly what you wanted, but I hope this information helped!
EDIT: Noticed someone gave this another vote, and figured I should update it. The co-ordinate system in Openpyxl experienced some changes sometime in the passed couple years, introducing a
coordinate
attribute for items in_cell
. This needs to be edited, too, or the rows will be left blank (instead of deleted), and Excel will throw an error about problems with the file. This works for Openpyxl 2.2.3 (untested with later versions)