Convert List to XLSX Excel File & Vice Versa in Python (Examples)

 

Hi! This tutorial will show you how to turn a list into an xlsx file and vice-versa in the Python programming language.

First, though, here is an overview of this tutorial:

Let’s jump right into the Python code!

 

Create Sample List

Here, we will create the sample Python list that we will turn into an xlsx file. So, in your Python IDE, run the code below to create a sample list of car brands:

cars = ["Toyota", "Mercedes", "Ferrari", "Chevrolet", "GMC"]
 
print(type(cars))
 
# <class 'list'>

As seen, the sample list, cars, contains 6 strings. Let’s continue next with importing the relevant libraries!

 

Install Relevant Libraries

We will need to install 3 Python libraries that will enable us to write our sample car brands list to xlsx. Therefore, run the line of code below to install the libraries.

pip install pandas openpyxl xlsxwriter

Now it is time to import the libraries installed. See the following.

import pandas as pd
from openpyxl import Workbook
import xlsxwriter

We are ready to go! Let’s see some examples of conversion!

 

Example 1: List to XLSX | Turn List to XLSX File Using pandas

In this first example, we will use the pandas library to turn the list to an xlsx file:

df = pd.DataFrame({"Car Brands": cars})
df.to_excel("cars.xlsx", index=False)

In the code above, we first created a pandas DataFrame of car brands and parsed the list as a column, and then we wrote it to xlsx with the to_excel() method. In the method, we also parsed the boolean “False” to the index = argument so that the row index is not included in the output file.

Now, if you check inside your current working directory, you should see the “car.xlsx” file. Let’s now continue with the next example!

 

Example 2: List to XLSX | Turn List to XLSX File Using openpyxl

In this second example, we will transform the list into an xlsx file, using openpyxl:

wb = Workbook()
ws = wb.active
for item in cars:
    ws.append([item])
wb.save("cars2.xlsx")

The above code created a new excel workbook (wb) and worksheet (ws), then iteratively wrote the items in the car list in a new row in the worksheet via append(), and then saved the file as “cars2.xlsx”. Again, the file is saved inside your current working directory; check it!

 

Example 3: List to XLSX | Turn List to XLSX File Using xlsxwriter

In this third example, we will use the xlsxwriter package to convert the list into an xlsx file:

workbook = xlsxwriter.Workbook("cars3.xlsx")
worksheet = workbook.add_worksheet()
for row_num, item in enumerate(cars):
    worksheet.write(row_num, 0, item)
workbook.close()

The code will create a new excel workbook, add a worksheet, loop through the list and write each item in a new row in the worksheet, and save the workbook to a file named “cars3.xlsx” in your current working directory.

Now that we have demonstrated how to turn a list into an xlsx file, we will look at an example of how to turn the xlsx file back into a list.
 

Example 1: Turn XLSX File to List Using pandas tolist() Method

In this example, we will use pandas’ tolist() method to turn the xlsx file back into a list:

df = pd.read_excel("cars.xlsx")
cars_list = df["Car Brands"].tolist()
print(cars_list)
 
# ['Toyota', 'Mercedes', 'Ferrari', 'Chevrolet', 'GMC']
 
 
print(type(cars_list))
 
# <class 'list'>

In the code, we first of all read in the xlsx file with pd.read_excel(), then we selected the column of interest in the DataFrame, and then used the tolist() function to convert the row values into a list.

With that, we have demonstrated how to convert a list to an xlsx file and vice-versa in Python. I hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to convert a list to an xlsx file and vice-versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a list to an xlsx file and vice-versa in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to convert a list to an xlsx file and vice-versa in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top