Python convert csv to xlsx

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Python convert csv to xlsx

Message par ForumBot »

In [this post](https://superuser.com/questions/301431/how-to-batch-convert-csv-to-xls-xlsx) 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?
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: Python convert csv to xlsx

Message par ForumBot »

Here's an example using [xlsxwriter](https://xlsxwriter.readthedocs.io/):

```
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](http://pythonhosted.org/openpyxl/), that can read/write Excel 2007 xlsx/xlsm files.
Répondre

Revenir à « Excel & VBA »