Signup/Sign In

Converting Xlsx file To CSV file using Python

Posted in Programming   LAST UPDATED: MAY 26, 2023

    Converting Xlsx files to CSV format is a common task when working with data in Python. Whether you need to extract specific data for analysis or transform your data into a more portable format, understanding how to convert xlsx files to CSV files using Python is essential. In this article, we will explore different approaches and libraries to seamlessly perform this conversion, providing you with a comprehensive guide to tackle this task efficiently.

    In the previous article covering working with Xlsx file in Python, we covered various methods of the openpyxl module of Python to create, modify and analyze Xlsx files.

    By the end of this article, you will have a solid understanding of the different methods available for converting Xlsx files to CSV files in Python, empowering you to handle data conversions effortlessly. So let's dive in and unlock the world of Xlsx to CSV conversion using Python!

    How to Convert Xlsx file To CSV file using Python

    converting xlsx file to csv file in python

    You can install the module using the following command.

    pip install openpyxl

    Converting Xlsx file To CSV file

    Following are the steps to convert an Xlsx file to a CSV file:

    1. Get the data from the Xlsx file using the openpyxl module.

    2. Store every row of data in a CSV file with comma-separated values.

    3. Save the file with a .csv extension.

    convert xlsx file to csv file using openpyxl module of python

    In the picture above you can see the data in the xlsx file which we are going to convert into a CSV file.

    Python Code

    ## XLSX TO CSV
    import openpyxl
    
    filename = 'appending.xlsx'
    
    ## opening the xlsx file
    xlsx = openpyxl.load_workbook(filename)
    
    ## opening the active sheet
    sheet = xlsx.active
    
    ## getting the data from the sheet
    data = sheet.rows
    
    ## creating a csv file
    csv = open("data.csv", "w+")
    
    for row in data:
        l = list(row)
        for i in range(len(l)):
            if i == len(l) - 1:
                csv.write(str(l[i].value))
            else:
                csv.write(str(l[i].value) + ',')
            csv.write('\n')
    
    ## close the csv file
    csv.close()

    Once the program executes successfully, open the file data.csv

    convert xlsx file to csv file using openpyxl module of python

    Conclusion

    Converting Xlsx files to CSV format using Python is a valuable skill for data manipulation and analysis. In this article, we explored various methods and libraries, such as pandas and openpyxl, to seamlessly perform this conversion.

    We discussed step-by-step examples and showcased the flexibility of these libraries in handling Xlsx to CSV conversions.

    Frequently Asked Questions(FAQs)

    1. What is the difference between Xlsx and CSV file formats?

    Xlsx is a file format used by Microsoft Excel to store data with rich formatting and multiple sheets, while CSV (Comma-Separated Values) is a simple text file format that stores tabular data without any formatting, with each value separated by a comma.

    2. How can I convert an Xlsx file to CSV using pandas in Python?

    You can convert an Xlsx file to CSV using pandas by loading the Xlsx file using the pandas.read_excel() function and then saving it as a CSV using the DataFrame.to_csv() method.

    3. Can I convert multiple sheets from an Xlsx file to separate CSV files?

    Yes, you can convert multiple sheets from an Xlsx file to separate CSV files using pandas. You can iterate over the sheets and save each sheet as a separate CSV file.

    4. Are there any libraries specifically designed for Xlsx to CSV conversion in Python?

    Yes, there is a popular library called "openpyxl" that is specifically designed for working with xlsx files in Python. You can use openpyxl to read the xlsx file and then write the data to a CSV file using standard file I/O operations.

    5. Can I specify custom delimiters other than commas for the CSV output?

    Yes, you can specify custom delimiters for the CSV output by setting the sep parameter in pandas' DataFrame.to_csv() method. For example, you can use df.to_csv('output.csv', sep=';') to use a semicolon as the delimiter.

    You may also like:

    About the author:
    I am a Computer Science Student. I am interested in Programming. I love to know about new technologies.
    Tags:python
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS