insert rows

1,865 views
Skip to first unread message

Aron Kisdi

unread,
Nov 10, 2011, 6:50:41 AM11/10/11
to openpyx...@googlegroups.com
Hello,
 
Background
I'm working on a fairly big project using openpyxl. It will take some time to get to a working stage but hopefully it will be an interesting example/reference for other projects to use.
The plan is to be able to exchange data (input/output) between many specialist worksheets (subsystems) so one subsystem can use inputs from another.
 
Problem
I'm trying to add a sheet with log of inputs to each subsystem (specialist spreadsheet). I would like to organise the log depending on which other subsystem is the input coming from. Now when a new input is added I want to add it to the top so the user can see the latest addition first. Currently I would need to shift all cells below the new addition down one cell and also modify named ranges accordingly.
 
Question
Is there any way to insert a row, like you would insert a row in Excel (right click insert) so the cells below that row updated automatically?
 
Many Thanks,
 
Aron

Eric

unread,
Nov 10, 2011, 7:06:54 AM11/10/11
to openpyx...@googlegroups.com
Hi Aron,
well, as of now, there is no way to just insert a row at a specific index, like you would do in Excel. Actually you're the first to ask for such a feature :)

However, it does not seem to me too complex to add it (basically using what you're already doing), as long as you don't have too many rows to move (as I suspect it would be a O(n) operation).

If you have a workaround for now, I suggest you file a ticket on the bugtracker and I (or some other contributor) will take care of it, or you can also give it a try yourself ;-)

Cheers,
Eric

2011/11/10 Aron Kisdi <kis...@gmail.com>

Aron Kisdi

unread,
Nov 10, 2011, 8:04:23 AM11/10/11
to openpyx...@googlegroups.com
Hi Eric,
 
Ok, added a ticket. I will see if I can come up with a good way of doing this.
 
Is there an efficient way to find the last cell which contains a value (last none-empty cell) in a row or column?
 
Thanks,
 
Aron

Brent Hoover

unread,
Nov 10, 2011, 8:06:40 AM11/10/11
to openpyx...@googlegroups.com
Hi Aron,

Just so I understand your need a little better, you want to open a workbook, grab a particular worksheet, insert a row at the top and then resave the workbook?

Eric is right that this conversation is probably better off captured in an enhancement request though. If you add it I will try and see if it's something I could take care of this weekend. Also it sounds like your sheets may be fairly large so if you could include that as well it may affect the type of operation that makes sense. (e.g. a generator rather than a standard iterator).

Brent Hoover
Computer Scientist

Eric

unread,
Nov 10, 2011, 8:18:44 AM11/10/11
to openpyx...@googlegroups.com

Aron Kisdi

unread,
Nov 10, 2011, 8:24:22 AM11/10/11
to openpyx...@googlegroups.com
Hi Brent,
 
Thanks for the reply. Added a tiket for this: #83
 
To clarify:
I wan't to open a workbook. grab a particular worksheet. Find under which title I need to insert the new row (this is ok, it can be done easily multiple ways e.g.: with named ranges). The title is jsut a cell with s string e.g.: "From SubsystemX". Insert a row below. Write some values to that row. Resave workbook. So what you said exept the new row is not nescesarily the first one.
 
Eric, thanks!
 
Thanks,
 
Aron

sarika...@gmail.com

unread,
Jul 15, 2017, 10:26:36 AM7/15/17
to openpyxl-users
Hello,

Even I am looking for the solution to insert a row or a column in excel through openpyxl code. I fsomeone can suggest the logic, it will be helpfull.

Regards,
Sarika.

alex.ro...@gmail.com

unread,
Sep 30, 2018, 1:26:03 PM9/30/18
to openpyxl-users
Hello, my name is Alex, and I just wrote a module on inserting/deleting rows and columns with openpyxl (I don't know if you're still having this problem but wanted to include it). Openpyxl does not have this feature directly, but this should simulate the effect.

Charlie Clark

unread,
Sep 30, 2018, 1:52:07 PM9/30/18
to openpyx...@googlegroups.com
Am .09.2018, 19:26 Uhr, schrieb <alex.ro...@gmail.com>:

> Hello, my name is Alex, and I just wrote a module on inserting/deleting
> rows and columns with openpyxl (I don't know if you're still having this
> problem but wanted to include it). Openpyxl does not have this feature
> directly, but this should simulate the effect.

Hi Alex,

thanks, but can you *can* insert and delete rows and columns with openpyxl
2.5

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

alex.ro...@gmail.com

unread,
Sep 30, 2018, 1:56:24 PM9/30/18
to openpyxl-users
Ah okay. Well that's good. I guess it'll help for earlier versions :D

Charlie Clark

unread,
Sep 30, 2018, 2:08:10 PM9/30/18
to openpyx...@googlegroups.com
Am .09.2018, 19:56 Uhr, schrieb <alex.ro...@gmail.com>:

> Ah okay. Well that's good. I guess it'll help for earlier versions

Maybe, but it has no tests so I couldn't recommend it. I suspect it's also
a bit slow on large worksheets.
Reply all
Reply to author
Forward
0 new messages