fix this error xlwings attributeerror 'sheet' object has no attribute 'copy' using python

Viewed 288

i have a python that read excel file and split the it into multiple files based in the sheet number for this i used the xlwings package. latest version 0.27.3

The problem is that once the script run it crash and display the below error:

Traceback (most recent call last): File "f:\AIenv\streamlit\TEC_APPS\split_excel.py", line 18, in
sheet.copy(after=wb_new.sheets[0]) File "f:\AIenv\lib\site-packages\xlwings\main.py", line 1503, in copy
self.impl.copy(before=before, after=after) File "f:\AIenv\lib\site-packages\xlwings_xlwindows.py", line 1010, in copy self.xl.Copy(Before=before, After=after) File "f:\AIenv\lib\site-packages\xlwings_xlwindows.py", line 94, in call v = self.__method(*args, **kwargs) File "C:\Users\LTGM~1\AppData\Local\Temp\gen_py\3.7\00020813-0000-0000-C000-000000000046x0x1x9.py", line 47229, in Copy , After) pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'Copy method of Worksheet class failed', 'xlmain11.chm', 0, -2146827284), None)

code:

from pathlib import Path
import xlwings as xw

base_dir = Path(__file__).parent
output_dir = base_dir / "output" 
output_dir.mkdir(parents=True,exist_ok=True)

excel_file = "data.xlsm"

app = xw.App(visible=False)
wb = xw.Book(excel_file)


for sheet in wb.sheets:
     wb_new = app.books.add()
     sheet.copy(after=wb_new.sheets[0])
     wb_new.sheets[0].delete()
     wb_new.save(f"{sheet.name}.xlsx")
     wb_new.close()
0 Answers
Related