Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88126

copy entire excel worksheet to a new worksheet using Python win32com

$
0
0

I try to open an existing workbook called 'N11 Result.xlsx'. Copy the entire worksheet 'Sheet1' (rename it to'Built with Python' first...) and paste to a new worksheet(named 'Annual') within the same workbook. I think the last line of my code doesn't work... any help will be helpful.

import os,sys
sys.path.append(r"""U:\Programming\Python\Python Manipulate Excel""")
work_dir=r"""U:\Programming\Python\Python Manipulate Excel"""
file_name='N11 Result.xlsx'

import win32com.client
excel = win32com.client.Dispatch('Excel.Application')
excel.Visible = True

wb = excel.Workbooks.Open(work_dir+'\\'+file_name)
ws = wb.Worksheets('Sheet1')
ws.Name = 'Built with Python'
wb.Worksheets("Built with Python").Copy(before=wb.Worksheets("Annual"))
excel.Application.Quit()

The error message showed up will be

Traceback (most recent call last): File "U:\Programming\Python\Python Manipulate Excel\Test_createExcel_01.py", line 17, in wb.Worksheets("Built with Python").Copy(before=wb.Worksheets("Annual")) File "C:\Temp\gen_py\2.7\00020813-0000-0000-C000-000000000046x0x1x7\Sheets.py", line 113, in call ret = self._oleobj_.InvokeTypes(0, LCID, 2, (9, 0), ((12, 1),),Index com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)


Viewing all articles
Browse latest Browse all 88126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>