def csvfile_to_wb(csv_filename):
'''Open a CSV file and return an openpyxl workbook.'''
logger.log(
DEBUG_DETAILED,
'Converting CSV file {} into an XLSX workbook.'.format(csv_filename))
with open(csv_filename) as csv_file:
dialect = csv.Sniffer().sniff(csv_file.read())
if USING_PYTHON2:
for attr in dir(dialect):
a = getattr(dialect, attr)
if type(a) == unicode:
setattr(dialect, attr, bytes(a))
csv_file.seek(0)
reader = csv.reader(csv_file, dialect)
wb = pyxl.Workbook()
ws = wb.active
for row_index, row in enumerate(reader, 1):
for column_index, cell in enumerate(row, 1):
if cell not in ('', None):
ws.cell(row=row_index, column=column_index).value = cell
return (wb, dialect)
评论列表
文章目录