csv文件转换为xlsx文件

"""
@author: ahan
@file: csv2excel.py
@time: 2020/5/20 22:36
@ide: pycharm

"""

import csv
import openpyxl


def rewrite_excel(fileName):
wb = openpyxl.Workbook()
sheet = wb.create_sheet()
sheet.title = '测试用例'

del wb['Sheet']
with open(fileName, 'r', encoding="utf-8") as f:
lines = csv.reader(f)
for k, line in enumerate(lines):
numLines = len(line)
for j in range(0, numLines):
try:
sheet.cell(row=k + 1, column=j + 1).value = float(line[j])
except ValueError:
sheet.cell(row=k + 1, column=j + 1).value = line[j]
excel_path = fileName[:-4] + ".xlsx"
wb.save(excel_path)
print(fileName + '转换完成')
return excel_path


if __name__ == "__main__":
fileName = r"C:\Users\ahan\Desktop\111\审批流_20200520_174024.csv"
rewrite_excel(fileName)
posted @ 2020-05-21 12:30  &阿岩  Views(1853)  Comments(0Edit  收藏  举报