pachamama
pachamama

Reputation: 31

python win32 excel copy worksheet and change name of copied worksheet

I'm trying to use python win32 excel application in order to:

1) copy a worksheet

2) change the name of the copied worksheet

Using the following code:

from win32com.client import Dispatch

xl = Dispatch("Excel.Application")
wb = xl.Workbooks.Open('Spreadsheet.xlsx')
ws = wb.Worksheets('Sheet1')

ws.Name = 'Name of Copied Sheet'

But I get the following error:

AttributeError: 'NoneType' object has no attribute 'Name'

Upvotes: 3

Views: 6282

Answers (1)

LamerLink
LamerLink

Reputation: 131

This is old but I did it the following way successfully, might help someone else in the future.

import win32com.client as win32  

excel = win32.DispatchEx('Excel.Application')
wb = excel.Workbooks.Open(file)
wb.Worksheets(1).Name = "New Name"

Upvotes: 3

Related Questions