Reputation: 79
Using openpyxl==3.0.4.
I am looking for a way to insert rows with data describing the columns using a dict similar to what we can do with append. For example, I can do this;
ws.append({2: 4495, 3: 'Chicago, IL (Trial)', 4: 'Cloud', 5: 'US', 6: None, 7: 'Chicago Area', 8: None, 9: None, 10: None, 11: None, 12: 'X', 13: None})
but this will append the rows to the bottom of the worksheet. In can insert empty rows specifying the row at which to insert the rows above, but I don't seem to be able to provide the data.
Basically I am looking for something to insert this data with at given row:
{2: 4495, 3: 'Chicago, IL (Trial)', 4: 'Cloud', 5: 'US', 6: None, 7: 'Chicago Area', 8: None, 9: None, 10: None, 11: None, 12: 'X', 13: None}
{2: 4497, 3: 'Ashburn, VA (Trial)', 4: 'Cloud', 5: 'US', 6: None, 7: 'Ashburn Area', 8: None, 9: None, 10: None, 11: None, 12: 'X', 13: None}
Upvotes: 1
Views: 8369
Reputation: 19430
There is not a built-in way to do this that I know of, but you can just insert_rows
and a simple loop will get you there:
data = {2: 4495, 3: 'Chicago, IL (Trial)', 4: 'Cloud', 5: 'US', 6: None, 7: 'Chicago Area', 8: None, 9: None, 10: None, 11: None, 12: 'X', 13: None}
ws.insert_rows(start_row)
for col, value in data.items():
ws.cell(row=start_row, column=col, value=value)
Upvotes: 2
Reputation: 79
I found a bit of a hack looking at the source code to set the internal attribute _current_row
to my insert point;
worksheet._current_row = start_row
Would be nice if we could support this natively
Upvotes: 2