'Python win32com select range

I need to select a range of values from a workbook_A and copy them to another workbook_B. The issue is that i need to copy ONLY the values while preserving the formatting of workbook_B.

I do not understand why it give me a error "com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'Select method of Range class failed', 'xlmain11.chm', 0, -2146827284), None)"

Here the code

import pandas as pd
import win32com
from win32com.client import Dispatch,constants

wb_A = r"A.xlsx"
wb_B_Template = r"B_Template_Preformatted.xlsx"

xlapp = win32com.client.gencache.EnsureDispatch('Excel.Application')
xlapp.Visible= False
xlapp.DisplayAlerts = False

cars = {'Brand': ['Honda','Toyota','Ford','Audi'],
        'Price': [0.23, 0.35, 0.43, 0.57]}

df_cars = pd.DataFrame(cars, columns = ['Brand', 'Price'])
df_cars.to_excel(wb_A, "Table")

wb_origin = xlapp.Workbooks.Open(wb_A)
wb_destination = xlapp.Workbooks.Open(wb_B_Template)

wb_origin.Worksheets("Table").Range("A1:C5").Select()
xlapp.Selection.Copy(Destination=wb_destination.Worksheets("Tabelle").Range("A1"))

wb_origin.Close()
wb_destination.Close(SaveChanges=True)

xlapp.Quit()


Solution 1:[1]

import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
ws = wb.Worksheets("Sheet1")
ws.Range("A1").Value = 1
ws.Range("A2").Value = 2
ws.Range("A1:A2").AutoFill(ws.Range("A1:A10"),win32.constants.xlFillDefault)
wb.SaveAs('autofill_cells.xlsx')
excel.Application.Quit()

Check this out

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aadesh Gurav