Home > database >  How can I overwrite Excel sheet by win32com in python
How can I overwrite Excel sheet by win32com in python

Time:12-22

I'm trying to overwrite excel sheet data from A file to B file. For this, I'm using pywin32.

import win32com.client
    
excel = win32com.client.Dispatch("Excel.Application")
wb_dst = excel.ActiveWorkbook
wb_src = excel.Workbooks.Open("source.xlsx")
wb_src.ActiveSheet.Copy(After=wb_dst.ActiveSheet)

I finished about copy. But this code made additional sheet to destination file. Basically two files are almost same. So I wanted to copy at same sheet on destination file. But Copy function in pywin32 make automatically before or after active sheet. How can I overwrite it?

CodePudding user response:

You can use something like the following:

wb_src.ActiveSheet.Cells.Copy(Destination=wb_dst.ActiveSheet.Cells(1))

or

wb_src.ActiveSheet.UsedRange.Copy(Destination=wb_dst.ActiveSheet.Cells(1))

which use the Copy method of the Range class, different from the Copy method of the Worksheet class.

  • Related