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 脚本
2022-05-13 09:28:13
浏览数 (3)