2

I'm trying to copy a range from one excel spreadsheet to another using a for loop but keep getting an error message. I'm using win32com and python to make the copy.

Here is a snippet of my code

params_columns = [1,3,5]
for x in params_columns:
        copy_range = "{}2:{}1048576".format(colnum_string(x + 1), colnum_string(x + 1))
        excel = Dispatch("Excel.Application")
        excel.Visible = 1
        source = excel.Workbooks.Open(filepath_first)
        excel.Range("A1:A3").Select()
        excel.Selection.Copy()
        copy = excel.Workbooks.Open(filepath_second)
        excel.Range("A1:A3").Select()

        # paste type https://learn.microsoft.com/en-us/office/vba/api/excel.xlpastetype
        excel.Selection.PasteSpecial(Paste=-4104)
        excel.Selection.PasteSpecial(Paste=8)

where colnum_string converts the column number to a string.

The error that is returned is as follows:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'PasteSpecial method of Range class failed', 'xlmain11.chm', 0, -2146827284), None)

Thanks for your help.

Major Anarchy
  • 17
  • 1
  • 4

0 Answers0