Python csv for excel

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.

Getting Started

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!

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 csv_data. A 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 csv_data and 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:

CSV to Excel Spreadsheet

CSV to Excel Spreadsheet

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 csv and 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 csv_data.

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!

Wrapping Up

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.

Related Reading

Would you like to learn more about processing Excel spreadsheets with Python? Then check out these tutorials:

  • OpenPyXL – Working with Microsoft Excel Using Python

  • Styling Excel Cells with OpenPyXL and Python
  • Reading Excel Spreadsheets with Python and xlrd

In this post there is a Python example to convert from csv to xls.

However, my file has more than 65536 rows so xls does not work. If I name the file xlsx it doesnt make a difference. Is there a Python package to convert to xlsx?

Community's user avatar

asked Jul 15, 2013 at 10:21

user670186's user avatar

0

Here’s an example using xlsxwriter:

import os
import glob
import csv
from xlsxwriter.workbook import Workbook


for csvfile in glob.glob(os.path.join('.', '*.csv')):
    workbook = Workbook(csvfile[:-4] + '.xlsx')
    worksheet = workbook.add_worksheet()
    with open(csvfile, 'rt', encoding='utf8') as f:
        reader = csv.reader(f)
        for r, row in enumerate(reader):
            for c, col in enumerate(row):
                worksheet.write(r, c, col)
    workbook.close()

FYI, there is also a package called openpyxl, that can read/write Excel 2007 xlsx/xlsm files.

user's user avatar

user

10.9k6 gold badges23 silver badges83 bronze badges

answered Jul 16, 2013 at 18:51

alecxe's user avatar

alecxealecxe

458k119 gold badges1069 silver badges1182 bronze badges

14

With my library pyexcel,

 $ pip install pyexcel pyexcel-xlsx

you can do it in one command line:

from pyexcel.cookbook import merge_all_to_a_book
# import pyexcel.ext.xlsx # no longer required if you use pyexcel >= 0.2.2 
import glob


merge_all_to_a_book(glob.glob("your_csv_directory/*.csv"), "output.xlsx")

Each csv will have its own sheet and the name will be their file name.

answered Oct 19, 2014 at 23:42

chfw's user avatar

chfwchfw

4,4822 gold badges27 silver badges31 bronze badges

9

Simple two line code solution using pandas

  import pandas as pd

  read_file = pd.read_csv ('File name.csv')
  read_file.to_excel ('File name.xlsx', index = None, header=True)

answered Nov 16, 2019 at 23:11

Bhanu Sinha's user avatar

Bhanu SinhaBhanu Sinha

1,51612 silver badges10 bronze badges

3

First install openpyxl:

pip install openpyxl

Then:

from openpyxl import Workbook
import csv


wb = Workbook()
ws = wb.active
with open('test.csv', 'r') as f:
    for row in csv.reader(f):
        ws.append(row)
wb.save('name.xlsx')

Paolo's user avatar

Paolo

19.5k21 gold badges75 silver badges113 bronze badges

answered Mar 9, 2017 at 19:07

zhuhuren's user avatar

zhuhurenzhuhuren

3274 silver badges7 bronze badges

1

Adding an answer that exclusively uses the pandas library to read in a .csv file and save as a .xlsx file. This example makes use of pandas.read_csv (Link to docs) and pandas.dataframe.to_excel (Link to docs).

The fully reproducible example uses numpy to generate random numbers only, and this can be removed if you would like to use your own .csv file.

import pandas as pd
import numpy as np

# Creating a dataframe and saving as test.csv in current directory
df = pd.DataFrame(np.random.randn(100000, 3), columns=list('ABC'))
df.to_csv('test.csv', index = False)

# Reading in test.csv and saving as test.xlsx

df_new = pd.read_csv('test.csv')
writer = pd.ExcelWriter('test.xlsx')
df_new.to_excel(writer, index = False)
writer.save()

answered Dec 29, 2017 at 17:19

patrickjlong1's user avatar

patrickjlong1patrickjlong1

3,6431 gold badge18 silver badges32 bronze badges

2

Simple 1-to-1 CSV to XLSX file conversion without enumerating/looping through the rows:

import pyexcel

sheet = pyexcel.get_sheet(file_name="myFile.csv", delimiter=",")
sheet.save_as("myFile.xlsx")

Notes:

  1. I have found that if the file_name is really long (>30 characters excluding path)
    then the resultant XLSX file will throw an error when Excel tries
    to load it. Excel will offer to fix the error which it does, but it
    is frustrating.
  2. There is a great answer previously provided that
    combines all of the CSV files in a directory into one XLSX workbook,
    which fits a different use case than just trying to do a 1-to-1 CSV file to
    XLSX file conversion.

answered Apr 8, 2020 at 20:16

Larry W's user avatar

Larry WLarry W

1011 silver badge5 bronze badges

2

How I do it with openpyxl lib:

import csv
from openpyxl import Workbook

def convert_csv_to_xlsx(self):
    wb = Workbook()
    sheet = wb.active

    CSV_SEPARATOR = "#"

    with open("my_file.csv") as f:
        reader = csv.reader(f)
        for r, row in enumerate(reader):
            for c, col in enumerate(row):
                for idx, val in enumerate(col.split(CSV_SEPARATOR)):
                    cell = sheet.cell(row=r+1, column=idx+1)
                    cell.value = val

    wb.save("my_file.xlsx")

mcarton's user avatar

mcarton

26.8k5 gold badges82 silver badges92 bronze badges

answered Aug 17, 2016 at 16:58

Rubycon's user avatar

RubyconRubycon

18.1k10 gold badges49 silver badges70 bronze badges

There is a simple way

import os
import csv
import sys

from openpyxl import Workbook

reload(sys)
sys.setdefaultencoding('utf8')

if __name__ == '__main__':
    workbook = Workbook()
    worksheet = workbook.active
    with open('input.csv', 'r') as f:
        reader = csv.reader(f)
        for r, row in enumerate(reader):
            for c, col in enumerate(row):
                for idx, val in enumerate(col.split(',')):
                    cell = worksheet.cell(row=r+1, column=c+1)
                    cell.value = val
    workbook.save('output.xlsx')

answered May 5, 2017 at 2:23

David Ding's user avatar

David DingDavid Ding

1,4331 gold badge15 silver badges13 bronze badges

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    Like Article

    Pandas can read, filter, and re-arrange small and large datasets and output them in a range of formats including Excel. In this article, we will be dealing with the conversion of .csv file into excel (.xlsx). 
    Pandas provide the ExcelWriter class for writing data frame objects to excel sheets. 
    Syntax: 
     

    final = pd.ExcelWriter('GFG.xlsx')

    Example:
    Sample CSV File:
     

    python-csv-to-json

    Python3

    import pandas as pd

    df_new = pd.read_csv('Names.csv')

    GFG = pd.ExcelWriter('Names.xlsx')

    df_new.to_excel(GFG, index=False)

    GFG.save()

    Output:
     

    python-csv-to-excel

     Method 2:

    The read_* functions are used to read data to pandas, the to_* methods are used to store data. The to_excel() method stores the data as an excel file. In the example here, the sheet_name is named passengers instead of the default Sheet1. By setting index=False the row index labels are not saved in the spreadsheet.

    Python3

    import pandas as pd

    df = pd.read_csv("./weather_data.csv")

    df.to_excel("weather.xlsx", sheet_name="Testing", index=False)

    Like Article

    Save Article

    In this quick guide, you’ll see the complete steps to convert a CSV file to an Excel file using Python.

    To start, here is a simple template that you can use to convert a CSV to Excel using Python:

    import pandas as pd
    
    read_file = pd.read_csv (r'Path where the CSV file is storedFile name.csv')
    read_file.to_excel (r'Path to store the Excel fileFile name.xlsx', index = None, header=True)
    

    In the next section, you’ll see how to apply this template in practice.

    Step 1: Install the Pandas package

    If you haven’t already done so, install the Pandas package. You can use the following command to install the Pandas package (under Windows):

    pip install pandas
    

    Step 2: Capture the path where the CSV file is stored

    Next, capture the path where the CSV file is stored on your computer.

    Here is an example of a path where a CSV file is stored:

    C:UsersRonDesktopTestProduct_List.csv

    Where ‘Product_List‘ is the current CSV file name, and ‘csv‘ is the file extension.

    Step 3: Specify the path where the new Excel file will be stored

    Now, you’ll need to specify the path where the new Excel file will be stored. For example:

    C:UsersRonDesktopTestNew_Products.xlsx

    Where ‘New_Products‘ is the new file name, and ‘xlsx‘ is the Excel file extension.

    Step 4: Convert the CSV to Excel using Python

    For this final step, you’ll need to use the following template to perform the conversion:

    import pandas as pd
    
    read_file = pd.read_csv (r'Path where the CSV file is storedFile name.csv')
    read_file.to_excel (r'Path to store the Excel fileFile name.xlsx', index = None, header=True)
    

    Here is the complete syntax for our example (note that you’ll need to modify the paths to reflect the location where the files will be stored on your computer):

    import pandas as pd
    
    read_file = pd.read_csv (r'C:UsersRonDesktopTestProduct_List.csv')
    read_file.to_excel (r'C:UsersRonDesktopTestNew_Products.xlsx', index = None, header=True)
    

    Run the code in Python and the new Excel file (i.e., New_Products) will be saved at your specified location.

    Problem Formulation

    💡 Challenge: Given a CSV file. How to convert it to an excel file in Python?

    csv to excel in Python

    We create a folder with two files, the file csv_to_excel.py and my_file.csv. We want to convert the CSV file to an excel file so that after running the script csv_to_excel.py, we obtain the third file my_file.csv in our folder like so:

    All methods discussed in this tutorial show different code snippets to put into csv_to_excel.py so that it converts the CSV to XLSX in Python.

    Method 1: 5 Easy Steps in Pandas

    The most pythonic way to convert a .csv to an .xlsx (Excel) in Python is to use the Pandas library.

    1. Install the pandas library with pip install pandas
    2. Install the openpyxl library that is used internally by pandas with pip install openpyxl
    3. Import the pandas libray with import pandas as pd
    4. Read the CSV file into a DataFrame df by using the expression df = pd.read_csv('my_file.csv')
    5. Store the DataFrame in an Excel file by calling df.to_excel('my_file.xlsx', index=None, header=True)
    import pandas as pd
    
    
    df = pd.read_csv('my_file.csv')
    df.to_excel('my_file.xlsx', index=None, header=True)
    

    Note that there are many ways to customize the to_excel() function in case

    • you don’t need a header line,
    • you want to fix the first line in the Excel file,
    • you want to format the cells as numbers instead of strings, or
    • you have an index column in the original CSV and want to consider it in the Excel file too.

    If you want to do any of those, feel free to read our full guide on the Finxter blog here:

    🌍 Tutorial: Pandas DataFrame.to_excel() – An Unofficial Guide to Saving Data to Excel

    Also, we’ve recorded a video on the ins and outs of this method here:

    pd.to_excel() – An Unofficial Guide to Saving Data to Excel

    Let’s have a look at an alternative to converting a CSV to an Excel file in Python:

    Method 2: Modules csv and openpyxl

    To convert a CSV to an Excel file, you can also use the following approach:

    • Import the csv module
    • Import the openpyxl module
    • Read the CSV file into a list of lists, one inner list per row, by using the csv.reader() function
    • Write the list of lists to the Excel file by using the workbook representation of the openpyxl library.
    • Get the active worksheet by calling workbook.active
    • Write to the worksheet by calling worksheet.append(row) and append one list of values, one value per cell.

    The following function converts a given CSV to an Excel file:

    import csv
    import openpyxl
    
    
    def csv_to_excel(csv_filename, excel_filename):
    
        # Read CSV file
        csv_data = []
        with open(csv_filename) as f:
            csv_data = [row for row in csv.reader(f)]
        
        # Write to Excel file
        workbook = openpyxl.workbook.Workbook()
        worksheet = workbook.active
        for row in csv_data:
            worksheet.append(row)
        workbook.save(excel_filename)
    
    
    if __name__ == "__main__":
        csv_to_excel("my_file.csv", "my_file.xlsx")

    This is a bit more fine-granular approach and it allows you to modify each row in the code or even write additional details into the Excel worksheet.

    More Python CSV Conversions

    🐍 Learn More: I have compiled an “ultimate guide” on the Finxter blog that shows you the best method, respectively, to convert a CSV file to JSON, Excel, dictionary, Parquet, list, list of lists, list of tuples, text file, DataFrame, XML, NumPy array, and list of dictionaries.

    Where to Go From Here?

    Enough theory. Let’s get some practice!

    Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

    To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

    You build high-value coding skills by working on practical coding projects!

    Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

    🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

    If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

    Join the free webinar now!

    While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

    To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

    His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

    Like this post? Please share to your friends:
  • Python create table in word
  • Python open excel csv
  • Python create excel document
  • Python num to word
  • Python count word in text