Sir DrinksCoffeeALot Sir DrinksCoffeeALot - 1 year ago 241
Python Question

How to copy and paste existing sheet in existing workbook using python?

I've been searching for a way to do the following:

  1. Open an existing workbook that contains several worksheets,

  2. Create a new worksheet, and rename it.

  3. Copy data and formatting from an existing worksheet, named 'Template', and paste that data to the previously created worksheet.

Is there any way to do what I need?

This is the way how to do it:

import openpyxl
from openpyxl.worksheet.copier import WorksheetCopy

workbook = openpyxl.load_workbook('input.xlsx')
template_worksheet = workbook.get_sheet_by_name(sheet_name)
new_worksheet = workbook.create_sheet('New_Sheet_Name')
instance = WorksheetCopy(template_worksheet, new_worksheet)

Answer Source

You will have to use python package for this task, I recommend you use openpyxl package. Basic usage of the package can be found here.

You will basically read the excel and at the same time write a new excel, copying data row by row.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download