openpyxl将CSV转换为EXCEL

我如何转换CSV文件:使用openpyxl模块的分隔符到XLS(Excel表)? 如果你提供一个示例脚本,这将是一件好事。

Solutions Collecting From Web of "openpyxl将CSV转换为EXCEL"

那么你去…

 import csv from openpyxl import Workbook from openpyxl.cell import get_column_letter f = open(r'C:\Users\Asus\Desktop\herp.csv') csv.register_dialect('colons', delimiter=':') reader = csv.reader(f, dialect='colons') wb = Workbook() dest_filename = r"C:\Users\Asus\Desktop\herp.xlsx" ws = wb.worksheets[0] ws.title = "A Snazzy Title" for row_index, row in enumerate(reader): for column_index, cell in enumerate(row): column_letter = get_column_letter((column_index + 1)) ws.cell('%s%s'%(column_letter, (row_index + 1))).value = cell wb.save(filename = dest_filename) 

简单得多,简约的解决方案:

 import csv import openpyxl wb = openpyxl.Workbook() ws = wb.active f = open('file.csv') reader = csv.reader(f, delimiter=':') for row in reader: ws.append(row) f.close() wb.save('file.xlsx')