{"id":154,"date":"2013-04-01T23:23:35","date_gmt":"2013-04-02T04:23:35","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=154"},"modified":"2013-04-01T23:23:35","modified_gmt":"2013-04-02T04:23:35","slug":"read-excel-files-from-python","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2013\/04\/01\/read-excel-files-from-python\/","title":{"rendered":"Read Excel files from Python"},"content":{"rendered":"<p>Use the excellent\u00a0<a href=\"http:\/\/www.lexicon.net\/sjmachin\/xlrd.htm\">xlrd<\/a>\u00a0package, which works on any platform. That means you can read Excel files from Python in Linux! Example usage:<\/p>\n<p>Open the workbook<\/p>\n<div>\n<div id=\"highlighter_637606\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>import<\/code> <code>xlrd<\/code><\/div>\n<div><code>wb <\/code><code>=<\/code> <code>xlrd.open_workbook(<\/code><code>'myworkbook.xls'<\/code><code>)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Check the sheet names<\/p>\n<div>\n<div id=\"highlighter_140427\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>wb.sheet_names()<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Get the first sheet either by index or by name<\/p>\n<div>\n<div id=\"highlighter_566356\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>sh <\/code><code>=<\/code> <code>wb.sheet_by_index(<\/code><code>0<\/code><code>)<\/code><\/div>\n<div><code>sh <\/code><code>=<\/code> <code>wb.sheet_by_name(u<\/code><code>'Sheet1'<\/code><code>)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Iterate through rows, returning each as a list that you can index:<\/p>\n<div>\n<div id=\"highlighter_156190\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>for<\/code> <code>rownum <\/code><code>in<\/code> <code>range<\/code><code>(sh.nrows):<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>print<\/code> <code>sh.row_values(rownum)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>If you just want the first column:<\/p>\n<div>\n<div id=\"highlighter_153289\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>first_column <\/code><code>=<\/code> <code>sh.col_values(<\/code><code>0<\/code><code>)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Index individual cells:<\/p>\n<div>\n<div id=\"highlighter_759440\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>cell_A1 <\/code><code>=<\/code> <code>sh.cell(<\/code><code>0<\/code><code>,<\/code><code>0<\/code><code>).value<\/code><\/div>\n<div><code>cell_C4 <\/code><code>=<\/code> <code>sh.cell(rowx<\/code><code>=<\/code><code>3<\/code><code>,colx<\/code><code>=<\/code><code>2<\/code><code>).value<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>(Note Python indices start at zero but Excel starts at one)<\/p>\n<p>Turns out the\u00a0put_cell()\u00a0method isn\u2019t supported, so ignore the following section (Thanks for the heads up, John!)<\/p>\n<p><span style=\"text-decoration: line-through;\">Put something in the cell:<\/span><\/p>\n<div>\n<div id=\"highlighter_286217\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>row <\/code><code>=<\/code> <code>0<\/code><\/div>\n<div><code>col <\/code><code>=<\/code> <code>0<\/code><\/div>\n<div><code>ctype <\/code><code>=<\/code> <code>1<\/code>\u00a0 <code># see below<\/code><\/div>\n<div><code>value <\/code><code>=<\/code> <code>'asdf'<\/code><\/div>\n<div><code>xf <\/code><code>=<\/code> <code>0<\/code>\u00a0 <code># extended formatting (use 0 to use default)<\/code><\/div>\n<div><code>sh.put_cell(row, col, ctype, value, xf)<\/code><\/div>\n<div><code>sh.cell(<\/code><code>0<\/code><code>,<\/code><code>0<\/code><code>)\u00a0 <\/code><code># text:u'asdf'<\/code><\/div>\n<div><code>sh.cell(<\/code><code>0<\/code><code>,<\/code><code>0<\/code><code>).value\u00a0 <\/code><code># 'asdf'<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Use the excellent\u00a0xlrd\u00a0package, which works on any platform. That means you can read Excel files from Python in Linux! Example usage: Open the workbook import&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-154","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=154"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/154\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}