{"id":13,"date":"2012-10-17T04:29:35","date_gmt":"2012-10-17T04:29:35","guid":{"rendered":"https:\/\/code4reference.com\/?p=13"},"modified":"2012-10-17T04:29:35","modified_gmt":"2012-10-17T04:29:35","slug":"code4referencepython-sample-script-to-read-and-parse-excel-file-using-xlrd-library","status":"publish","type":"post","link":"https:\/\/code4reference.com\/?p=13","title":{"rendered":"Code4ReferencePython sample script to read and parse excel file using xlrd library"},"content":{"rendered":"<p>Recently I got interested in Python programming actually this was my first scripting language and its really a handy tool. I would like to share a small code which\u00a0\u00a0can access Excel(*.xls) file.\u00a0\u00a0I am assuming that you are having Ubuntu machine.\u00a0In order to run this code you need to have xlrd package and Python installed. If your system doesn\u2019t have xlrd package then you can install it in two ways.<\/p>\n<p>you can install by use of <em>pip<\/em><\/p>\n<pre>\n$ sudo pip install xlrd\n<\/pre>\n<p>otherwise try to execute below command<\/p>\n<pre>$ sudo apt-get install python-xlrd<\/pre>\n<p>Here is a simple code which demonstrate a few functionality of this library.<\/p>\n<pre>\n#!\/usr\/bin\/python\nimport optparse  ###Option parser\ntry:\n  import xlrd; ###Need to make sure xlrd library is available\nexcept ImportError:\n  print 'Could not find package. Try installing it first'\n  print ''\n  print 'Halting.'\n  sys.exit(1)\n\ndef formatDataTypeString(dataType):\n # Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 5=Error, 6=Blank\n formatString =\"\"\n if dataType == 0:\n   formatString += \"Empty :\"\n elif dataType == 1:\n   formatString +=\" Text :\"\n elif dataType == 2:\n   formatString +=\"Number :\"\n elif dataType == 3:\n   formatString +=\"Date :\"\n elif dataType == 4:\n   formatString +=\" Boolean :\"\n elif dataType == 5:\n    formatString +=\" Error :\"\n elif dataType == 6:\n    formatString +=\" Blank :\"\n\n   return formatString\n\ndef metaData(xlFile):\n    \"\"\"This method takes one argument which is xl file name. It display the\n    meta-data of the xl file. It displays sheet names present in the file\n    and the content type.\n    \"\"\"\n    book = xlrd.open_workbook(xlFile)\n    print(\"#########sheet Names#########\")\n    sheetNames = \"\"\n    for sheet in book.sheet_names():\n        sheetNames += str(sheet) +\"\"\n    print(sheetNames)\n    sheets = book.sheets()\n    for sheet in sheets:\n        print(\"=====\" + str(sheet.name) + \"(\" + str(sheet.nrows) + \", \" + str(sheet.ncols) + \")=====\")\n        if sheet.ncols &gt; 0:\n            curCol = 0;\n            colsType = \"\";\n            while curCol &lt; sheet.ncols:\n                colsType +=formatDataTypeString(sheet.cell_type(0,curCol))\n                curCol +=1\n            print(\"First row cell Type \" + colsType)\n        else:\n            print(\"Sheet is empty\")\n\ndef showData(xlFile, sheetName):\n    \"\"\"This method takes two arguments xlFile name and SheetName.\n    It display data if it is present in the provided sheet\"\"\"\n    book = xlrd.open_workbook(xlFile)\n    if sheetName:\n        sheet = book.sheet_by_name(sheetName)\n    else:\n        sheet = book.sheet_by_index(0)\n    if sheet.nrows &gt; 0:\n        for i in xrange(sheet.nrows):\n            print(sheet.row_values(i))\n    else:\n        print (\"Sheet is empty\")\ndef main():\n    ##Argument parsing\n    parser = optparse.OptionParser(usage = \"%prog filename [options] \\n\" )\n    parser.add_option(\"-m\",\"--metadata\",\n                        action=\"store_true\",\n                        default = False,\n                        dest = \"metaData\",\n                        help = \"Meta information of XL sheet\")\n    parser.add_option(\"-d\",\"--showData\",\n                        action=\"store_true\",\n                        default = False,\n                        dest = \"showData\",\n                        help = \"Show Data of specified sheet otherwise show data from first sheet\")\n    parser.add_option(\"-s\",\"--sheet\",\n                        dest = \"sheetName\",\n                        help = \"Sheet name whose data you want to see. This option should be used with -s(--showData) option\")\n\n    (options, args) = parser.parse_args()\n\n    if len(args) != 1:\n        parser.print_help()\n        return -1\n    else:\n        if not args[0]:\n            print(\"Please provide file name\")\n            print(\"\")\n            parser.print_help()\n            return -1\n        else:\n            if options.metaData:\n                metaData(args[0])\n            elif options.showData:\n                showData(xlFile=args[0], sheetName=options.sheetName)\n            else:\n                parser.print_help()\nif __name__ == \"__main__\":\n   main()\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recently I got interested in Python programming actually this was my first scripting language and its really a handy tool. I would like to share a small code which\u00a0\u00a0can access Excel(*.xls) file.\u00a0\u00a0I am assuming that you are having Ubuntu machine.\u00a0In order to run this code you need to have xlrd package and Python installed. If [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13"}],"version-history":[{"count":0,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions"}],"wp:attachment":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}