MS Excel is a powerful tool for handling huge amounts of tabular data. It can be particularly useful for sorting, analyzing, performing complex calculations and visualizing data. In this article, we will discuss how to extract a table from a webpage and store it in Excel format.
Step #1: Converting to Pandas dataframe
Pandas is a Python library used for managing tables. Our first step would be to store the table from the webpage into a Pandas dataframe. The function
read_html() returns a list of dataframes, each element representing a table in the webpage. Here we are assuming that the webpage contains a single table.
0 1 2 3 4 0 ROLL_NO NAME ADDRESS PHONE AGE 1 1 RAM DELHI 9455123451 18 2 2 RAMESH GURGAON 9652431543 18 3 3 SUJIT ROHTAK 9156253131 20 4 4 SURESH DELHI 9156768971 18
Step #2: Storing the Pandas dataframe in an excel file
For this, we use the to_excel() function of Pandas, passing the filename as a parameter.
In case of multiple tables on the webpage, we can change the index number from 0 to that of the required table.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Create a GUI to convert CSV file into excel file using Python
- How to Create a Pivot table with multiple indexes from an excel sheet using Pandas in Python?
- Convert HTML table into CSV file in python
- How to import an excel file into Python using Pandas?
- Convert CSV to Excel using Pandas in Python
- Convert Excel to CSV in Python
- Combine Multiple Excel Worksheets Into a Single Pandas Dataframe
- Web Scraping Coronavirus Data into MS Excel
- Convert CSV to HTML Table in Python
- Python program to convert Set into Tuple and Tuple into Set
- Display the Pandas DataFrame in table style and border around the table and not around the rows
- Python MySQL - Insert into Table
- PostgreSQL - Insert Data Into a Table using Python
- Python MariaDB - Insert into Table using PyMySQL
- Python | Writing to an excel file using openpyxl module
- Writing to an excel sheet using Python
- Python | Arithmetic operations in excel file using openpyxl
- Python | Adjusting rows and columns of an excel file using openpyxl module
- Python | Plotting charts in excel sheet using openpyxl module | Set - 1
- Python | Excel File Comparison
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.