{"id":2040,"date":"2022-03-23T11:46:10","date_gmt":"2022-03-23T11:46:10","guid":{"rendered":"https:\/\/www.confianzit.com\/cit-blog\/?p=2040"},"modified":"2023-09-14T10:58:17","modified_gmt":"2023-09-14T10:58:17","slug":"how-to-read-xlsx-files-in-python","status":"publish","type":"post","link":"https:\/\/www.confianzit.com\/cit-blog\/how-to-read-xlsx-files-in-python\/","title":{"rendered":"How to Read XLSX Files in Python"},"content":{"rendered":"<p>[et_pb_section fb_built=&#8221;1&#8243; _builder_version=&#8221;4.16&#8243; global_colors_info=&#8221;{}&#8221;][et_pb_row column_structure=&#8221;2_3,1_3&#8243; _builder_version=&#8221;4.16&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221; global_colors_info=&#8221;{}&#8221;][et_pb_column type=&#8221;2_3&#8243; _builder_version=&#8221;4.16&#8243; custom_padding=&#8221;|||&#8221; global_colors_info=&#8221;{}&#8221; custom_padding__hover=&#8221;|||&#8221;][et_pb_text module_class=&#8221;blog-left-content&#8221; _builder_version=&#8221;4.21.0&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221; hover_enabled=&#8221;0&#8243; global_colors_info=&#8221;{}&#8221; sticky_enabled=&#8221;0&#8243;]<\/p>\n<p><!-- divi:paragraph -->XLSX is the spreadsheet file format used by Microsoft Excel. These files store data in a less structured way than CSV or TXT files.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->You might need to read or write XLSX files in Python for many reasons. One of the most common reasons is that it is a standard format for storing data. Other reasons include the following:<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:list --><\/p>\n<ul>\n<li>You want to parse an Excel file into an array of rows and columns<\/li>\n<li>You want to extract information from an Excel file that is not readily available in a tabular format (e.g., data from charts)<\/li>\n<li>You want to perform calculations with the data contained within an Excel file<\/li>\n<\/ul>\n<p><!-- \/divi:list --><\/p>\n<p><!-- divi:paragraph -->There are a few different methods to perform the task of writing XLSX files in Python, no matter what your reason is. In this article, we\u2019ll go through two popular methods step by step to see which is the best method for you.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:heading --><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_62 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.confianzit.com\/cit-blog\/how-to-read-xlsx-files-in-python\/#What_is_an_XLSX_File\" title=\"What is an XLSX File?\">What is an XLSX File?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.confianzit.com\/cit-blog\/how-to-read-xlsx-files-in-python\/#What_is_Python\" title=\"What is Python?\">What is Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.confianzit.com\/cit-blog\/how-to-read-xlsx-files-in-python\/#Method_2_Pandas\" title=\"Method #2: Pandas\">Method #2: Pandas<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.confianzit.com\/cit-blog\/how-to-read-xlsx-files-in-python\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.confianzit.com\/cit-blog\/how-to-read-xlsx-files-in-python\/#Talk_to_our_experts_now\" title=\"    Talk to our experts now  \">    Talk to our experts now  <\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.confianzit.com\/cit-blog\/how-to-read-xlsx-files-in-python\/#Talk_To_Our_Experts_Now\" title=\"Talk To Our Experts Now\n\t\">Talk To Our Experts Now\n\t<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"What_is_an_XLSX_File\"><\/span>What is an XLSX File?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><!-- \/divi:heading --><\/p>\n<p><!-- divi:paragraph -->An XLSX file is Microsoft\u2019s default file type when creating spreadsheets in modern versions of Excel. XLSX files can be opened using various programs, much like the DOCX file format in Word.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Microsoft introduced XLSX files due to the rise of competition from other business applications, including Open Office. It replaced the proprietary XLS file format that was used previously.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:heading --><\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_is_Python\"><\/span>What is Python?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><!-- \/divi:heading --><\/p>\n<p><!-- divi:paragraph -->Python is a general-purpose programming language often used for scientific computing, data analysis, and machine learning. It\u2019s also popular for <a href=\"https:\/\/www.confianzit.com\/web-design-development-company\" class=\"rank-math-link\">web development<\/a>, and it\u2019s often used as a scripting language.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Python is a high-level, general-purpose programming language that\u2019s interpreted. This means it can run without being compiled first and that it provides support for dynamic typing. The clean syntax of some Python features primarily arises from its coherent design philosophy.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Python also supports multiple programming paradigms, like object-oriented programming and procedural programming.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Many other open-source projects have been built using Python, including the Apache web server and the Matplotlib library, which NASA uses to create data graphs from satellites such as COBE and WMAP.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->You can download Python interpreters for all major operating systems from the Python website.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph {\"fontSize\":\"medium\"} --><\/p>\n<p class=\"has-medium-font-size\"><strong>Two Options for Reading XLSX Files in Python<\/strong><\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Two common ways to read XLSX files in Python are OpenPyXL and Pandas. This section will outline the steps you can take for each one.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph {\"style\":{\"typography\":{\"fontSize\":30}}} --><\/p>\n<p style=\"font-size: 30px;\"><strong>Method #1: OpenPyXL<\/strong><\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->OpenPyXL is a Python library created for reading and writing Excel 2010 xlsx\/xlsm\/xltx\/xltm files. It can read both the .xlsx and .xlsm file formats, which includes support for charts, graphs, and other data visualizations.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->OpenPyXL provides a set of classes representing the various objects in an Excel worksheet and allows reading, modifying, creating, and writing spreadsheets.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->The API is designed to be intuitive and easy to learn. It is mainly based on the semantics of a Worksheet object with additional support for formatting, writing, and reading data.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->OpenPyXL can also write out to the Excel binary formats (.xlsb\/.xlsm) as well as the legacy spreadsheet (.csv) format.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->The library is free software that was released under the Apache License 2.0.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Note: All photos in this section are credited to https:\/\/www.marsja.se\/your-guide-to-reading-excel-xlsx-files-in-python\/<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph --><strong>Step 1<\/strong><\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->The first thing you need to do (and this goes for both methods on the list) is download Python. There are a lot of different versions of Python, so make sure you find one that suits your needs.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph --><strong>Step 2<\/strong><\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Next, download openpyxl from their website using pip (Python\u2019s standard package manager). Openpyxl recommends doing this in a Python vitualenv without system packages.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph --><strong>Step 3<\/strong><\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Import the modules you need: openpyxk and <a href=\"https:\/\/docs.python.org\/3\/library\/pathlib.html#pathlib.Path\" target=\"_blank\" rel=\"noopener\">Path<\/a>.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:image {\"id\":2041,\"sizeSlug\":\"large\",\"linkDestination\":\"media\"} --><\/p>\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"468\" height=\"65\" src=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image1.png\" alt=\"\" class=\"wp-image-2041\" srcset=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image1.png 468w, https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image1-300x42.png 300w\" sizes=\"(max-width: 468px) 100vw, 468px\" \/><\/a><\/figure>\n<p><!-- \/divi:image --><\/p>\n<p><!-- divi:paragraph --><strong>Step 4<\/strong><\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Next, we\u2019ll create a variable that points at the location and filename of the Excel file we want to import. Here, we will use Path:<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:image {\"id\":2042,\"sizeSlug\":\"large\",\"linkDestination\":\"media\"} --><\/p>\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"466\" height=\"97\" src=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image2.png\" alt=\"\" class=\"wp-image-2042\" srcset=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image2.png 466w, https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image2-300x62.png 300w\" sizes=\"(max-width: 466px) 100vw, 466px\" \/><\/a><\/figure>\n<p><!-- \/divi:image --><\/p>\n<p><!-- divi:paragraph --><strong>Step 5<\/strong><\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->The last step is to use the active method to read the active sheet!<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:image {\"id\":2043,\"sizeSlug\":\"large\",\"linkDestination\":\"media\"} --><\/p>\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"609\" height=\"86\" src=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image3.png\" alt=\"\" class=\"wp-image-2043\" srcset=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image3.png 609w, https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image3-480x68.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 609px, 100vw\" \/><\/a><\/figure>\n<p><!-- \/divi:image --><\/p>\n<p><!-- divi:paragraph -->For any further questions or for answers to troubleshooting questions, refer to the OpenPyXL tutorials.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:heading --><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Method_2_Pandas\"><\/span>Method #2: <a href=\"https:\/\/pandas.pydata.org\/\" target=\"_blank\" rel=\"noopener\">Pandas<\/a><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><!-- \/divi:heading --><\/p>\n<p><!-- divi:paragraph -->Pandas is a powerful Python library that enables data manipulation and analysis.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->The library is built on NumPy and provides data structures and operations for manipulating numerical tables, time series, and relational data. It is a module of the Python Data Analysis Library.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->It provides fast, flexible, and expressive data structures designed to make working with structured (tabular, multidimensional, potentially heterogeneous) and time-series data both easy and intuitive.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Some of the features of Pandas is:<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:list --><\/p>\n<ul>\n<li>Data alignment<\/li>\n<li>Time series analysis<\/li>\n<li>Data resampling<\/li>\n<li>Data splitting<\/li>\n<li>Data merging<\/li>\n<\/ul>\n<p><!-- \/divi:list --><\/p>\n<p><!-- divi:paragraph -->Pandas can read data from a variety of formats such as CSV, Excel, SQL databases, etc. It can also read data from the clipboard or any other source that we might want to specify.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->This section will show you how to read an Excel file in Python using Pandas.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph --><strong>Step 1<\/strong><\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->First, you need to import Pandas and tell it where the Excel file is located. We do this by importing Pandas and then specifying the location of our Excel file with the .xlsx extension.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Whatever code you use, you\u2019ll need to modify it based on this pathname.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph --><strong>Step 2<\/strong><\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Next, you\u2019ll need to important Pandas as pd. This step brings Pandas\u2019 data analysis library into your environment and tells Python that Pandas\u2019 alias is pd.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->import pandas as pd<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->df = PD.read_excel(xlFilePath)<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph --><strong>Step 3<\/strong><\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->At this point, you can run Python with your code to read the excel file!<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Use <a href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.read_excel.html\" target=\"_blank\" rel=\"noopener\">Pandas documentation<\/a> to customize this process for your specific needs.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->If you only want to read specific columns or rows, there are further steps you can follow. Both of these methods are only for reading excel sheets, but they provide jumping-off points for editing them and modifying data as well.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:heading --><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><!-- \/divi:heading --><\/p>\n<p><!-- divi:paragraph -->While these are two of the more popular ways to read XLSX files, there are other options if you need a hyper-specific use case. But we hope this has been a clear and concise explanation of how to read XLSX files in Python!<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->If you\u2019re looking for a more guided and one-on-one experience, don\u2019t hesitate to reach out! Our technical experts at <a href=\"https:\/\/www.confianzit.com\/\" class=\"rank-math-link\">Confianz Global<\/a> have a broad depth of experience and knowledge to help you with any IT questions you may have.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->Confianz Global Inc. has proven expertise in building applications using Python. We are a <a href=\"https:\/\/www.confianzit.com\/\" class=\"rank-math-link\">Software development company<\/a> based out of Charlotte, North Carolina &#8211; focused on <a href=\"https:\/\/www.confianzit.com\/odoo-implementation\" class=\"rank-math-link\">Odoo ERP implementation<\/a>, <a href=\"https:\/\/www.confianzit.com\/mobile-app-development\" class=\"rank-math-link\">Mobile Application development <\/a>and <a href=\"https:\/\/www.confianzit.com\/web-design-development-company\" class=\"rank-math-link\">Web application development<\/a>.<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p><!-- divi:paragraph -->So don\u2019t wait and <a href=\"https:\/\/www.confianzit.com\/contact-us\">contact us today<\/a>!<\/p>\n<p><!-- \/divi:paragraph --><\/p>\n<p>[\/et_pb_text][\/et_pb_column][et_pb_column type=&#8221;1_3&#8243; _builder_version=&#8221;4.16&#8243; custom_padding=&#8221;|||&#8221; global_colors_info=&#8221;{}&#8221; custom_padding__hover=&#8221;|||&#8221;][et_pb_code _builder_version=&#8221;4.21.0&#8243; _module_preset=&#8221;default&#8221; animation_style=&#8221;bounce&#8221; animation_direction=&#8221;top&#8221; sticky_position=&#8221;top&#8221; sticky_offset_top=&#8221;20px&#8221; sticky_limit_bottom=&#8221;column&#8221; sticky_transition=&#8221;off&#8221; motion_trigger_start=&#8221;top&#8221; global_module=&#8221;2151&#8243; locked=&#8221;off&#8221; global_colors_info=&#8221;{}&#8221;]<\/p>\n<div class=\"blog-floating-form\"><!-- [et_pb_line_break_holder] -->  <\/p>\n<h4><span class=\"ez-toc-section\" id=\"Talk_to_our_experts_now\"><\/span><!-- [et_pb_line_break_holder] -->    Talk to our experts now<!-- [et_pb_line_break_holder] -->  <span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><!-- [et_pb_line_break_holder] -->  \n<div class=\"wpcf7 no-js\" id=\"wpcf7-f1888-o1\" lang=\"en-US\" dir=\"ltr\">\n<div class=\"screen-reader-response\"><p role=\"status\" aria-live=\"polite\" aria-atomic=\"true\"><\/p> <ul><\/ul><\/div>\n<form action=\"\/cit-blog\/wp-json\/wp\/v2\/posts\/2040#wpcf7-f1888-o1\" method=\"post\" class=\"wpcf7-form init\" aria-label=\"Contact form\" novalidate=\"novalidate\" data-status=\"init\">\n<div style=\"display: none;\">\n<input type=\"hidden\" name=\"_wpcf7\" value=\"1888\" \/>\n<input type=\"hidden\" name=\"_wpcf7_version\" value=\"5.8.6\" \/>\n<input type=\"hidden\" name=\"_wpcf7_locale\" value=\"en_US\" \/>\n<input type=\"hidden\" name=\"_wpcf7_unit_tag\" value=\"wpcf7-f1888-o1\" \/>\n<input type=\"hidden\" name=\"_wpcf7_container_post\" value=\"0\" \/>\n<input type=\"hidden\" name=\"_wpcf7_posted_data_hash\" value=\"\" \/>\n<input type=\"hidden\" name=\"_wpcf7_recaptcha_response\" value=\"\" \/>\n<\/div>\n<div class=\"form-block\" style=\"    background: #fff;\">\n\t<h3 style=\"    background: #0C2464;\n    border-bottom: 5px solid #cecece;\n    border-radius: 5px 5px 90px 90px;\n    margin: 0 auto;\n    text-align: center;\n    padding: 20px;\n    color: #fff;    margin-bottom: 15px;\"><span class=\"ez-toc-section\" id=\"Talk_To_Our_Experts_Now\"><\/span><b>Talk To Our Experts Now<\/b>\n\t<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\t<div style=\"padding:20px;\">\n\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"your-name\"><input size=\"40\" class=\"wpcf7-form-control wpcf7-text wpcf7-validates-as-required your-name\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Name\" value=\"\" type=\"text\" name=\"your-name\" \/><\/span>\n\t\t<\/p>\n\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"your-email\"><input size=\"40\" class=\"wpcf7-form-control wpcf7-email wpcf7-validates-as-required wpcf7-text wpcf7-validates-as-email your-email\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Email\" value=\"\" type=\"email\" name=\"your-email\" \/><\/span>\n\t\t<\/p>\n\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"your-number\"><input size=\"40\" class=\"wpcf7-form-control wpcf7-tel wpcf7-validates-as-required wpcf7-text wpcf7-validates-as-tel your-number\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Phone Number\" value=\"\" type=\"tel\" name=\"your-number\" \/><\/span>\n\t\t<\/p>\n\t\t<p><span class=\"wpcf7-form-control-wrap\" data-name=\"message\"><textarea cols=\"40\" rows=\"10\" class=\"wpcf7-form-control wpcf7-textarea wpcf7-validates-as-required form-message\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Message\" name=\"message\"><\/textarea><\/span>\n\t\t<\/p>\n\t<span class=\"wpcf7-form-control-wrap recaptcha\" data-name=\"recaptcha\"><span data-sitekey=\"6LfFkQATAAAAAIYlZ_UH9UozO-OLkpAaWPWx6QtM\" class=\"wpcf7-form-control wpcf7-recaptcha g-recaptcha\"><\/span>\r\n<noscript>\r\n\t<div class=\"grecaptcha-noscript\">\r\n\t\t<iframe loading=\"lazy\" src=\"https:\/\/www.google.com\/recaptcha\/api\/fallback?k=6LfFkQATAAAAAIYlZ_UH9UozO-OLkpAaWPWx6QtM\" frameborder=\"0\" scrolling=\"no\" width=\"310\" height=\"430\">\r\n\t\t<\/iframe>\r\n\t\t<textarea name=\"g-recaptcha-response\" rows=\"3\" cols=\"40\" placeholder=\"reCaptcha Response Here\">\r\n\t\t<\/textarea>\r\n\t<\/div>\r\n<\/noscript>\r\n<\/span>\n\t\t<div class=\"form-buttons\">\n\t\t\t<p><input class=\"wpcf7-form-control wpcf7-submit has-spinner\" type=\"submit\" value=\"Get a free quote\" \/>\n\t\t\t<\/p>\n\t\t<\/div>\n\t<\/div>\n<\/div><div class=\"wpcf7-response-output\" aria-hidden=\"true\"><\/div>\n<\/form>\n<\/div>\n<!-- [et_pb_line_break_holder] --><\/div>\n<p>[\/et_pb_code][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>XLSX is the spreadsheet file format used by Microsoft Excel. These files store data in a less structured way than CSV or TXT files. You might need to read or write XLSX files in Python for many reasons. One of the most common reasons is that it is a standard format for storing data. Other [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":2060,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"<!-- wp:paragraph -->\n<p>XLSX is the spreadsheet file format used by Microsoft Excel. These files store data in a less structured way than CSV or TXT files.&nbsp;<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>You might need to read or write XLSX files in Python for many reasons. One of the most common reasons is that it is a standard format for storing data. Other reasons include the following:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list -->\n<ul><li>You want to parse an Excel file into an array of rows and columns&nbsp;<\/li><li>You want to extract information from an Excel file that is not readily available in a tabular format (e.g., data from charts)<\/li><li>You want to perform calculations with the data contained within an Excel file<\/li><\/ul>\n<!-- \/wp:list -->\n\n<!-- wp:paragraph -->\n<p>There are a few different methods to perform the task of writing XLSX files in Python, no matter what your reason is. In this article, we\u2019ll go through two popular methods step by step to see which is the best method for you.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:heading -->\n<h2>What is an XLSX File?<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>An XLSX file is Microsoft\u2019s default file type when creating spreadsheets in modern versions of Excel. XLSX files can be opened using various programs, much like the DOCX file format in Word.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Microsoft introduced XLSX files due to the rise of competition from other business applications, including Open Office. It replaced the proprietary XLS file format that was used previously.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:heading -->\n<h2>What is Python?<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p><a href=\"https:\/\/www.confianzit.com\/python-application-development\" class=\"rank-math-link\">Python<\/a> is a general-purpose programming language often used for scientific computing, data analysis, and machine learning. It\u2019s also popular for <a href=\"https:\/\/www.confianzit.com\/web-design-development-company\" class=\"rank-math-link\">web development<\/a>, and it\u2019s often used as a scripting language.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Python is a high-level, general-purpose programming language that\u2019s interpreted. This means it can run without being compiled first and that it provides support for dynamic typing. The clean syntax of some Python features primarily arises from its coherent design philosophy.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Python also supports multiple programming paradigms, like object-oriented programming and procedural programming.&nbsp;<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Many other open-source projects have been built using <a href=\"https:\/\/www.confianzit.com\/python-application-development\" class=\"rank-math-link\">Python<\/a>, including the Apache web server and the Matplotlib library, which NASA uses to create data graphs from satellites such as COBE and WMAP.&nbsp;<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>You can download Python interpreters for all major operating systems from the Python website.&nbsp;<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph {\"fontSize\":\"medium\"} -->\n<p class=\"has-medium-font-size\"><strong>Two Options for Reading XLSX Files in Python<\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Two common ways to read XLSX files in Python are OpenPyXL and Pandas. This section will outline the steps you can take for each one.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph {\"style\":{\"typography\":{\"fontSize\":30}}} -->\n<p style=\"font-size:30px\"><strong>Method #1: OpenPyXL<\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>OpenPyXL is a <a href=\"https:\/\/www.confianzit.com\/python-application-development\" class=\"rank-math-link\">Python<\/a> library created for reading and writing Excel 2010 xlsx\/xlsm\/xltx\/xltm files. It can read both the .xlsx and .xlsm file formats, which includes support for charts, graphs, and other data visualizations.&nbsp;<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>OpenPyXL provides a set of classes representing the various objects in an Excel worksheet and allows reading, modifying, creating, and writing spreadsheets.&nbsp;<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>The API is designed to be intuitive and easy to learn. It is mainly based on the semantics of a Worksheet object with additional support for formatting, writing, and reading data.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>OpenPyXL can also write out to the Excel binary formats (.xlsb\/.xlsm) as well as the legacy spreadsheet (.csv) format.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>The library is free software that was released under the Apache License 2.0.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Note: All photos in this section are credited to https:\/\/www.marsja.se\/your-guide-to-reading-excel-xlsx-files-in-python\/<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p><strong>Step 1<\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>The first thing you need to do (and this goes for both methods on the list) is download Python. There are a lot of different versions of Python, so make sure you find one that suits your needs.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p><strong>Step 2<\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Next, download openpyxl from their website using pip (Python\u2019s standard package manager). Openpyxl recommends doing this in a Python vitualenv without system packages.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p><strong>Step 3<\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Import the modules you need: openpyxk and <a href=\"https:\/\/docs.python.org\/3\/library\/pathlib.html#pathlib.Path\">Path<\/a>.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":2041,\"sizeSlug\":\"large\",\"linkDestination\":\"media\"} -->\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image1.png\"><img src=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image1.png\" alt=\"\" class=\"wp-image-2041\"\/><\/a><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p><strong>Step 4<\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Next, we\u2019ll create a variable that points at the location and filename of the Excel file we want to import. Here, we will use Path:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":2042,\"sizeSlug\":\"large\",\"linkDestination\":\"media\"} -->\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image2.png\"><img src=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image2.png\" alt=\"\" class=\"wp-image-2042\"\/><\/a><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p><strong>Step 5<\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>The last step is to use the active method to read the active sheet!<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":2043,\"sizeSlug\":\"large\",\"linkDestination\":\"media\"} -->\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image3.png\"><img src=\"https:\/\/www.confianzit.com\/cit-blog\/wp-content\/uploads\/2022\/03\/image3.png\" alt=\"\" class=\"wp-image-2043\"\/><\/a><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>For any further questions or for answers to troubleshooting questions, refer to the OpenPyXL tutorials. <\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:heading -->\n<h2>Method #2: <a href=\"https:\/\/pandas.pydata.org\/\">Pandas<\/a><\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Pandas is a powerful Python library that enables data manipulation and analysis.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>The library is built on NumPy and provides data structures and operations for manipulating numerical tables, time series, and relational data. It is a module of the Python Data Analysis Library.&nbsp;<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>It provides fast, flexible, and expressive data structures designed to make working with structured (tabular, multidimensional, potentially heterogeneous) and time-series data both easy and intuitive.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Some of the features of Pandas is:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list -->\n<ul><li>Data alignment<\/li><li>Time series analysis<\/li><li>Data resampling<\/li><li>Data splitting<\/li><li>Data merging<\/li><\/ul>\n<!-- \/wp:list -->\n\n<!-- wp:paragraph -->\n<p>Pandas can read data from a variety of formats such as CSV, Excel, SQL databases, etc. It can also read data from the clipboard or any other source that we might want to specify.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>This section will show you how to read an Excel file in <a href=\"https:\/\/www.confianzit.com\/python-application-development\" class=\"rank-math-link\">Python<\/a> using Pandas.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p><strong>Step 1<\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>First, you need to import Pandas and tell it where the Excel file is located. We do this by importing Pandas and then specifying the location of our Excel file with the .xlsx extension.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Whatever code you use, you\u2019ll need to modify it based on this pathname.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p><strong>Step 2<\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Next, you\u2019ll need to important Pandas as pd. This step brings Pandas\u2019 data analysis library into your environment and tells Python that Pandas\u2019 alias is pd.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>import pandas as pd<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>df = PD.read_excel(xlFilePath)<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p><strong>Step 3<\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>At this point, you can run Python with your code to read the excel file!<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Use <a href=\"https:\/\/pandas.pydata.org\/docs\/reference\/api\/pandas.read_excel.html\">Pandas documentation<\/a> to customize this process for your specific needs.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>If you only want to read specific columns or rows, there are further steps you can follow. Both of these methods are only for reading excel sheets, but they provide jumping-off points for editing them and modifying data as well.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:heading -->\n<h2>Conclusion<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>While these are two of the more popular ways to read XLSX files, there are other options if you need a hyper-specific use case. But we hope this has been a clear and concise explanation of how to read XLSX files in <a href=\"https:\/\/www.confianzit.com\/python-application-development\" class=\"rank-math-link\">Python<\/a>!<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>If you\u2019re looking for a more guided and one-on-one experience, don\u2019t hesitate to reach out! Our technical experts at <a href=\"https:\/\/www.confianzit.com\/\" class=\"rank-math-link\">Confianz Global<\/a> have a broad depth of experience and knowledge to help you with any IT questions you may have. <\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Confianz Global Inc. has proven expertise in building applications using Python. We are a <a href=\"https:\/\/www.confianzit.com\/\" class=\"rank-math-link\">Software development company<\/a> based out of Charlotte, North Carolina - focused on <a href=\"https:\/\/www.confianzit.com\/odoo-implementation\" class=\"rank-math-link\">Odoo ERP implementation<\/a>, <a href=\"https:\/\/www.confianzit.com\/mobile-app-development\" class=\"rank-math-link\">Mobile Application development <\/a>and <a href=\"https:\/\/www.confianzit.com\/web-design-development-company\" class=\"rank-math-link\">Web application development<\/a>.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>So don\u2019t wait and <a href=\"https:\/\/www.confianzit.com\/contact-us\">contact us today<\/a>!<\/p>\n<!-- \/wp:paragraph -->","_et_gb_content_width":"","footnotes":""},"categories":[7],"tags":[297,296,298,299,294,295],"_links":{"self":[{"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/posts\/2040"}],"collection":[{"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/comments?post=2040"}],"version-history":[{"count":8,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/posts\/2040\/revisions"}],"predecessor-version":[{"id":29084,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/posts\/2040\/revisions\/29084"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/media\/2060"}],"wp:attachment":[{"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/media?parent=2040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/categories?post=2040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.confianzit.com\/cit-blog\/wp-json\/wp\/v2\/tags?post=2040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}