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?
asked Jul 15, 2013 at 10:21
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
11.2k6 gold badges23 silver badges83 bronze badges
answered Jul 16, 2013 at 18:51
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
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 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
19.6k21 gold badges75 silver badges113 bronze badges
answered Mar 9, 2017 at 19:07
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
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:
- 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. - 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 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
26.8k5 gold badges82 silver badges92 bronze badges
answered Aug 17, 2016 at 16:58
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 DingDavid Ding
1,4331 gold badge15 silver badges13 bronze badges
Improve Article
Save Article
Like Article
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:
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:
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
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 fileexcel_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:
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 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.
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?
asked Jul 15, 2013 at 10:21
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
11.2k6 gold badges23 silver badges83 bronze badges
answered Jul 16, 2013 at 18:51
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
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 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
19.6k21 gold badges75 silver badges113 bronze badges
answered Mar 9, 2017 at 19:07
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
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:
- 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. - 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 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
26.8k5 gold badges82 silver badges92 bronze badges
answered Aug 17, 2016 at 16:58
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 DingDavid Ding
1,4331 gold badge15 silver badges13 bronze badges