Writing to excel instea of csv

Good day Experts…I have found this code from ex-colleague. This code write the result to csv. Can someone please guide me how to convert this code instead of csv will write to excel xlsx. Thanks in advance.

if (write_csv_file==True):
    input_file_base = os.path.splitext(input_file)[0]
    with open(input_file_base + "thefile.csv", 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter=';')
        writer.writerow(['a[a]', 'b[b]', 'c[c]', 'd[d]', 'e[e]', 'f[f]'])
        for i in range(0,6):
        for i in range(0,len(T)):


I can give you a few simple hints to help you along:

  1. Excel can read CSV files. If you want this code to write a file which Excel can import, the code already does that.
  2. You can convert CSV files to Excel format manually, via Excel. If you really need Excel-format, not CSV-format, files for a particular purpose, and the quantity is small enough that hand conversion is acceptable, then consider that option.
  3. As you can see from the expression csv.writer(), this code uses the csv module from the Python standard library. One general approach to solving problems in Python is searching for a way to do it using the standard library. A quick search through the Python standard library index unfortunately does not yield a lead.
  4. The other source of Python modules is PyPI, the Python Package Index. Try searching there. There is a module called excel which claims to read, not write, Excel files.
  5. The capability to write Excel-format files might not be labelled “Excel”. The OpenOffice.org and LibreOffice applications also can write Excel-format files. The .xlsx file format is similar to what these applications use. Search also for a package using these names as search terms.

Good luck!

There’s a module on PyPI called openpyxl that can read and write .xlsx files.

1 Like

Thanks to all… for the guidance…i manage to do it with the following code of reference:

import pandas as pd

read_file = pd.read_csv (r'Path where the CSV file is stored\File name.csv')
read_file.to_excel (r'Path to store the Excel file\File name.xlsx', index = None, header=True)

Have a great day to all