def csv_to_xlsx(input,output):
with open(input, encoding='ANSI') as f:
read = csv.reader(f)
workbook = Workbook()
sheet = workbook.active
l = 1
com = re.compile("(\n|\t){1,}")
for line in read:
st = ",".join(line)
ss = re.sub(com, "\n", st)
line = ss.split(",")
r = 1
for i in line:
sheet.cell(row =l, column = r).value=i
sheet.cell(row =l, column = r).alignment=Alignment(wrapText=True)
r = r + 1
l = l + 1
col_width = []
i = 0
for col in sheet.columns:
for j in range(len(col)):
if j == 0:
col_width.append(len(str(col[j].value)))
else:
if col_width[i] < len(str(col[j].value)):
col_width[i] = len(str(col[j].value))
i = i + 1
for i in range(len(col_width)):
col_letter = get_column_letter(i+1)
if col_width[i] > 100:
sheet.column_dimensions[col_letter].width = 100
elif col_width[i]:
if col_width[i]<=6:col_width[i]=10
sheet.column_dimensions[col_letter].width = col_width[i]
workbook.save(output) # 保存Excel
python csv文件转换成excel 脚本
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 如何使用python将大量数据导出到Excel中的小技巧 (1) 问题描述:为了更好地展示数据,Excel格式的...