{"id":34411,"date":"2022-09-28T15:00:39","date_gmt":"2022-09-28T15:00:39","guid":{"rendered":"https:\/\/www.askpython.com\/?p=34411"},"modified":"2023-02-16T19:56:39","modified_gmt":"2023-02-16T19:56:39","slug":"python-read-excel","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/python-read-excel","title":{"rendered":"Python Read Excel- Different ways to read an excel file using Python"},"content":{"rendered":"\n<p>An Excel file is a spreadsheet file containing some cells in rows and columns (Tabular view) and can help in the arrangement, calculation, sorting, and managing of data. The data in the spreadsheet may be numeric, text, formulas, hyperlinks, functions, etc. An XLS file stores data as binary streams. It can only be created by the most popular MS Excel or other spreadsheet programs. The file format .xlsx always indicates an excel file on its own. <\/p>\n\n\n\n<p>The following image depicts an excel file created by the MS-excel program:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/Excel-file-by-MS-Excel.png\" alt=\"Excel File By MS Excel\" class=\"wp-image-34413\" width=\"445\" height=\"362\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/Excel-file-by-MS-Excel.png 668w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/Excel-file-by-MS-Excel-300x244.png 300w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><figcaption>Excel File By MS Excel<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to read Excel files using Python<\/h2>\n\n\n\n<p>To read excel files using Python, we need to use some popular Python modules and methods. Let&#8217;s understand those as well.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Python xlrd module <\/h3>\n\n\n\n<p><code>xlrd <\/code>is a python library or module to read and manage information from Excel files ( i.e. files in .<code>xlsx <\/code>format ). <strong>This Module will not be applicable for anything other than<\/strong> <strong>.<code>xlsx\u00a0<\/code>files.<\/strong><\/p>\n\n\n\n<p>Let&#8217;s have a quick look at how to install <code>xlrd <\/code>module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nC:\\Users\\pc&gt; pip install xlrd\n<\/pre><\/div>\n\n\n<p>As you are using python, You must have downloaded the<strong> <\/strong><a href=\"https:\/\/www.askpython.com\/python-modules\/python-pip\" data-type=\"post\" data-id=\"3848\"><strong><code>pip<\/code><\/strong> package installer<\/a>. You can also use another Python package manager of your choice. <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/installing-xlrd-module.png\" alt=\"Installing Xlrd Module\" class=\"wp-image-34414\" width=\"767\" height=\"244\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/installing-xlrd-module.png 880w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/installing-xlrd-module-300x96.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/installing-xlrd-module-768x245.png 768w\" sizes=\"auto, (max-width: 767px) 100vw, 767px\" \/><figcaption>Installing Xlrd Module<\/figcaption><\/figure>\n\n\n\n<p>In this method, We are going to use <code>xlwings <\/code>module along with the method associated with it (i.e. <code>xlwings.Book()<\/code> ). <\/p>\n\n\n\n<p>This method will automatically open our .xlsx in the background for us in its original program (i.e. MS-Excel) where we can operate and manage our data.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#importing the xlwings module as xw\nimport xlwings as xw\n\n#you can get ur excel file already been opened after execution of this command\nws = xw.Book(&quot;C:\\\\Users\\\\pc\\\\Desktop\\\\students.xlsx&quot;).sheets&#x5B;&#039;Sheet1&#039;]\n<\/pre><\/div>\n\n\n<p>From the above code snippet, We can get our Excel automatically opened on our desktop where we can access it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Python pandas module <\/h3>\n\n\n\n<p><strong><a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/python-pandas-module-tutorial\" data-type=\"post\" data-id=\"2986\">Pandas<\/a><\/strong> is an open-source Python library or module that provides in-built high-performance data structures and data analysis tools. It is most preferably used to analyze data along with two other core python libraries- <code>Matplotlib <\/code>for data visualization and <code>NumPy <\/code>for mathematical operations. <\/p>\n\n\n\n<p>We are going to install this module in the same way as our previous module using the pip installer as follows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nC:\\Users\\pc&gt; pip install pandas\n<\/pre><\/div>\n\n\n<p>The above code snippet will install the <code>pandas<\/code> module for us as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"216\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/installing-panda-module-1024x216.png\" alt=\"Installing Panda Module\" class=\"wp-image-34415\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/installing-panda-module-1024x216.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/installing-panda-module-300x63.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/installing-panda-module-768x162.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/installing-panda-module.png 1351w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Installing Panda Module<\/figcaption><\/figure>\n\n\n\n<p>To read excel files, let&#8217;s run the following snippet of code. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# importing pandas module as pd\nimport pandas as pd\n\n#using read_excel() method to read our excel file and storing the same in the variable named &quot;df &quot;\ndf = pd.read_excel(&quot;C:\\\\Users\\\\pc\\\\Desktop\\\\students.xlsx&quot;)\n\n#printing our spreadsheet using print() method\nprint(df)\n<\/pre><\/div>\n\n\n<p>In the above method, We are using <code>read_excel<\/code>() method to read our .<code>xlsx <\/code>file. We can use this method along with the pandas module as <code>panda.read_excel<em>()<\/em><\/code>&nbsp;to read the excel file data into a DataFrame object (Here it is &#8216; <code>df <\/code>&#8216;). <\/p>\n\n\n\n<p>The above code snippet will print our spreadsheet as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"693\" height=\"201\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/read_excel-method.png\" alt=\"Read Excel Method\" class=\"wp-image-34417\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/read_excel-method.png 693w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/read_excel-method-300x87.png 300w\" sizes=\"auto, (max-width: 693px) 100vw, 693px\" \/><figcaption>Read Excel Method<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Using Python openpyxl module<\/h3>\n\n\n\n<p><code>Openpyxl <\/code>is a Python library or module used to read or write from an Excel file. This module needs to be installed to use certain methods like load_workbook(), otherwise, we can&#8217;t use those methods, It will throw <code>error<\/code>. Let&#8217;s install this module using our command prompt. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nC:\\Users\\pc&gt; pip install openpyxl\n<\/pre><\/div>\n\n\n<p>The above code snippet will install our <code>openpyxl <\/code>module as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"764\" height=\"247\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/installing-openpyxl-module.png\" alt=\"Installing Openpyxl Module\" class=\"wp-image-34416\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/installing-openpyxl-module.png 764w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/installing-openpyxl-module-300x97.png 300w\" sizes=\"auto, (max-width: 764px) 100vw, 764px\" \/><figcaption>Installing Openpyxl Module<\/figcaption><\/figure>\n\n\n\n<p>In our second method, We are going to use our openpyxl module along with load_workbook() method as our following code snippet.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# importing openpyxl module \nimport openpyxl\n\n#using load_workbook() method to read our excel file and storing to dataframe object table1\ntable1 = openpyxl.load_workbook(&quot;C:\\\\Users\\\\pc\\\\Desktop\\\\students.xlsx&quot;)\n\n#To access the table1 we need to activate and store to an another object (Here it is table2)\ntable2 = table1.active\n\nfor row in range(1, table2.max_row):\n    for col in table2.iter_cols(1, table2.max_column):\n        print(col&#x5B;row].value, end = &quot; &quot;)\n    print(&quot;\\n&quot;)\n\n<\/pre><\/div>\n\n\n<p>In the above code snippet, We are using load_workbook() method to read our required excel file along with openpyxl module. <strong>We couldn&#8217;t use this method without importing this library or module. Not only this<\/strong>, This module is responsible for reading the location as a <strong>parameter( Here it is &#8220;C:\\Users\\pc\\Desktop\\students.xlsx&#8221; ) in our read_excel() method.<\/strong> <\/p>\n\n\n\n<p>After reading our excel file and assigning it to table1, It needs to be activated. Otherwise, If we print the table1, the following output occurs.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/printing-table1.png\" alt=\"Printing Table1\" class=\"wp-image-34418\" width=\"713\" height=\"45\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/printing-table1.png 776w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/printing-table1-300x19.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/printing-table1-768x48.png 768w\" sizes=\"auto, (max-width: 713px) 100vw, 713px\" \/><figcaption>Printing Table1<\/figcaption><\/figure>\n\n\n\n<p>We are going to access table2 by using for loop as the above code snippet. We will get our results as follows.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/output-by-method-2.png\" alt=\"Output By Method 2\" class=\"wp-image-34419\" width=\"512\" height=\"216\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/output-by-method-2.png 652w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/09\/output-by-method-2-300x127.png 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><figcaption>Output By Method 2<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, We covered Different methods to read our Excel file using Python. We discussed some popular Modules along with some required methods of Python for our appropriate output. Hope You must have practiced and enjoyed our code snippets. We must visit again with some more exciting topics.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>An Excel file is a spreadsheet file containing some cells in rows and columns (Tabular view) and can help in the arrangement, calculation, sorting, and managing of data. The data in the spreadsheet may be numeric, text, formulas, hyperlinks, functions, etc. An XLS file stores data as binary streams. It can only be created by [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":34434,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-34411","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/34411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=34411"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/34411\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/34434"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=34411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=34411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=34411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}