Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Working with Excel Files
Summary

Instruction

Good job! Suppose you need several very similar worksheets in your workbook. Instead of making each one from scratch, you can copy an existing worksheet:

ws = wb.active
wb_copy = wb.copy_worksheet(ws)

The new sheet will appear in your workbook under the name of the worksheet you copied and the word "Copy". If we had a "Calendar" worksheet and we copied it, the new sheet's name would be "Calendar Copy". However, you need to remember that:

  1. You cannot copy worksheets from different workbooks.
  2. In the current version of openpyxl, it is not possible to copy elements like charts or images.

To be safe, use copy_worksheet to copy cell data you want to preserve.

Exercise

Copy the "Entertainment" worksheet and change the name of the copy to "Eating outside". Save the modified workbook in the file budget.xlsx.

Stuck? Here's a hint!

To select a particular worksheet, use wb["name of worksheet"].

Use the title property to change the copied worksheet's name.