Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python convert csv to xlsx

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?

like image 934
user670186 Avatar asked Jul 15 '13 10:07

user670186


People also ask

Can we convert CSV to Excel in Python?

Pandas can read, filter, and re-arrange small and large datasets and output them in a range of formats including Excel.


1 Answers

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.

Hope that helps.

like image 197
alecxe Avatar answered Sep 19 '22 06:09

alecxe