Python convert csv to xlsx
本文中有一个Python示例,可将csv转换为xls。
但是,我的文件有超过65536行,因此xls不起作用。如果我将文件命名为xlsx,则没有任何区别。是否有Python包可以转换为xlsx?
这是使用xlsxwriter的示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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() |
仅供参考,还有一个名为openpyxl的程序包,可以读取/写入Excel 2007 xlsx / xlsm文件。
希望有帮助。
使用我的库
1 | $ pip install pyexcel pyexcel-xlsx |
您可以在一个命令行中完成它:
1 2 3 4 5 6 | 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") |
每个csv将有其自己的工作表,并且名称将是其文件名。
首先安装openpyxl:
1 | pip install openpyxl |
然后:
1 2 3 4 5 6 7 8 9 10 | 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') |
添加一个专门使用pandas库读取.csv文件并另存为.xlsx文件的答案。本示例使用
完全可复制的示例仅使用numpy生成随机数,如果要使用自己的.csv文件,可以将其删除。
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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() |
我如何使用openpyxl lib执行该操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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") |
简单的1对1 CSV到XLSX文件转换,而无需枚举/循环遍历各行:
1 2 3 4 | import pyexcel sheet = pyexcel.get_sheet(file_name="myFile.csv", delimiter=",") sheet.save_as("myFile.xlsx") |
注意事项:
那么当Excel尝试时,生成的XLSX文件将引发错误
加载它。 Excel将提供修复它所做的错误的方法,但是它
令人沮丧
将目录中的所有CSV文件合并到一个XLSX工作簿中,
它适合于不同的用例,而不仅仅是尝试一对一的CSV文件
XLSX文件转换。
使用熊猫的简单两行代码解决方案
1 2 3 4 | import pandas as pd read_file = pd.read_csv ('File name.csv') read_file.to_excel ('File name.xlsx', index = None, header=True) |
有一个简单的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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') |