How can I convert all .csv files in a folder into

2020-05-24 22:36发布

I have a folder with 20 or so .csv files. I want to create a single .xlsx file (excel file) with multiple worksheet tabs (1 for each .csv).

Can somebody recommend a simple script to do this where the user only needs to specify 2 things: the folder with the .csv files & the path to the new .xlsx file?

I found the same question at this thread but without an answer I could understand:

https://superuser.com/questions/742454/how-to-convert-many-csv-files-into-1-xlsx-file-with-multiple-tabs?rq=1

Thanks All,

标签: python excel csv
2条回答
兄弟一词,经得起流年.
2楼-- · 2020-05-24 23:13

I see you've tagged the question with python, but an option is using the Power Query ( free Excel add-in from Microsoft ) that has a "Get External Data" -> "From Folder" option.

查看更多
Fickle 薄情
3楼-- · 2020-05-24 23:24

The following code takes folder name (with multiple csv files) as input and creates output as a single xls file with multiple sheets

import xlwt, csv, os

csv_folder = "Output/"

book = xlwt.Workbook()
for fil in os.listdir(csv_folder):
    sheet = book.add_sheet(fil[:-4])
    with open(csv_folder + fil) as filname:
        reader = csv.reader(filname)
        i = 0
        for row in reader:
            for j, each in enumerate(row):
                sheet.write(i, j, each)
            i += 1

book.save("Output.xls")
查看更多
登录 后发表回答