I have been using xlwings in Python, but have not been able to figure out how to copy a worksheet. I want to treat a particular worksheet as a template, and copy that worksheet each time before making modifications.
I am using version 0.11.4 of xlwings. If such functionality is not built in, I am okay with going outside of xlwings to use pywin32 functions in order to accomplish this.
For anyone struggling on how to copy sheet to an another workbook, here is an example:
After poking around in several places and reading the pywin32 documentation, I found a solution to copy the worksheet:
This does go outside of the native functionality provided by xlwings. Because xlwings is a wrapper around pywin32, the
.api()
call allows access to those pywin32 functions that are undocumented in xlwings.Also note that the 'After' command does not work within the worksheet; it will open a new workbook with the sheet copied. This shouldn't pose too big of an issue, as I believe the indexes can be reordered if needed.