There are many common file types that you will need to work with as a software developer. One such format is the CSV file. CSV stands for “Comma-Separated Values” and is a text file format that uses a comma as a delimiter to separate values from one another. Each row is its own record and each value is its own field. Most CSV files have records that are all the same length.
Microsoft Excel opens CSV files with no problem. You can open one yourself with Excel and then save it yourself in an Excel format. The purpose of this article is to teach you the following concepts:
- Converting a CSV file to Excel
- Converting an Excel spreadsheet to CSV
You will be using Python and OpenPyXL to do the conversion from one file type to the other.
You need to install OpenPyXL to be able to use the examples in this article. You can use pip to install OpenPyXL:
python3 -m pip install openpyxl
Now that you have OpenPyXL, you are ready to learn how to convert a CSV file to an Excel spreadsheet!
Converting a CSV file to Excel
You will soon see that converting a CSV file to an Excel spreadsheet doesn’t take very much code. However, you do need to have a CSV file to get started. With that in mind, open up your favorite text editor (Notepad, SublimeText, or something else) and add the following:
book_title,author,publisher,pub_date,isbn Python 101,Mike Driscoll, Mike Driscoll,2020,123456789 wxPython Recipes,Mike Driscoll,Apress,2018,978-1-4842-3237-8 Python Interviews,Mike Driscoll,Packt Publishing,2018,9781788399081
Save this file as books.txt. You can also download the CSV file from this book’s GitHub code repository.
Now that you have the CSV file, you need to create a new Python file too. Open up your Python IDE and create a new file named
csv_to_excel.py. Then enter the following code:
# csv_to_excel.py import csv import openpyxl def csv_to_excel(csv_file, excel_file): csv_data =  with open(csv_file) as file_obj: reader = csv.reader(file_obj) for row in reader: csv_data.append(row) workbook = openpyxl.Workbook() sheet = workbook.active for row in csv_data: sheet.append(row) workbook.save(excel_file) if __name__ == "__main__": csv_to_excel("books.csv", "books.xlsx")
Your code uses Python’s
csv module in addition to OpenPyXL. You create a function,
csv_to_excel(), then accepts two arguments:
csv_file– The path to the input CSV file
excel_file– The path to the Excel file that you want to create
You want to extract each row of data from the CSV. To extract the data, you create an
csv.reader() object and then iterate over one row at a time. For each iteration, you append the row to
row is a list of strings.
The next step of the process is to create the Excel spreadsheet. To add data to your
Workbook, you iterate over each row in
append() them to your Worksheet. Finally, you save the Excel spreadsheet.
When you run this code, you will have an Excel spreadsheet that looks like this:
You are now able to convert a CSV file to an Excel spreadsheet in less than twenty-five lines of code!
Now you are ready to learn how to convert an Excel spreadsheet to a CSV file!
Converting an Excel Spreadsheet to CSV
Converting an Excel spreadsheet to a CSV file can be useful if you need other processes to consume the data. Another potential need for a CSV file is when you need to share your Excel spreadsheet with someone who doesn’t have a spreadsheet program to open it. While rare, this may happen.
You can convert an Excel spreadsheet to a CSV file using Python. Create a new file named
excel_to_csv.py and add the following code:
# excel_to_csv.py import csv import openpyxl from openpyxl import load_workbook def excel_to_csv(excel_file, csv_file): workbook = load_workbook(filename=excel_file) sheet = workbook.active csv_data =  # Read data from Excel for value in sheet.iter_rows(values_only=True): csv_data.append(list(value)) # Write to CSV with open(csv_file, 'w') as csv_file_obj: writer = csv.writer(csv_file_obj, delimiter=',') for line in csv_data: writer.writerow(line) if __name__ == "__main__": excel_to_csv("books.xlsx", "new_books.csv")
Once again you only need the
openpyxl modules to do the conversion. This time, you load the Excel spreadsheet first and iterate over the Worksheet using the
iter_rows method. The
value you receive in each iteration of
iter_tools is a list of strings. You append the list of strings to
The next step is to create a
csv.writer(). Then you iterate over each list of strings in
csv_data and call
writerow() to add it to your CSV file.
Once your code finishes, you will have a brand new CSV file!
Converting a CSV file to an Excel spreadsheet is easy to do with Python. It’s a useful tool that you can use to take in data from your clients or other data sources and transform it into something that you can present to your company.
You can apply cell styling to the data as you write it to your Worksheet too. By applying cell styling, you can make your data stand out with different fonts or background row colors.
Try this code out on your own Excel or CSV files and see what you can do.
Would you like to learn more about processing Excel spreadsheets with Python? Then check out these tutorials: