pythonexcelwin32comvba

win32com Excel PasteSpecial


I'm having some trouble with PasteSpecial in python. Here's the sample code:

import win32com.client as win32com
from win32com.client import constants

xl = win32com.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True
wb = xl.Workbooks.Add ()
Sheet1 = wb.Sheets("Sheet1")

# Fill in some summy formulas
for i in range(10):
    Sheet1.Cells(i+1,1).Value = "=10*"+str(i+1)

Sheet1.Range("A1:A16").Copy()
Sheet1.Range("C1").Select()
Sheet1.PasteSpecial(Paste=constants.xlPasteValues)

I'm getting the following error:

TypeError: Paste() got an unexpected keyword argument 'Paste'

I know that paste is a keyword argument because of the MSDN here: http://msdn.microsoft.com/en-us/library/office/ff839476(v=office.15).aspx

Any idea why it won't let me do this? Can't really find much on the web.

Edit for solution(s):

import win32com.client as win32com
from win32com.client import constants

xl = win32com.gencache.EnsureDispatch('Excel.Application')
xl.Visible = True
wb = xl.Workbooks.Add ()
Sheet1 = wb.Sheets("Sheet1")

# Fill in some summy formulas
for i in range(10):
    Sheet1.Cells(i+1,1).Value = "=10*"+str(i+1)

Sheet1.Range("A1:A16").Copy()
Sheet1.Range("C1").PasteSpecial(Paste=constants.xlPasteValues)
# OR this I just found right after I posted this works as well:
xl.Selection.PasteSpecial(Paste=constants.xlPasteValues)

Solution

  • I don't work with python but to do a PasteSpecial in Excel-VBA, you have to mention the cell where you want to perform the pastespecial, so try like

    Sheet1.Range("C1").PasteSpecial(Paste=constants.xlPasteValues)
    

    If you want a simple paste then I guess this should work

    Sheet1.Paste