Reputation: 117
I am trying to learn enough Python to write a program to extract data from multiple Excel files and compile them into one Excel file. I am hoping someone can help me with where to find a basic understanding of this. I know this is vague and all, but I am not really sure where to start just yet.
Upvotes: 0
Views: 764
Reputation: 23480
import xlrd
book = xlrd.open_workbook('myfile.xls')
print book.nsheets
print book.sheet_names()
sh = book.sheet_by_index(0)
print sh.name, sh.nrows, sh.ncols
You can also loop through each row of the excel document by sheet name:
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows - 1
curr_row = -1
while curr_row < num_rows:
curr_row += 1
row = worksheet.row(curr_row)
print row
You can get the xlrd module here
Or for a list of Python excel modules here
Upvotes: 2
Reputation: 118
It is possible to read and write CSV (comma separated values) files using Python 2.4 Distribution. Like most languages, file operations can be done with Python. Writing a CSV file with Python can be done by importing the CSV module and creating a write object that will be used with the WriteRow Method. Reading a CSV file can be done in a similar way by creating a reader object and by using the print method to read the file. As file operations require advanced concepts, some knowledge of programming with Python is required to read and write the CSV (comma separated values) files.
Start by importing the CSV module:
import csv
We will define an object "writer" (named c), which can later be used to write the CSV file.
c = csv.writer(open("MYFILE.csv", "wb"))
Now we will apply the method to write a row. Writerow The method takes one argument - this argument must be a list and each list item is equivalent to a column. Here, we try to make an address book:
c.writerow(["Name","Address","Telephone","Fax","E-mail","Others"])
Upvotes: 0