win32com Excel PasteSpecial
我在Python中使用PasteSpecial遇到了一些麻烦。 以下是示例代码:
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)
我收到以下错误:
TypeError: Paste() got an unexpected keyword argument 'Paste'
我知道粘贴是一个关键字参数,因为这里的MSDN: http : //msdn.microsoft.com/en-us/library/office/ff839476( v= office.15).aspx
任何想法为什么它不会让我这样做? 在网上找不到太多东西。
编辑解决scheme:
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)
我不使用python,但要在Excel-VBA中做一个PasteSpecial
,你必须提到你想要执行pastespecial的单元格,所以试试像
Sheet1.Range("C1").PasteSpecial(Paste=constants.xlPasteValues)
如果你想要一个简单的粘贴,那么我想这应该工作
Sheet1.Paste
您可以通过在Excel vb中执行macros来获取xlPasteFormats的值:
Sub Macro2() Range("A7").Select ActiveCell.FormulaR1C1 = xlPasteFormats End Sub
xlPasteFormats的值是-4122
在Python脚本中,您可以使用
xlSheet.Range("A7:H7").Copy() xlSheet.Range("A%s:H%s"%(r,r)).PasteSpecial(Paste=-4122)