deleting a sheet

7,299 views
Skip to first unread message

Phillip M. Feldman

unread,
Dec 2, 2011, 9:26:55 PM12/2/11
to openpyxl-users
Has anyone been able to delete a sheet, either by name or by number,
using openpyxl? If so, I'd very much appreciate an example showing
how this is done.

Phillip M. Feldman

Eric Gazoni

unread,
Dec 3, 2011, 4:50:39 AM12/3/11
to openpyx...@googlegroups.com
Hi Philip,
the function remove_sheet() takes a worksheet object as argument.

You can use it this way:
import openpyxl
wb = openpyxl.workbook.Workbook()
ws = wb.create_sheet()
wb.remove_sheet(ws)

Cheers,
Eric

Le 3/12/11 03:26, Phillip M. Feldman a écrit :

Phillip M. Feldman

unread,
Dec 6, 2011, 12:56:59 AM12/6/11
to openpyxl-users
Hello Eric,

I'm doing something similar:

(code that creates the workbook and generates several sheets)
try:
sh= get_sheet_by_name('Sheet')
self.book.remove_sheet(sh)
except:
print("Warning: write_xls is unable to remove the sheet called
'Sheet'.")
pass
self.book.save(self.fname)

Invariably, I get the warning and the sheet called "Sheet" is
present. Any idea why this is happening? Thanks!

Phillip

On Dec 3, 1:50 am, Eric Gazoni <eric.gaz...@gmail.com> wrote:
> Hi Philip,
> the function remove_sheet() takes a worksheet object as argument.
>
> You can use it this way:
>
>     import openpyxl
>     wb = openpyxl.workbook.Workbook()
>     ws = wb.create_sheet()
>     wb.remove_sheet(ws)
>
> Cheers,
> Eric
>

> Le 3/12/11 03:26, Phillip M. Feldman a �crit :

robert.d...@gmail.com

unread,
Nov 28, 2013, 12:27:04 PM11/28/13
to openpyx...@googlegroups.com, phillip....@gmail.com
Hey Philip,

I've figured out how to delete a sheet by number, using the following command:

self.book.remove_sheet(self.book.worksheets[0])

Where I'm falling down, is trying to delete by name...
Robert

Phillip Feldman

unread,
Nov 28, 2013, 1:43:32 PM11/28/13
to robert.d...@gmail.com, openpyx...@googlegroups.com

Hello Robert,

Following your code, I did the following:

sh= self.book.get_sheet_by_name('Sheet')
self.book.remove_sheet(sh)

It appears to work.  Thanks!

Phillip

Phillip Feldman

unread,
Nov 28, 2013, 1:45:22 PM11/28/13
to robert.d...@gmail.com, openpyx...@googlegroups.com
I'd like to see the developers modify this so that one can pass any of the following:

- a sheet number (int)
- a sheet name (string)
- a sheet object

I suspect that this won't happen.  (It looks as though openpyxl development has come to an end).

Phillip

Charlie Clark

unread,
Nov 28, 2013, 3:03:36 PM11/28/13
to openpyx...@googlegroups.com
Am 28.11.2013, 19:45 Uhr, schrieb Phillip Feldman
<phillip....@gmail.com>:

> I'd like to see the developers

I wonder who they are?

> modify this so that one can pass any of the
> following:
> - a sheet number (int)

This will not happen.

> - a sheet name (string)

Should be possible.

> - a sheet object

Existing behaviour, see the recent thread on removing sheets.

> I suspect that this won't happen. (It looks as though openpyxl
> development has come to an end).

Inasmuch as 1.7 was recently released and announced on this mailing list
then your suspicions are unfounded and somewhat surprising. However, the
same is true for openpyxl as for *all* open source projects: if you think
something needs fixing the jump in. That's how I got involved and now look
at me: I'm co-maintainer! :-P

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
Reply all
Reply to author
Forward
0 new messages