{"id":159,"date":"2013-04-01T23:30:00","date_gmt":"2013-04-02T04:30:00","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=159"},"modified":"2013-04-01T23:30:00","modified_gmt":"2013-04-02T04:30:00","slug":"manipulating-excel-files-using-python-part-2-writing-excel-files","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2013\/04\/01\/manipulating-excel-files-using-python-part-2-writing-excel-files\/","title":{"rendered":"Manipulating Excel files using Python part 2: Writing Excel Files"},"content":{"rendered":"<p>Writing Excel files using Python is quite easy, using the\u00a0<a title=\"xwt repository\" href=\"https:\/\/secure.simplistix.co.uk\/svn\/xlwt\/trunk\/\">xlwt package<\/a>. Similar to\u00a0<a title=\"xlrd package\" href=\"http:\/\/www.lexicon.net\/sjmachin\/xlrd.htm\">xlrd<\/a>\u00a0mentioned in an\u00a0<a title=\"Reaing Excel files using Python\" href=\"http:\/\/michalisavraam.org\/blog\/38-blog-entries\/57-manipulating-excel-files-in-python-no-excel-needed.html\">earlier post<\/a>, xlwt allows one to write Excel files from scratch using Python.<img decoding=\"async\" title=\"excelPreview\" alt=\"excelPreview\" src=\"http:\/\/michalisavraam.org\/wp-content\/uploads\/2009\/06\/excelPreview.png\" width=\"461\" \/>A brief reminded of Excel files (as mentioned in an earlier post) follows, to help people understand how Excel files work.The program 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\u00a0<strong>Sheets<\/strong>, the little tabs on the bottom left corner usually. Within each Sheet, you can find an arrangement of\u00a0<strong>Cells<\/strong>\u00a0in 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.To create your own Excel files using Python, all you need to have is the xlwt package. The workflow for such a process is simple: create a workbook, create a sheet within the workbook, start writing data into the cells of the sheet. Sample code shown below should help you get started quickly and easily.<\/p>\n<div>\n<div id=\"highlighter_4610\">\n<div><a href=\"http:\/\/michalisavraam.org\/2009\/06\/manipulating-excel-files-using-python-part-2-writing-files\/#\">?<\/a><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<\/td>\n<td>\n<div>\n<div><code>import<\/code> <code>xlwt <\/code><code># Import the package<\/code><\/div>\n<div><code>wbook <\/code><code>=<\/code> <code>xlwt.Workbook() <\/code><code># Create a new workbook<\/code><\/div>\n<div><code>sheet <\/code><code>=<\/code> <code>wbook.add_sheet(&amp;amp;amp;quot;Sample Sheet&amp;amp;amp;quot;) <\/code><code># Create a sheet<\/code><\/div>\n<div><code>data <\/code><code>=<\/code> <code>&amp;amp;amp;quot;Sample data&amp;amp;amp;quot; <\/code><code># Something to write into the sheet<\/code><\/div>\n<div><code>for<\/code> <code>rowx <\/code><code>in<\/code> <code>range<\/code><code>(<\/code><code>5<\/code><code>):<\/code><\/div>\n<div><code># Loop through the first five rows<\/code><\/div>\n<div><code>for<\/code> <code>colx <\/code><code>in<\/code> <code>range<\/code><code>(<\/code><code>5<\/code><code>):<\/code><\/div>\n<div><code># Loop through the first five columns<\/code><\/div>\n<div><code># Write the data to rox, column<\/code><\/div>\n<div><code>sheet.write(rowx, colx, data)<\/code><\/div>\n<div><code># Save our workbook on the harddrive<\/code><\/div>\n<div><code>wbook.save(&amp;amp;amp;quot;myFile.xls&amp;amp;amp;quot;)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>And that is all you need. You just created a brand new Excel file called myFile.xls within Python without the use of Excel itself. The file has the first 5 rows and columns filled out with the text \u201cSample data\u201d. It is trivial then to change this so it can save any data you want with a little bit more of Python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Writing Excel files using Python is quite easy, using the\u00a0xlwt package. Similar to\u00a0xlrd\u00a0mentioned in an\u00a0earlier post, xlwt allows one to write Excel files from scratch&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-159","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/159","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=159"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/159\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}