Connecting Tech Pros Worldwide Forums | Help | Site Map

Copying values from one spreadsheet to another

Newbie
 
Join Date: Feb 2009
Posts: 3
#1: Feb 21 '09
Does anyone have an example of reading an Excel spreadsheet and iteratively copying the VALUES from each of the cells in each of the sheets over to a new spreadsheet?


TIA

Newbie
 
Join Date: Sep 2007
Location: Philippines
Posts: 6
#2: Mar 2 '09

re: Copying values from one spreadsheet to another


python and excel...uhum

first thing to do:
install the win32 extension for python (just search sourceforge for this)

now assume we have an excel file (file.xls) with 2 sheets : "sheet 1", "sheet 2"

#####################################
import win32com.client

xlApp = win32com.client.Dispatch('Excel.Application')
xlBook = xlApp.Workbooks.Open("file.xls")

sheet1 = xlBook.Worksheets["sheet 1"]
sheet2 = xlBook.Worksheets["sheet 2"]

# assume we are going to copy the first 200 rows of column 1

rows = 200
col = 1

index = 1
while index <= rows:
sheet2.Cells(index,col).Value = sheet1.Cells(index,col).Value

xlBook.Save(SaveChanges=0) #save the file
del xlApp #kill the excel application

#####################################

...hope this helps
Reply