0

我使用 pyexcel 编写了一些代码,将我文件夹中的所有文件从 csv 转换为 xlsx。但我想用与文件夹中每个文件相同的名称(而不是 file1.xlsx)导出它。你能帮忙吗?

from pyexcel.cookbook import merge_all_to_a_book
import pyexcel.ext.xlsx
import glob
import os
os.chdir(“/Users/vanicek/Desktop/csv2xlsx” )

i = 0

for file in glob.glob(“*.csv”):
       while os.path.exists(“file%s.xlsx” % i):
               i+=1
       merge_all_to_a_book(glob.glob(“*.csv”), “file%s.xlsx” % i)

print “Exported.”
4

1 回答 1

1
import os
import glob
import csv
from xlsxwriter.workbook import Workbook


for csvfile in glob.glob(os.path.join('.', '*.csv')):

workbook = Workbook(csvfile[:-4] + '.xlsx')
worksheet = workbook.add_worksheet()
with open(csvfile, 'rt', encoding='utf8') as f:
    reader = csv.reader(f)
    for r, row in enumerate(reader):
        for c, col in enumerate(row):
            worksheet.write(r, c, col)
workbook.close()
于 2018-04-11T13:15:53.167 回答