<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>michalisavraam.org blog &#187; read</title>
	<atom:link href="http://michalisavraam.org/tag/read/feed/" rel="self" type="application/rss+xml" />
	<link>http://michalisavraam.org</link>
	<description>a spatial web presence</description>
	<lastBuildDate>Mon, 24 May 2010 17:17:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Manipulating Excel files using Python part 1: Reading Excel Files</title>
		<link>http://michalisavraam.org/2009/06/manipulating-excel-files-using-python-part-1-reading-files/</link>
		<comments>http://michalisavraam.org/2009/06/manipulating-excel-files-using-python-part-1-reading-files/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 05:42:21 +0000</pubDate>
		<dc:creator>Michalis Avraam</dc:creator>
				<category><![CDATA[Python Points]]></category>
		<category><![CDATA[data manipulation]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[excel file]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[read]]></category>
		<category><![CDATA[reading]]></category>
		<category><![CDATA[xlrd]]></category>

		<guid isPermaLink="false">http://michalisavraam.org/blog/38-blog-entries/57-manipulating-excel-files-in-python-no-excel-needed</guid>
		<description><![CDATA[It is often the case that the freely available data online are in Excel format. If one has Excel, then one has the ability to do some sort of basic manipulation of the files. But if Excel is not available, or your analysis software does not read Excel files, there is another way: use Python [...]


Related posts:<ol><li><a href='http://michalisavraam.org/2009/06/manipulating-excel-files-using-python-part-2-writing-files/' rel='bookmark' title='Permanent Link: Manipulating Excel files using Python part 2: Writing Excel Files'>Manipulating Excel files using Python part 2: Writing Excel Files</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>It is often the case that the freely available data online are in Excel format. If one has Excel, then one has the ability to do some sort of basic manipulation of the files. But if Excel is not available, or your analysis software does not read Excel files, there is another way: use Python to manipulate Excel files.<span id="more-23"></span></p>
<p><img class="alignright size-full wp-image-43" title="excelPreview" src="http://michalisavraam.org/wp-content/uploads/2009/06/excelPreview.png" alt="excelPreview" width="461" height="280" />Before continuing, let&#8217;s discuss the basic idea of Excel. This programs stores data in what is called a <strong>Workbook</strong>. Think of this as the file on your computer (the .xls). A Workbook can then have one or more <strong>Sheets</strong>, the little tabs on the bottom left corner usually. Within each Sheet, you can find an arrangement of <strong>Cells</strong> in a matrix form, which are referenced by Column Name and Row Number (so the top left cell is A1, the one directly to its right is A2, the one below is B2, etc. The image should help shed some light if you are not familiar with the concept of spreadsheets.</p>
<p>In order to <strong>read</strong> Excel sheets in Python without using any Microsoft Office code, we need to use the excellent <a title="xlrd Package Website" href="http://www.lexicon.net/sjmachin/xlrd.htm">xlrd packge</a> by <a title="Lingfo Website" href="http://www.lexicon.net/sjmachin/">Lingfo</a>. The basic workflow is as follows:</p>
<ol>
<li>Open a workbook using <em>book = xlrd.open_workbook(&#8220;filetoread.xls&#8221;)</em></li>
<li>Access a sheet in the workbook using <em>sheet = book.sheet_by_index(0)</em> to open the first sheet, or if you know the name, <em>sheet = book.sheet_by_name(&#8220;Name of Sheet&#8221;)</em></li>
<li>Retrieve data using any of the multiple methods: <em>cellValue =sheet.cell_value(rowx, colx)</em>, or to retrieve a whole column use <em>colValues = sheet.col_values(colx, start_rowx=0, end_rowx=None)</em> or the retrieve a whole row use <em>rowValues = sheet.row_values(rowx, start_colx=0, end_colx=None)</em></li>
</ol>
<p>The basic idea is very simple, and we can easily write a little Python script that will allow us to read Excel files without the need to use Excel itself. The following example will offer a preview of an Excel file&#8217;s first sheet using the first 5 rows and columns of a file.</p>
<pre class="brush: python;">
import xlrd # Import the package
book = xlrd.open_workbook(&amp;amp;amp;quot;sample.xls&amp;amp;amp;quot;) # Open an .xls file
sheet = book.sheet_by_index(0) # Get the first sheet
for counter in range(5): # Loop for five times
# grab the current row
rowValues = sheet.row_values(counter,start_col=0, end_colx=4)
# Print the values of the row formatted to 10 characters wide
print &amp;amp;amp;quot;%-10s | %-10s | %-10s | %-10s | %-10s&amp;amp;amp;quot; % tuple(rowValues)
# Print row separator
print &amp;amp;amp;quot;-&amp;amp;amp;quot; *62
</pre>
<p>That is all there is to it. Quick, fast and no need for Excel whatsoever. Sample output is presented below:</p>
<p><img class="aligncenter size-full wp-image-46" title="sampleOutput" src="http://michalisavraam.org/wp-content/uploads/2009/06/sampleOutput.png" alt="sampleOutput" width="585" height="447" /></p>
Note: There is a rating embedded within this post, please visit this post to rate it.


<p>Related posts:<ol><li><a href='http://michalisavraam.org/2009/06/manipulating-excel-files-using-python-part-2-writing-files/' rel='bookmark' title='Permanent Link: Manipulating Excel files using Python part 2: Writing Excel Files'>Manipulating Excel files using Python part 2: Writing Excel Files</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://michalisavraam.org/2009/06/manipulating-excel-files-using-python-part-1-reading-files/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
