Python将csv转换为xlsx [英] Python convert csv to xlsx

查看:1364
本文介绍了Python将csv转换为xlsx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章中,有一个Python示例,从csv转换为xls。

In this post there is a Python example to convert from csv to xls.

但是,我的文件有超过65536行,所以xls不工作。如果我命名文件xlsx它不起作用。是否有一个Python包转换为xlsx?

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?

推荐答案

这里是一个使用xlsxwriter

import os
import glob
import csv
from xlsxwriter.workbook import Workbook


for csvfile in glob.glob(os.path.join('.', '*.csv')):
    workbook = Workbook(csvfile + '.xlsx')
    worksheet = workbook.add_worksheet()
    with open(csvfile, 'rb') 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,还有一个名为openpyxl ,可以读/写Excel 2007 xlsx / xlsm文件。

FYI, there is also a package called openpyxl, that can read/write Excel 2007 xlsx/xlsm files.

希望有帮助。

这篇关于Python将csv转换为xlsx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆