Download Article
In-depth guide for opening Excel files on your PC or phone
Download Article
This wikiHow teaches you how to open an Excel file, and view the spreadsheet file’s contents. You can use a desktop spreadsheet program like Microsoft Excel, a web-based spreadsheet viewer like Google Sheets, or the mobile Excel app to open, view, and edit Excel spreadsheets on any computer, phone or tablet.
-
1
Find and right-click the Excel file you want to open. Find the spreadsheet file on your computer, and right-click on its name or icon to see your options on a drop-down menu.
-
2
Hover over Open with on the right-click menu. A list of available apps will pop up on a sub-menu.
Advertisement
-
3
Select Microsoft Excel on the «Open with» menu. This will launch Excel on your computer, and open the selected file.
- If you don’t see the Excel app here, click Other or Choose another app to see all your apps.
- If you don’t have Excel installed, check out the available subscription plans and get a free trial at https://products.office.com/en/excel.
- As an alternative, you can download and use a free, open-source office suite like Apache OpenOffice (https://www.openoffice.org) or LibreOffice (https://www.libreoffice.org).
Advertisement
-
1
Open Microsoft Excel Online in your internet browser. Type or paste https://office.live.com/start/Excel.aspx into the address bar, and press ↵ Enter or ⏎ Return on your keyboard.
- If you’re prompted, sign in with your Microsoft ID or Outlook account.
- You can use Excel online in any desktop or mobile internet browser.
-
2
Click the Upload a Workbook button on the top-right. This button looks like a blue, upwards arrow icon in the upper-right corner of the page. It will open your file navigator, and allow you to select a file from your computer.
-
3
Select the Excel file you want to open. Find your spreadsheet file in the file navigator window, and click on its name or icon to select it.
-
4
Click Open. This button is in the bottom-right corner of the file navigator pop-up. It will upload your spreadsheet file, and open it in Excel Online.
- You can view and edit your file in your internet browser.
Advertisement
-
1
Open Google Sheets in your internet browser. Type or paste https://docs.google.com/spreadsheets into the address bar, and press ↵ Enter or ⏎ Return on your keyboard.
- Alternatively, go to https://sheets.google.com. It will open the same page.
- If you’re not automatically signed in, log in with your Google account.
- You can use Google Sheets in any desktop or mobile internet browser.
-
2
Click the folder icon on the top-right. You can find this button in the upper-right corner of your spreadsheet list, next to the AZ button. It will open the «Open a file» window in a pop-up.
-
3
Click the Upload tab. You can find it on a tab bar below the «Upload a file» heading in the pop-up. This will allow you to open any Excel file from your computer.
- Alternatively, you can click the My Drive tab, and open a file from your Google Drive library.
-
4
Drag and drop your Excel file on the «Open a file» window. When you’re in the Upload tab, you can just drag and drop any spreadsheet file from your computer here.
- This will upload your Excel file to Google Sheets, and open it in your internet browser.
- Alternatively, click the blue Select a file from your device button, and select your file manually.
Advertisement
-
1
-
2
Tap Sign in later at the bottom. This option will allow you to use the mobile Excel app on your phone or tablet without signing in to your Microsoft account.
- Alternatively, enter your registered email, phone or Skype ID, and tap the green-and-white arrow to sign in to your account.
-
3
Tap the Open button. This button looks like a folder icon on a navigation bar. It will open your available file sources.
- On iPhone, it’s in the lower-right corner of your screen.
- On Android, it’s in the upper-left corner of your screen.
-
4
Select where your spreadsheet file is saved. Selecting a source here will open all the files saved to this location.
- If you’re opening a file saved to your phone or tablet’s local storage, select This Device or On My iPhone/iPad here.
-
5
Select the spreadsheet file you want to open. Tapping a file will open it in the Excel mobile app.
Advertisement
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Thanks for submitting a tip for review!
About This Article
Article SummaryX
1. Find and right-click the file.
2. Hover over Open with.
3. Select Microsoft Excel.
Did this summary help you?
Thanks to all authors for creating a page that has been read 63,704 times.
Is this article up to date?
Want to know about apps to open excel files? This article will provide all the necessary information. Excel is a pivotal tool for almost all spheres of life. It is used by everyone who wishes to keep their data in an organized and easy-to-access manner.
But there’s catch to this, you can’t access the excel smoothly on your mobile right? Here’s the solution. Tap the blue button below and explore the easiest excel on your mobile.
Check out the screenshot of Lio App – easy excel sheet on your mobile.
There are several apps that are available today to open excel files. For example, Lio makes your task of calculating and keeping track of things very simple and completely hassle-free. Let’s look at it in detail along with other apps.
THE TOP PICKS OF APPS TO OPEN EXCEL DOCUMENTS
Lio
If you are looking for an easy app to keep a track of your things, then Lio is the answer. The amazing application is made for anybody and everybody under the sky. Whether you are a business owner, shop owner, taxi driver, teacher, student or anyone else, Lio can help you get more organised.
The app offers many templates that one can select based on their needs and requirements. You can share the file with others and work with different people on the same file and see real-time changes. You can also upload images and add them to these files to better manage your task.
Whatever you do, Lio is here for you.
the idea is simple, if you have data then lio has the templates for you. Easy excel on your fingertips.
AndrOpen Office
AndrOpen Office is a legit Open Office Software. It offers a tonne of features. It is a cellular port OpenOffice System. It has a phrase processor as well as a presentation function. The main feature is the spreadsheet. The app even lets you draw and even do calculations on the page.
Considering the functionality of the spreadsheet. It is a great cellular app and offers decent competition to favourably the biggest players in the show even if it doesn’t have all the functionality features of the others.
The process of importing Microsoft Excel files in XLS or XLT format is fairly simple. It is a brilliant chance for Google and Microsoft solutions. Access to the app is free
OfficeSuite
OfficeSuite is a tough competitor. It gives intense competition to Google Drive and Microsoft Office on Mobile. It has been around for a long time and incorporates a variety of functions and features. This provides a complete workplace experience. The spreadsheet functionality is also great and has tutorials for assistance with Excel documents and more.
Why Office Suite when you can have Lio.
Explore 100+ readymade templates, 10 indian languages and other master features that other excel sheets doesn’t have.
It is pretty easy to use, and is fairly good enough as the loose model makes it a bit dry in terms of functionality. The app comes with a subscription fee. It is charged every month. It is about 5 dollars every month. You can take an annual subscription for 30 dollars to save some money. The premium subscription completely removes ads and provides preset report formats and offers access to an extra set of functions. The office suite app has about 154 report formats.
The information is applicable for both Android, iOS, or PC. The app uses the same license across all platforms. Once you take the subscription, the app can be used on any device
Irrespective of the operating system.
SmartOffice
The SmartOffice is a long-term player in the field. Considering the competition the app seems to be a bit mature in terms of functionality. The app has a long list of features and capabilities. The spreadsheet functionality is also quite good. However, the app is not as effective as many of its rivals. It is completely loose which ensures you get enough for the money you paid.
Nowadays, Excel apps in terms of usability have fared far better than SmartOffice. Google Sheets is one of the best among them. But if you are a person who does a lot of offline editing then this software is a great pick for you.
It utilizes Google Drive and Dropbox as a means to store the data. Both of them act as a cloud garage. Since the method is not as effective, people have moved to different apps. However, a few long term users are still using SmartOffice. They enjoy using it.
Google Sheets
A time was there when creating excel sheets was a tussle. It always involved sitting down at a desk to make a spreadsheet. However, with the introduction of Google sheets making spreadsheets has never been easier. You can create and modify sheets using your mobile. How more compact should it get.
Google Sheets, sync with Google Drive, and the app could be used across platforms along with the web. The files work well with Microsoft Office and provide the same functionality as the trusted Microsoft excel. Moreover, the app automatically saves each modification you make.
Why google sheets when you can have Lio.
Explore 100+ readymade templates, 10 indian languages and other master features that other tool doesn’t have.
The app incorporates a collaborative function where multiple people can create and modify the same spreadsheet. Offers loads of customization features and capabilities. Many consider that microsoft excel is the king of spreadsheet software but Google Shees does it graeter and better. The App has a short learning curve and could be mastered even by a newbie
There is no subscription as such. All functionalities can be used for free unless your it stays within your drive space of 15GB. If you want more drive space then you have to pay for it.
WPS Office
We can say it is a great workplace app. It is frequently used by people. The app can be used free of charge but you need to deal with a lot of annoying ads. I mean a lot. To unlock the full functionality and get rid of the ads you have to pay for a subscription. You will do so because you dont want ads annoying especially when you are up doing something productive.
With respect to functionality, It has a list of functions that help access Microsoft Excel. It also has some extra gimmicks for Slack, Zoom and Google drive. This makes the app good as it integrates the functionality of storage and ease of use during online meetings. All the proceedings received by the app are used for marketing the app outside and within a loose version.
Quip
Quip is well developed excel software. An ideal alternative for google groups. The app has all the capabilities needed for a workspace. However, it requires a subscription to be purchased. The app can be used to edit a document with a group of people. The app also allows you to coordinate with other members using the chat feature that is inbuilt.
Possess as many as 400 features for the spreadsheets. The app can be used to include spreadsheets within any document. The subscription plans primarily focus on group users. But can be utilized for non-public use as well.
Docs To Go
Docs to Go is a relatively good option for use in a workspace. It has all the basic features of a phrase processor, a worksheet and a shows function. The spreadsheet function is ideal for running all Microsoft Office files and offers a lot of customization. This feature makes it ideal for less difficult tasks. It has 111 unique functions and a number of customization for columns and sorting.
The app finds it difficult to handle difficult, complex styling imported from different apps. If the easy ones are used, the app works fairly well
It boasts one hundred and eleven unique functions, numerous column customization, and sorting.
How to Open XLS Files
XLS is the specified format for any kind of Microsoft Excel document. This file format is supported by all spreadsheet apps and works quite well to examine documents.
If you are constantly on a run and want to edit your spreadsheet then and where you are comfortable. You can get this done through Google Sheets. The modified data will be synced across all devices with the same user account and stored in Google drive. You can use any app mentioned above to modify the XLS documents on your device
Your choice of the right app depends on whether you want your data saved on one device or should be synced across all devices. The benefit of having data synced is that you can modify the data on any device. You can choose based on the software and device you use to work on your files.
All the apps mentioned in the article is compatible with mobile phone and could be opened when and edited whenever needed.
Easily accessible on any device.
This is the catch, lio is a mobile first tool and allows you to work more smoothly on mobile.
Google Drive
1. Upload your spreadsheet to Google Drive from your pc. The old cellular model is not available anymore and will not assist in adding documents outside your device
2. Using the phone’s search engine go to Google Drive using the link drive.google.com.You will be taken to the cellular model page. If you want you can switch to the Web app model in Google drive.
3. Once the upload is complete Open the spreadsheet. You can view the spreadsheet and edit the contents once you have it uploaded
Documents To Go
- Install the Docs to Go app from App Store for Apple devices or Google Playstore for your android phone. This App comes pre-installed with Blackberry devices. The App requires a subscription to be purchased. The fees depend on the device you use
- Now launch the App. If you have the XLS document as an email attachment you can click on the attachment and select open with Docs to Go App. This will automatically
- Sync your phone with a PC using a USB to duplicate the files on pc to your phone. This ensures a backup for your XLS files
Also Read: Making Attendance Sheets In Excel For Employees/Students
Spreadsheet For IOS
- Install the spreadsheet app on your iPhone and launch the app on your Pc
- Select the settings icon and appears like a gear and slide the On button after you enable the file sharing option. At the lowest part of the screen, you can see an IP deal which you should hook up with the application
- Enter the IP address using your web browser from your PC. Your phone should be linked with the identical community as your pc
- Select the document to download out of your phone, or add a new XLS document. This way, you can sync dat using the IP address
First, you need to install Spreadsheet to your iPhone or a different iOS tool and open it to your pc.
Mention the current file extensions?
Format | Extensions |
Excel Workbook | .xlxs |
Excel add-in | .xlam |
Excel Macro enabled Template | .xltm |
Excel Binary Workbook | .xlxb |
Excel Macro enabled Workbook | .xlxm |
LIST THE STEPS TO DISPLAY EXCEL FILES IN AN APP
Step 1: Click on an info source. The app mainly consists of the information on the Excel file, Google sheet etc. It may also contain sales force, Microsoft dynamics CRM and plenty more
Step 2: Select applicable enter and output cells.
Step 3: Choose a layout for your calculation app.
Step 4: Share the App.
Also Read: In-Depth Look At Spreadsheets VS Database
How Does Lio help?
Lio is a useful application that helps in maintaining records in the form of tables and spreadsheets.
It helps to record the activities, which are easily accessible 24/7. Various templates are provided for specified purposes which are handy to use for anyone.
The mobile integrated app is convenient and the users find it not so difficult to use. It is one of the best for managing inventory, ordering, and holding it to be available for the right time.
Not downloaded the Lio App yet? Here is how you can start with Lio App.
Step 1: Select the Language you want to work on. Lio for Android
Step 2: Create your account using your Phone Number or Email Id.
Verify the OTP and you are good to go.
Step 3: Select a template in which you want to add your data.
Add your Data with our Free Cloud Storage.
Step 4: All Done? Share and Collaborate with your contacts.
Frequently Asked Questions
How can one save their excel files?
In Microsoft Excel, workbooks are saved as Excel Workbooks (.xlsx) consequently.
You have the choice to change the file format to any other file format that is recommended in Excel.
Where can you find your unsaved Excel Files?
Excel saves the files you forget to save in the folder mentioned below. C:Users\AppDataLocalMicrosoftOfficeUnsavedFiles. You also can get the right of entry to the documents from this folder.
Do we have a choice to backup Excel Files?
Excel’s Auto Backup is a completely beneficial characteristic that permits you to retrieve a formerly stored model of your workbook.
Saving a backup reproduction can defend your paintings in case you by chance keep modifications which you do now no longer need to hold or delete the unique file.
Go to FILE and then click on ‘ Save As’
How do you Turn on Auto-save in Excel Files?
Below are the stairs to show on Auto-keep in Excel:
– Tap on the File tab.
– Then you need to click on Options.
– In the Excel Options conversation box, click on the Save alternative at the left.
– Check the alternative – ‘Save AutoRecover facts every’ checkbox.
– Check the Option – “AutoSave OneDrive and SharePoint Online documents through default in Excel’. …
– Lastly, click ok.
What do you understand by the term pivoting?
A pivot table in Excel is the production or resumé of your actual table with supply data.
A pivot desk can offer short solutions to questions on your table which in any other case is most effective which requires using complex formulas.
Conclusion
It is the best way to store the information in a way that when you share it, others can access it easily. The purpose of getting information is to examine and to get insights out of it. Excel has a number of the maximum effective gear to examine data.
When you’ve got facts with lots of rows, you may insert a pivot desk out of that information and create a precise desk. You can also obtain free templates that you can download and create as well.
The system of cleansing and remodelling statistics takes a long time and it may be a tedious system.
For this, Microsoft has delivered the energy question: a statistics transformation and cleansing engine. You may load statistics from a couple of sources, and remodel them, after which the load is returned to the worksheet. I hope this article is beneficial to you and you have learnt all about apps to open excel files.
Microsoft Excel is one of the most powerful spreadsheet software applications in the world, and it has become critical in all business processes. Companies across the world, both big and small, are using Microsoft Excel to store, organize, analyze, and visualize data.
As a data professional, when you combine Python with Excel, you create a unique data analysis bundle that unlocks the value of the enterprise data.
In this tutorial, we’re going to learn how to read and work with Excel files in Python.
After you finish this tutorial, you’ll understand the following:
- Loading Excel spreadsheets into pandas DataFrames
- Working with an Excel workbook with multiple spreadsheets
- Combining multiple spreadsheets
- Reading Excel files using the
xlrd
package
In this tutorial, we assume you know the fundamentals of pandas DataFrames. If you aren’t familiar with the pandas library, you might like to try our Pandas and NumPy Fundamentals – Dataquest.
Let’s dive in.
Reading Spreadsheets with Pandas
Technically, multiple packages allow us to work with Excel files in Python. However, in this tutorial, we’ll use pandas and xlrd
libraries to interact with Excel workbooks. Essentially, you can think of a pandas DataFrame as a spreadsheet with rows and columns stored in Series objects. Traversability of Series as iterable objects allows us to grab specific data easily. Once we load an Excel workbook into a pandas DataFrame, we can perform any kind of data analysis on the data.
Before we proceed to the next step, let’s first download the following spreadsheet:
Sales Data Excel Workbook — xlsx ver.
The Excel workbook consists of two sheets that contain stationery sales data for 2020 and 2021.
NOTE
Although Excel spreadsheets can contain formula and also support formatting, pandas only imports Excel spreadsheets as flat files, and it doesn’t support spreadsheet formatting.
To import the Excel spreadsheet into a pandas DataFrame, first, we need to import the pandas package and then use the read_excel()
method:
import pandas as pd
df = pd.read_excel('sales_data.xlsx')
display(df)
OrderDate | Region | Rep | Item | Units | Unit Cost | Total | Shipped | |
---|---|---|---|---|---|---|---|---|
0 | 2020-01-06 | East | Jones | Pencil | 95 | 1.99 | 189.05 | True |
1 | 2020-02-09 | Central | Jardine | Pencil | 36 | 4.99 | 179.64 | True |
2 | 2020-03-15 | West | Sorvino | Pencil | 56 | 2.99 | 167.44 | True |
3 | 2020-04-01 | East | Jones | Binder | 60 | 4.99 | 299.40 | False |
4 | 2020-05-05 | Central | Jardine | Pencil | 90 | 4.99 | 449.10 | True |
5 | 2020-06-08 | East | Jones | Binder | 60 | 8.99 | 539.40 | True |
6 | 2020-07-12 | East | Howard | Binder | 29 | 1.99 | 57.71 | False |
7 | 2020-08-15 | East | Jones | Pencil | 35 | 4.99 | 174.65 | True |
8 | 2020-09-01 | Central | Smith | Desk | 32 | 125.00 | 250.00 | True |
9 | 2020-10-05 | Central | Morgan | Binder | 28 | 8.99 | 251.72 | True |
10 | 2020-11-08 | East | Mike | Pen | 15 | 19.99 | 299.85 | False |
11 | 2020-12-12 | Central | Smith | Pencil | 67 | 1.29 | 86.43 | False |
If you want to load only a limited number of rows into the DataFrame, you can specify the number of rows using the nrows
argument:
df = pd.read_excel('sales_data.xlsx', nrows=5)
display(df)
OrderDate | Region | Rep | Item | Units | Unit Cost | Total | Shipped | |
---|---|---|---|---|---|---|---|---|
0 | 2020-01-06 | East | Jones | Pencil | 95 | 1.99 | 189.05 | True |
1 | 2020-02-09 | Central | Jardine | Pencil | 36 | 4.99 | 179.64 | True |
2 | 2020-03-15 | West | Sorvino | Pencil | 56 | 2.99 | 167.44 | True |
3 | 2020-04-01 | East | Jones | Binder | 60 | 4.99 | 299.40 | False |
4 | 2020-05-05 | Central | Jardine | Pencil | 90 | 4.99 | 449.10 | True |
Skipping a specific number of rows from the begining of a spreadsheet or skipping over a list of particular rows is available through the skiprows
argument, as follows:
df = pd.read_excel('sales_data.xlsx', skiprows=range(5))
display(df)
2020-05-05 00:00:00 | Central | Jardine | Pencil | 90 | 4.99 | 449.1 | True | |
---|---|---|---|---|---|---|---|---|
0 | 2020-06-08 | East | Jones | Binder | 60 | 8.99 | 539.40 | True |
1 | 2020-07-12 | East | Howard | Binder | 29 | 1.99 | 57.71 | False |
2 | 2020-08-15 | East | Jones | Pencil | 35 | 4.99 | 174.65 | True |
3 | 2020-09-01 | Central | Smith | Desk | 32 | 125.00 | 250.00 | True |
4 | 2020-10-05 | Central | Morgan | Binder | 28 | 8.99 | 251.72 | True |
5 | 2020-11-08 | East | Mike | Pen | 15 | 19.99 | 299.85 | False |
6 | 2020-12-12 | Central | Smith | Pencil | 67 | 1.29 | 86.43 | False |
The code above skips the first five rows and returns the rest of the data. Instead, the following code returns all the rows except for those with the mentioned indices:
df = pd.read_excel('sales_data.xlsx', skiprows=[1, 4,7,10])
display(df)
OrderDate | Region | Rep | Item | Units | Unit Cost | Total | Shipped | |
---|---|---|---|---|---|---|---|---|
0 | 2020-02-09 | Central | Jardine | Pencil | 36 | 4.99 | 179.64 | True |
1 | 2020-03-15 | West | Sorvino | Pencil | 56 | 2.99 | 167.44 | True |
2 | 2020-05-05 | Central | Jardine | Pencil | 90 | 4.99 | 449.10 | True |
3 | 2020-06-08 | East | Jones | Binder | 60 | 8.99 | 539.40 | True |
4 | 2020-08-15 | East | Jones | Pencil | 35 | 4.99 | 174.65 | True |
5 | 2020-09-01 | Central | Smith | Desk | 32 | 125.00 | 250.00 | True |
6 | 2020-11-08 | East | Mike | Pen | 15 | 19.99 | 299.85 | False |
7 | 2020-12-12 | Central | Smith | Pencil | 67 | 1.29 | 86.43 | False |
Another useful argument is usecols
, which allows us to select spreadsheet columns with their letters, names, or positional numbers. Let’s see how it works:
df = pd.read_excel('sales_data.xlsx', usecols='A:C,G')
display(df)
OrderDate | Region | Rep | Total | |
---|---|---|---|---|
0 | 2020-01-06 | East | Jones | 189.05 |
1 | 2020-02-09 | Central | Jardine | 179.64 |
2 | 2020-03-15 | West | Sorvino | 167.44 |
3 | 2020-04-01 | East | Jones | 299.40 |
4 | 2020-05-05 | Central | Jardine | 449.10 |
5 | 2020-06-08 | East | Jones | 539.40 |
6 | 2020-07-12 | East | Howard | 57.71 |
7 | 2020-08-15 | East | Jones | 174.65 |
8 | 2020-09-01 | Central | Smith | 250.00 |
9 | 2020-10-05 | Central | Morgan | 251.72 |
10 | 2020-11-08 | East | Mike | 299.85 |
11 | 2020-12-12 | Central | Smith | 86.43 |
In the code above, the string assigned to the usecols
argument contains a range of columns with :
plus column G separated by a comma. Also, we’re able to provide a list of column names and assign it to the usecols
argument, as follows:
df = pd.read_excel('sales_data.xlsx', usecols=['OrderDate', 'Region', 'Rep', 'Total'])
display(df)
OrderDate | Region | Rep | Total | |
---|---|---|---|---|
0 | 2020-01-06 | East | Jones | 189.05 |
1 | 2020-02-09 | Central | Jardine | 179.64 |
2 | 2020-03-15 | West | Sorvino | 167.44 |
3 | 2020-04-01 | East | Jones | 299.40 |
4 | 2020-05-05 | Central | Jardine | 449.10 |
5 | 2020-06-08 | East | Jones | 539.40 |
6 | 2020-07-12 | East | Howard | 57.71 |
7 | 2020-08-15 | East | Jones | 174.65 |
8 | 2020-09-01 | Central | Smith | 250.00 |
9 | 2020-10-05 | Central | Morgan | 251.72 |
10 | 2020-11-08 | East | Mike | 299.85 |
11 | 2020-12-12 | Central | Smith | 86.43 |
The usecols
argument accepts a list of column numbers, too. The following code shows how we can pick up specific columns using their indices:
df = pd.read_excel('sales_data.xlsx', usecols=[0, 1, 2, 6])
display(df)
OrderDate | Region | Rep | Total | |
---|---|---|---|---|
0 | 2020-01-06 | East | Jones | 189.05 |
1 | 2020-02-09 | Central | Jardine | 179.64 |
2 | 2020-03-15 | West | Sorvino | 167.44 |
3 | 2020-04-01 | East | Jones | 299.40 |
4 | 2020-05-05 | Central | Jardine | 449.10 |
5 | 2020-06-08 | East | Jones | 539.40 |
6 | 2020-07-12 | East | Howard | 57.71 |
7 | 2020-08-15 | East | Jones | 174.65 |
8 | 2020-09-01 | Central | Smith | 250.00 |
9 | 2020-10-05 | Central | Morgan | 251.72 |
10 | 2020-11-08 | East | Mike | 299.85 |
11 | 2020-12-12 | Central | Smith | 86.43 |
Working with Multiple Spreadsheets
Excel files or workbooks usually contain more than one spreadsheet. The pandas library allows us to load data from a specific sheet or combine multiple spreadsheets into a single DataFrame. In this section, we’ll explore how to use these valuable capabilities.
By default, the read_excel()
method reads the first Excel sheet with the index 0
. However, we can choose the other sheets by assigning a particular sheet name, sheet index, or even a list of sheet names or indices to the sheet_name
argument. Let’s try it:
df = pd.read_excel('sales_data.xlsx', sheet_name='2021')
display(df)
OrderDate | Region | Rep | Item | Units | Unit Cost | Total | Shipped | |
---|---|---|---|---|---|---|---|---|
0 | 2021-01-15 | Central | Gill | Binder | 46 | 8.99 | 413.54 | True |
1 | 2021-02-01 | Central | Smith | Binder | 87 | 15.00 | 1305.00 | True |
2 | 2021-03-07 | West | Sorvino | Binder | 27 | 19.99 | 139.93 | True |
3 | 2021-04-10 | Central | Andrews | Pencil | 66 | 1.99 | 131.34 | False |
4 | 2021-05-14 | Central | Gill | Pencil | 53 | 1.29 | 68.37 | False |
5 | 2021-06-17 | Central | Tom | Desk | 15 | 125.00 | 625.00 | True |
6 | 2021-07-04 | East | Jones | Pen Set | 62 | 4.99 | 309.38 | True |
7 | 2021-08-07 | Central | Tom | Pen Set | 42 | 23.95 | 1005.90 | True |
8 | 2021-09-10 | Central | Gill | Pencil | 47 | 1.29 | 9.03 | True |
9 | 2021-10-14 | West | Thompson | Binder | 57 | 19.99 | 1139.43 | False |
10 | 2021-11-17 | Central | Jardine | Binder | 11 | 4.99 | 54.89 | False |
11 | 2021-12-04 | Central | Jardine | Binder | 94 | 19.99 | 1879.06 | False |
The code above reads the second spreadsheet in the workbook, whose name is 2021
. As mentioned before, we also can assign a sheet position number (zero-indexed) to the sheet_name
argument. Let’s see how it works:
df = pd.read_excel('sales_data.xlsx', sheet_name=1)
display(df)
OrderDate | Region | Rep | Item | Units | Unit Cost | Total | Shipped | |
---|---|---|---|---|---|---|---|---|
0 | 2021-01-15 | Central | Gill | Binder | 46 | 8.99 | 413.54 | True |
1 | 2021-02-01 | Central | Smith | Binder | 87 | 15.00 | 1305.00 | True |
2 | 2021-03-07 | West | Sorvino | Binder | 27 | 19.99 | 139.93 | True |
3 | 2021-04-10 | Central | Andrews | Pencil | 66 | 1.99 | 131.34 | False |
4 | 2021-05-14 | Central | Gill | Pencil | 53 | 1.29 | 68.37 | False |
5 | 2021-06-17 | Central | Tom | Desk | 15 | 125.00 | 625.00 | True |
6 | 2021-07-04 | East | Jones | Pen Set | 62 | 4.99 | 309.38 | True |
7 | 2021-08-07 | Central | Tom | Pen Set | 42 | 23.95 | 1005.90 | True |
8 | 2021-09-10 | Central | Gill | Pencil | 47 | 1.29 | 9.03 | True |
9 | 2021-10-14 | West | Thompson | Binder | 57 | 19.99 | 1139.43 | False |
10 | 2021-11-17 | Central | Jardine | Binder | 11 | 4.99 | 54.89 | False |
11 | 2021-12-04 | Central | Jardine | Binder | 94 | 19.99 | 1879.06 | False |
As you can see, both statements take in either the actual sheet name or sheet index to return the same result.
Sometimes, we want to import all the spreadsheets stored in an Excel file into pandas DataFrames simultaneously. The good news is that the read_excel()
method provides this feature for us. In order to do this, we can assign a list of sheet names or their indices to the sheet_name
argument. But there is a much easier way to do the same: to assign None
to the sheet_name
argument. Let’s try it:
all_sheets = pd.read_excel('sales_data.xlsx', sheet_name=None)
Before exploring the data stored in the all_sheets
variable, let’s check its data type:
type(all_sheets)
dict
As you can see, the variable is a dictionary. Now, let’s reveal what is stored in this dictionary:
for key, value in all_sheets.items():
print(key, type(value))
2020 <class 'pandas.core.frame.DataFrame'>
2021 <class 'pandas.core.frame.DataFrame'>
The code above shows that the dictionary’s keys are the Excel workbook sheet names, and its values are pandas DataFrames for each spreadsheet. To print out the content of the dictionary, we can use the following code:
for key, value in all_sheets.items():
print(key)
display(value)
2020
OrderDate | Region | Rep | Item | Units | Unit Cost | Total | Shipped | |
---|---|---|---|---|---|---|---|---|
0 | 2020-01-06 | East | Jones | Pencil | 95 | 1.99 | 189.05 | True |
1 | 2020-02-09 | Central | Jardine | Pencil | 36 | 4.99 | 179.64 | True |
2 | 2020-03-15 | West | Sorvino | Pencil | 56 | 2.99 | 167.44 | True |
3 | 2020-04-01 | East | Jones | Binder | 60 | 4.99 | 299.40 | False |
4 | 2020-05-05 | Central | Jardine | Pencil | 90 | 4.99 | 449.10 | True |
5 | 2020-06-08 | East | Jones | Binder | 60 | 8.99 | 539.40 | True |
6 | 2020-07-12 | East | Howard | Binder | 29 | 1.99 | 57.71 | False |
7 | 2020-08-15 | East | Jones | Pencil | 35 | 4.99 | 174.65 | True |
8 | 2020-09-01 | Central | Smith | Desk | 32 | 125.00 | 250.00 | True |
9 | 2020-10-05 | Central | Morgan | Binder | 28 | 8.99 | 251.72 | True |
10 | 2020-11-08 | East | Mike | Pen | 15 | 19.99 | 299.85 | False |
11 | 2020-12-12 | Central | Smith | Pencil | 67 | 1.29 | 86.43 | False |
2021
OrderDate | Region | Rep | Item | Units | Unit Cost | Total | Shipped | |
---|---|---|---|---|---|---|---|---|
0 | 2021-01-15 | Central | Gill | Binder | 46 | 8.99 | 413.54 | True |
1 | 2021-02-01 | Central | Smith | Binder | 87 | 15.00 | 1305.00 | True |
2 | 2021-03-07 | West | Sorvino | Binder | 27 | 19.99 | 139.93 | True |
3 | 2021-04-10 | Central | Andrews | Pencil | 66 | 1.99 | 131.34 | False |
4 | 2021-05-14 | Central | Gill | Pencil | 53 | 1.29 | 68.37 | False |
5 | 2021-06-17 | Central | Tom | Desk | 15 | 125.00 | 625.00 | True |
6 | 2021-07-04 | East | Jones | Pen Set | 62 | 4.99 | 309.38 | True |
7 | 2021-08-07 | Central | Tom | Pen Set | 42 | 23.95 | 1005.90 | True |
8 | 2021-09-10 | Central | Gill | Pencil | 47 | 1.29 | 9.03 | True |
9 | 2021-10-14 | West | Thompson | Binder | 57 | 19.99 | 1139.43 | False |
10 | 2021-11-17 | Central | Jardine | Binder | 11 | 4.99 | 54.89 | False |
11 | 2021-12-04 | Central | Jardine | Binder | 94 | 19.99 | 1879.06 | False |
Combining Multiple Excel Spreadsheets into a Single Pandas DataFrame
Having one DataFrame per sheet allows us to have different columns or content in different sheets.
But what if we prefer to store all the spreadsheets’ data in a single DataFrame? In this tutorial, the workbook spreadsheets have the same columns, so we can combine them with the concat()
method of pandas.
If you run the code below, you’ll see that the two DataFrames stored in the dictionary are concatenated:
combined_df = pd.concat(all_sheets.values(), ignore_index=True)
display(combined_df)
OrderDate | Region | Rep | Item | Units | Unit Cost | Total | Shipped | |
---|---|---|---|---|---|---|---|---|
0 | 2020-01-06 | East | Jones | Pencil | 95 | 1.99 | 189.05 | True |
1 | 2020-02-09 | Central | Jardine | Pencil | 36 | 4.99 | 179.64 | True |
2 | 2020-03-15 | West | Sorvino | Pencil | 56 | 2.99 | 167.44 | True |
3 | 2020-04-01 | East | Jones | Binder | 60 | 4.99 | 299.40 | False |
4 | 2020-05-05 | Central | Jardine | Pencil | 90 | 4.99 | 449.10 | True |
5 | 2020-06-08 | East | Jones | Binder | 60 | 8.99 | 539.40 | True |
6 | 2020-07-12 | East | Howard | Binder | 29 | 1.99 | 57.71 | False |
7 | 2020-08-15 | East | Jones | Pencil | 35 | 4.99 | 174.65 | True |
8 | 2020-09-01 | Central | Smith | Desk | 32 | 125.00 | 250.00 | True |
9 | 2020-10-05 | Central | Morgan | Binder | 28 | 8.99 | 251.72 | True |
10 | 2020-11-08 | East | Mike | Pen | 15 | 19.99 | 299.85 | False |
11 | 2020-12-12 | Central | Smith | Pencil | 67 | 1.29 | 86.43 | False |
12 | 2021-01-15 | Central | Gill | Binder | 46 | 8.99 | 413.54 | True |
13 | 2021-02-01 | Central | Smith | Binder | 87 | 15.00 | 1305.00 | True |
14 | 2021-03-07 | West | Sorvino | Binder | 27 | 19.99 | 139.93 | True |
15 | 2021-04-10 | Central | Andrews | Pencil | 66 | 1.99 | 131.34 | False |
16 | 2021-05-14 | Central | Gill | Pencil | 53 | 1.29 | 68.37 | False |
17 | 2021-06-17 | Central | Tom | Desk | 15 | 125.00 | 625.00 | True |
18 | 2021-07-04 | East | Jones | Pen Set | 62 | 4.99 | 309.38 | True |
19 | 2021-08-07 | Central | Tom | Pen Set | 42 | 23.95 | 1005.90 | True |
20 | 2021-09-10 | Central | Gill | Pencil | 47 | 1.29 | 9.03 | True |
21 | 2021-10-14 | West | Thompson | Binder | 57 | 19.99 | 1139.43 | False |
22 | 2021-11-17 | Central | Jardine | Binder | 11 | 4.99 | 54.89 | False |
23 | 2021-12-04 | Central | Jardine | Binder | 94 | 19.99 | 1879.06 | False |
Now the data stored in the combined_df
DataFrame is ready for further processing or visualization. In the following piece of code, we’re going to create a simple bar chart that shows the total sales amount made by each representative. Let’s run it and see the output plot:
total_sales_amount = combined_df.groupby('Rep').Total.sum()
total_sales_amount.plot.bar(figsize=(10, 6))
Reading Excel Files Using xlrd
Although importing data into a pandas DataFrame is much more common, another helpful package for reading Excel files in Python is xlrd
. In this section, we’re going to scratch the surface of how to read Excel spreadsheets using this package.
NOTE
The xlrd package doesn’t support xlsx files due to a potential security vulnerability. So, we use the xls
version of the sales data. You can download the xls
version from the link below:
Sales Data Excel Workbook — xls ver.
Let’s see how it works:
import xlrd
excel_workbook = xlrd.open_workbook('sales_data.xls')
Above, the first line imports the xlrd
package, then the open_workbook
method reads the sales_data.xls
file.
We can also open an individual sheet containing the actual data. There are two ways to do so: opening a sheet by index or by name. Let’s open the first sheet by index and the second one by name:
excel_worksheet_2020 = excel_workbook.sheet_by_index(0)
excel_worksheet_2021 = excel_workbook.sheet_by_name('2021')
Now, let’s see how we can print a cell value. The xlrd
package provides a method called cell_value()
that takes in two arguments: the cell’s row index and column index. Let’s explore it:
print(excel_worksheet_2020.cell_value(1, 3))
Pencil
We can see that the cell_value
function returned the value of the cell at row index 1 (the 2nd row) and column index 3 (the 4th column).
The xlrd
package provides two helpful properties: nrows
and ncols
, returning the number of nonempty spreadsheet’s rows and columns respectively:
print('Columns#:', excel_worksheet_2020.ncols)
print('Rows#:', excel_worksheet_2020.nrows)
Columns#: 8
Rows#: 13
Knowing the number of nonempty rows and columns in a spreadsheet helps us with iterating over the data using nested for
loops. This makes all the Excel sheet data accessible via the cell_value()
method.
Conclusion
This tutorial discussed how to load Excel spreadsheets into pandas DataFrames, work with multiple Excel sheets, and combine them into a single pandas DataFrame. We also explored the main aspects of the xlrd
package as one of the simplest tools for accessing the Excel spreadsheets data.
In this brief Python tutorial, we will learn how to read Excel (xlsx) files using Python. Specifically, we will read xlsx files in Python using the Python module openpyxl. First, we start by the simplest example of reading a xlsx file in Python. Second, we will learn how to read multiple Excel files using Python.
In previous posts, we have learned how to use Pandas read_excel method to import xlsx files with Python. As previously mentioned, however, we will use another package called openpyxl in this post. In the next paragraph, we will learn how to install openpyxl.
Openpyxl Syntax
Basically, here’s the simplest form of using openpyxl for reading a xlsx file in Python:
import openpyxl
from pathlib import Path
xlsx_file = Path('SimData', 'play_data.xlsx')
wb_obj = openpyxl.load_workbook(xlsx_file)
# Read the active sheet:
sheet = wb_obj.active
Code language: Python (python)
It is, of course, also possible to learn how to read, write, and append to files in Python (e.g., text files). Make sure to check that post out, as well.
Prerequisites: Python and Openpyxl
Now, before we will learn what Openpyxl is, we need to make sure that we have both Python 3 and the module openpyxl installed. One easy way to install Python is to download a Python distribution such as Anaconda or ActivePython. Openpyxl, on the other hand, can, as with many Python packages, be installed using both pip and conda. Now, using pip we type the following in a command prompt or terminal window, pip install openpyxl
and using conda we type this; conda install openpyxl
. Note, sometimes when we are installing Python packages with pip, we may notice that we don’t have the latest version of pip. Luckily, it is quite easy to upgrade pip to the latest version using pip.
What is the use of Openpyxl in Python?
Openpyxl is a Python module that can read and write Excel (with extension xlsx/xlsm/xltx/xltm) files. Furthermore, this module enables a Python script to modify Excel files. For instance, if we want to go through thousands of rows but just read specific data points and make small changes to these points, we can do this based on some criteria with openpyxl.
How do I read an Excel (xlsx) File in Python?
Now, the general method for reading xlsx files in Python (with openpyxl) is to import openpyxl (import openpyxl) and then read the workbook: wb = openpyxl.load_workbook(PATH_TO_EXCEL_FILE)
. In this post, we will learn more about this, of course.
How to Read an Excel (xlsx) File in Python
Now, in this section, we will be reading an xlsx file in Python using openpyxl. In a previous section, we have already been familiarized with the general template (syntax) for reading an Excel file using openpyxl and we will now get into this module in more detail. Note we will also work with the Path method from the Pathlib module.
1. Import the Needed Modules
In the first step, to reading an xlsx file in Python, we need to import the modules we need. That is, we will import Path and openpyxl:
import openpyxl
from pathlib import Path
Code language: Python (python)
2. Setting the Path to the Excel (xlsx) File
In the second step, we will create a variable using Path. Furthermore, this variable will point at the location and filename of the Excel file we want to import with Python:
# Setting the path to the xlsx file:
xlsx_file = Path('SimData', 'play_data.xlsx')</code></pre>
Code language: Python (python)
Note, “SimData” is a subdirectory to that of the Python script (or notebook). That is, if we were to store the Excel file in a completely different directory, we need to put in the full path. For example, xlsx_file = Path(Path.home(), 'Documents', 'SimData', 'play_data.xlsx')
if the data is stored in the Documents in our home directory.
3. Read the Excel File (Workbook)
In the third step, we are going to use Python to read the xlsx file. Now, we are using the load_workbook() method:
wb_obj = openpyxl.load_workbook(xlsx_file)
Code language: Python (python)
4. Read the Active Sheet from the Excel file
Now, in the fourth step, we are going to read the active sheet using the active method:
sheet = wb_obj.active
Code language: Python (python)
Note, if we know the sheet name we can also use this to read the sheet we want: play_data = wb_obj['play_data']
5. Work or Manipulate the Excel Sheet
In the final and fifth steps, we can work or manipulate the Excel sheet we have imported with Python. For example, if we want to get the value from a specific cell, we can do as follows:
print(sheet["C2"].value)
Code language: Python (python)
Another example of what we can do with the spreadsheet in Python is we can iterate through the rows and print them:
for row in sheet.iter_rows(max_row=6):
for cell in row:
print(cell.value, end=" ")
print()
Code language: Python (python)
Note that we used the max_row and set it to 6 to print the six first rows from the Excel file.
6. Bonus: Determining the Number of Rows and Columns in the Excel File
In the sixth, and bonus step, we are going to find out how many rows and columns we have in the example Excel file we have imported with Python:
print(sheet.max_row, sheet.max_column)
Code language: Python (python)
Reading an Excel (xlsx) File to a Python Dictionary
Now, before we learn how to read multiple xlsx files, we will import data from Excel into a Python dictionary. It’s quite simple, but for the example below, we need to know the column names before we start. If we want to find out the column names, we can run the following code (or open the Excel file):
import openpyxl
from pathlib import Path
xlsx_file = Path('SimData', 'play_data.xlsx')
wb_obj = openpyxl.load_workbook(xlsx_file)
sheet = wb_obj.active
col_names = []
for column in sheet.iter_cols(1, sheet.max_column):
col_names.append(column[0].value)
print(col_names)
Code language: Python (python)
Creating a Dictionary from an Excel File
In this section, we will finally read the Excel file using Python and create a dictionary.
data = {}
for i, row in enumerate(sheet.iter_rows(values_only=True)):
if i == 0:
data[row[1]] = []
data[row[2]] = []
data[row[3]] = []
data[row[4]] = []
data[row[5]] = []
data[row[6]] = []
else:
data['Subject ID'].append(row[1])
data['First Name'].append(row[2])
data['Day'].append(row[3])
data['Age'].append(row[4])
data['RT'].append(row[5])
data['Gender'].append(row[6])
Code language: Python (python)
Now, let’s walk through the code example above. First, we create a Python dictionary (data). Second, we loop through each row (using iter_rows), and we only go through the rows where there are values. Second, we have an if statement where we check if it’s the first row and add the keys to the dictionary. That is, we set the column names as keys. Third, we append the data to each key (column name) in the other statement.
How to Read Multiple Excel (xlsx) Files in Python
In this section, we will learn how to read multiple xlsx files in Python using openpyxl. Additionally to openpyxl and Path, we are also going to work with the os module.
1. Import the Modules
In the first step, we are going to import the modules Path, glob, and openpyxl:
import glob
import openpyxl
from pathlib import Path
Code language: Python (python)
2. Read all xlsx Files in the Directory to a List
Second, we will read all the .xlsx files in a subdirectory into a list. Now, we use the glob module together with Path:
xlsx_files = [path for path in Path('XLSX_FILES').rglob('*.xlsx')]
Code language: Python (python)
3. Create Workbook Objects (i.e., read the xlsx files)
Third, we can now read all the xlsx files using Python. Again, we will use the load_workbook method. However, this time we will loop through each file we found in the subdirectory,
wbs = [openpyxl.load_workbook(wb) for wb in xlsx_files]
Code language: Python (python)
Now, in the code examples above, we are using Python list comprehension (twice, in both step 2 and 3). First, we create a list of all the xlsx files in the “XLSX_FILES” directory. Second, we loop through this list and create a list of workbooks. Of course, we could add this to the first line of code above.
4. Work with the Imported Excel Files
In the fourth step, we can now work with the imported excel files. For example, we can get the first file by adding “[0]” to the list. If we want to know the sheet names of this file we do like this:wbs[0].sheetnames
. Many of the things we can do, and have done in the previous example on reading xlsx files in Python, can be done when we’ve read multiple Excel files.
Notice this is one great example of how to use this programming language. Other examples are, for instance, to use it for renaming files in Python.
Conclusion: Reading Excel (xlsx) Files in Python
In conclusion, Openpyxl is a powerful Python library that enables developers to read, write, and manipulate Excel files using Python. This tutorial covered how to read an Excel (xlsx) file in Python using Openpyxl. We started by importing the necessary modules, setting the path to the Excel file, and then reading the file and its active sheet. We then explored how to work with the sheet and even determine the number of rows and columns in the file.
Moreover, we also learned how to read an Excel file to a Python dictionary and create a dictionary from an Excel file. Finally, we learned how to read multiple Excel files in a directory by importing the required modules, reading all the xlsx files in the directory to a list, creating workbook objects, and working with the imported Excel files.
By mastering these techniques, you can easily read and manipulate Excel files using Python, which can be incredibly useful in various data processing applications. So go ahead, try it out, and unlock the full potential of Openpyxl in your Python projects!
It is, of course, possible to import data from various other file formats. For instance, read the post about parsing json files in Python to learn more about reading JSON files.
What is the best way to read Excel (XLS) files with Python (not CSV files).
Is there a built-in package which is supported by default in Python to do this task?
asked May 31, 2010 at 10:28
1
I highly recommend xlrd for reading .xls
files. But there are some limitations(refer to xlrd github page):
Warning
This library will no longer read anything other than .xls files. For
alternatives that read newer file formats, please see
http://www.python-excel.org/.The following are also not supported but will safely and reliably be
ignored:- Charts, Macros, Pictures, any other embedded object, including embedded worksheets. - VBA modules - Formulas, but results of formula calculations are extracted. - Comments - Hyperlinks - Autofilters, advanced filters, pivot tables, conditional formatting, data validation
Password-protected files are not supported and cannot be read by this
library.
voyager mentioned the use of COM automation. Having done this myself a few years ago, be warned that doing this is a real PITA. The number of caveats is huge and the documentation is lacking and annoying. I ran into many weird bugs and gotchas, some of which took many hours to figure out.
UPDATE:
For newer .xlsx
files, the recommended library for reading and writing appears to be openpyxl (thanks, Ikar Pohorský).
Saikat
13.4k20 gold badges104 silver badges121 bronze badges
answered May 31, 2010 at 12:24
taleinattaleinat
8,3811 gold badge30 silver badges44 bronze badges
2
You can use pandas to do this, first install the required libraries:
$ pip install pandas openpyxl
See code below:
import pandas as pd
xls = pd.ExcelFile(r"yourfilename.xls") # use r before absolute file path
sheetX = xls.parse(2) #2 is the sheet number+1 thus if the file has only 1 sheet write 0 in paranthesis
var1 = sheetX['ColumnName']
print(var1[1]) #1 is the row number...
Skully
2,2723 gold badges22 silver badges31 bronze badges
answered May 23, 2017 at 4:04
3
You can choose any one of them http://www.python-excel.org/
I would recommended python xlrd library.
install it using
pip install xlrd
import using
import xlrd
to open a workbook
workbook = xlrd.open_workbook('your_file_name.xlsx')
open sheet by name
worksheet = workbook.sheet_by_name('Name of the Sheet')
open sheet by index
worksheet = workbook.sheet_by_index(0)
read cell value
worksheet.cell(0, 0).value
answered Apr 6, 2017 at 14:15
SomilSomil
1,9031 gold badge20 silver badges35 bronze badges
1
I think Pandas is the best way to go. There is already one answer here with Pandas using ExcelFile
function, but it did not work properly for me. From here I found the read_excel
function which works just fine:
import pandas as pd
dfs = pd.read_excel("your_file_name.xlsx", sheet_name="your_sheet_name")
print(dfs.head(10))
P.S. You need to have the xlrd
installed for read_excel
function to work
Update 21-03-2020: As you may see here, there are issues with the xlrd
engine and it is going to be deprecated. The openpyxl
is the best replacement. So as described here, the canonical syntax should be:
dfs = pd.read_excel("your_file_name.xlsx", sheet_name="your_sheet_name", engine="openpyxl")
Update 03-03-2023: There are now several other options available. For example the Polars library that is written in Rust:
import polars as pl
dfs = pl.read_excel("your_file_name.xlsx", sheet_name="your_sheet_name")
Feel free to also check the PyArrow and pyodbc libraries.
answered Jun 12, 2018 at 10:35
Foad S. FarimaniFoad S. Farimani
12k15 gold badges72 silver badges181 bronze badges
3
For xlsx I like the solution posted earlier as https://web.archive.org/web/20180216070531/https://stackoverflow.com/questions/4371163/reading-xlsx-files-using-python. I uses modules from the standard library only.
def xlsx(fname):
import zipfile
from xml.etree.ElementTree import iterparse
z = zipfile.ZipFile(fname)
strings = [el.text for e, el in iterparse(z.open('xl/sharedStrings.xml')) if el.tag.endswith('}t')]
rows = []
row = {}
value = ''
for e, el in iterparse(z.open('xl/worksheets/sheet1.xml')):
if el.tag.endswith('}v'): # Example: <v>84</v>
value = el.text
if el.tag.endswith('}c'): # Example: <c r="A3" t="s"><v>84</v></c>
if el.attrib.get('t') == 's':
value = strings[int(value)]
letter = el.attrib['r'] # Example: AZ22
while letter[-1].isdigit():
letter = letter[:-1]
row[letter] = value
value = ''
if el.tag.endswith('}row'):
rows.append(row)
row = {}
return rows
Improvements added are fetching content by sheet name, using re to get the column and checking if sharedstrings are used.
def xlsx(fname,sheet):
import zipfile
from xml.etree.ElementTree import iterparse
import re
z = zipfile.ZipFile(fname)
if 'xl/sharedStrings.xml' in z.namelist():
# Get shared strings
strings = [element.text for event, element
in iterparse(z.open('xl/sharedStrings.xml'))
if element.tag.endswith('}t')]
sheetdict = { element.attrib['name']:element.attrib['sheetId'] for event,element in iterparse(z.open('xl/workbook.xml'))
if element.tag.endswith('}sheet') }
rows = []
row = {}
value = ''
if sheet in sheets:
sheetfile = 'xl/worksheets/sheet'+sheets[sheet]+'.xml'
#print(sheet,sheetfile)
for event, element in iterparse(z.open(sheetfile)):
# get value or index to shared strings
if element.tag.endswith('}v') or element.tag.endswith('}t'):
value = element.text
# If value is a shared string, use value as an index
if element.tag.endswith('}c'):
if element.attrib.get('t') == 's':
value = strings[int(value)]
# split the row/col information so that the row leter(s) can be separate
letter = re.sub('d','',element.attrib['r'])
row[letter] = value
value = ''
if element.tag.endswith('}row'):
rows.append(row)
row = {}
return rows
answered Oct 28, 2018 at 11:53
2
If you need old XLS format. Below code for ansii ‘cp1251’.
import xlrd
file=u'C:/Landau/task/6200.xlsx'
try:
book = xlrd.open_workbook(file,encoding_override="cp1251")
except:
book = xlrd.open_workbook(file)
print("The number of worksheets is {0}".format(book.nsheets))
print("Worksheet name(s): {0}".format(book.sheet_names()))
sh = book.sheet_by_index(0)
print("{0} {1} {2}".format(sh.name, sh.nrows, sh.ncols))
print("Cell D30 is {0}".format(sh.cell_value(rowx=29, colx=3)))
for rx in range(sh.nrows):
print(sh.row(rx))
answered Nov 17, 2019 at 5:15
1
For older .xls
files, you can use xlrd
either you can use xlrd
directly by importing it. Like below
import xlrd
wb = xlrd.open_workbook(file_name)
Or you can also use pandas pd.read_excel()
method, but do not forget to specify the engine, though the default is xlrd
, it has to be specified.
pd.read_excel(file_name, engine = xlrd)
Both of them work for older .xls
file formats.
Infact I came across this when I used OpenPyXL
, i got the below error
InvalidFileException: openpyxl does not support the old .xls file format, please use xlrd to read this file, or convert it to the more recent .xlsx file format.
answered Aug 12, 2020 at 6:06
2
You can use any of the libraries listed here (like Pyxlreader that is based on JExcelApi, or xlwt), plus COM automation to use Excel itself for the reading of the files, but for that you are introducing Office as a dependency of your software, which might not be always an option.
answered May 31, 2010 at 10:46
Esteban KüberEsteban Küber
36.1k15 gold badges83 silver badges97 bronze badges
1
You might also consider running the (non-python) program xls2csv. Feed it an xls file, and you should get back a csv.
answered Nov 25, 2012 at 21:43
2
with open(csv_filename) as file:
data = file.read()
with open(xl_file_name, 'w') as file:
file.write(data)
You can turn CSV to excel like above with inbuilt packages. CSV can be handled with an inbuilt package of dictreader and dictwriter which will work the same way as python dictionary works. which makes it a ton easy
I am currently unaware of any inbuilt packages for excel but I had come across openpyxl. It was also pretty straight forward and simple You can see the code snippet below hope this helps
import openpyxl
book = openpyxl.load_workbook(filename)
sheet = book.active
result =sheet['AP2']
print(result.value)
answered Jun 19, 2020 at 12:26
For older Excel files there is the OleFileIO_PL module that can read the OLE structured storage format used.
answered Sep 18, 2013 at 20:35
Gavin SmithGavin Smith
3,0661 gold badge18 silver badges24 bronze badges
If the file is really an old .xls, this works for me on python3 just using base open() and pandas:
df = pandas.read_csv(open(f, encoding = 'UTF-8'), sep='t')
Note that the file I’m using is tab delimited. less or a text editor should be able to read .xls so that you can sniff out the delimiter.
I did not have a lot of luck with xlrd because of – I think – UTF-8 issues.
answered Dec 14, 2020 at 21:16
1