xlwings copy and paste with source formatting

Viewed 2916

I would like to copy and paste one sheet to another using xlwings in python while keeping the source formatting. The code which I have found below copy and pastes the ranges but only the values. Please can someone help me modify the code to preserve source formatting

import xlwings as xw
wb = xw.Book('mybook.xlxs')
my_values = wb.sheets['Sheet1'].range('A1:A6').options(ndim=2).value 
wb.sheets['Sheet2'].range('A1:A6').value = my_values

Thanks!

1 Answers

Long battle and finally got the answer. From xlwings source code: https://github.com/xlwings/xlwings/pull/1216/files

source_sheet.range.copy(destination_sheet.range)

In other words:

wb.sheets['Sheet1'].range('A1:A6').copy(wb.sheets['Sheet2'].range('A1:A6'))

It works also from one workbook to another workbook.

Related