如何使用:
模块将带有openpyxl
分隔符的CSV文件转换为XLS(Excel工作表)?
答案 0 :(得分:34)
一个更简单,极简主义的解决方案:
import csv
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
with open('file.csv') as f:
reader = csv.reader(f, delimiter=':')
for row in reader:
ws.append(row)
wb.save('file.xlsx')
答案 1 :(得分:10)
嗯,你走了......
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)
答案 2 :(得分:0)
这是Adam扩展的解决方案,用于删除openpyxl认为非法的字符,并会抛出异常:
import re
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
...
##ws.append(row) - Replace with the code below
ws.append([ILLEGAL_CHARACTERS_RE.sub('',row)])
ILLEGAL_CHARACTERS_RE是一个编译的正则表达式,其中包含openpyxl认为的字符"非法"。代码只是用空字符串替换这些字符。
来源:Bitbucket openpyxl issue #873 - Remove illegal characters instead of throwing an exception
答案 3 :(得分:0)
在 John 的建议之上,我使用 function 稍微修改了我的脚本以删除所有原始数据的 string 撇号。通过这种方式,我设法检查了所有原始数据(字符串和数字),这些数据也放置在相应的单元格中。最后,我从第 20 行开始将数字数据分配给浮点类型。这是因为从第 20 行开始的所有数字数据都存在,而上面的所有数据都是文本。
cell_value = cell.replace('"', '')
下面是我的脚本:
import csv
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
with open(filepath1_csv) as f:
reader = csv.reader(f)
for row_index, row in enumerate(reader):
for column_index, cell in enumerate(row):
column_letter = column_index + 1
cell_value = cell.replace('"', '')
ws.cell(row = row_index + 1, column = column_letter).value = cell_value
for row in ws.iter_rows(min_row=20, min_col=1, max_col=5,
max_row=ws.max_row):
for cell in row:
if cell.value is None:
break
else:
cell.value = float(cell.value)
wb.save(filename = filepath1_xlsx)