سؤال

So I am trying to copy alot of data from one woorkbook to another. The thing is that the data in the source workbook has some weird formating so I want to just get the values. The code I have so far is this.

excel=win32com.client.Dispatch("Excel.Application");
excel.Visible = 1;
source = excel.Workbooks.Open(Cali.xlsm');
copy = excel.Workbooks.Open(temp.xlsx');
sdata = source.ActiveSheet;cdata = copy.ActiveSheet;
data=sdata.Range("89:89")
sdata.Range("89:89",data.End(4)).Copy()

now I can use

cdata.Paste()

but it pastes the formating as well

I found

cdata.PasteSpecial()

but it also pastes the formating.

Anyone who know how to use PasteSpecial() so it copies just the values or someone knows a better way I would be very apprecitave.

هل كانت مفيدة؟

المحلول

You could try the following which copies values from cells A1:A3 from one workbook to another.

from win32com.client import Dispatch
wkbk1 = "...\workbook1.xlsx"
wkbk2 = "...\workbook2.xlsx"
excel = Dispatch("Excel.Application")
excel.Visible = 1
source = excel.Workbooks.Open(wkbk1)
excel.Range("A1:A3").Select()
excel.Selection.Copy()
copy = excel.Workbooks.Open(wkbk2)
excel.Range("A1:A3").Select()
excel.Selection.PasteSpecial(Paste=-4163)

wkbk1 and wkbk2 are file paths to two workbooks.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top