简体   繁体   中英

write multiple excel files with xlsxwriter python

is there a way to create multi excel files with xlsxwriter?

from itertools import chain
import glob ,csv, sys, os

openSoundingFile = 'D:/apera/Workspace/Sounding2/*.txt'


for filename in glob.glob(openSoundingFile):
    newName = filename
    spamReader = csv.reader(open(filename, 'rb'), delimiter=';',quotechar='"')
    workbook = xlsxwriter.Workbook('D:/apera/Workspace/Sounding2/' + newName[:-4] + '.xlsx'
    sheet = workbook.add_worksheet('Original data')

    for rowx, row in enumerate(spamReader):
        for colx, value in enumerate(row):
            sheet.write(rowx, colx, value)


    workbook.close()

so i wanna to save all the txt files to exel files. I think the problem is here

workbook = xlsxwriter.Workbook('D:/apera/Workspace/Sounding2/' + newName[:-4] + '.xlsx'

if i'm not using + newName[:-4] + it will work but only write 1 excel files. Is there a way to do it?

The error showed that you combined the path to the files 2 times on top of each other:

'D:/apera/Workspace/Sounding2/bla.txtD:/apera/Workspace/Sounding2/'

This does it for me:

import xlsxwriter
import glob ,csv

openSoundingFile = 'D:/apera/Workspace/Sounding2/*.txt'

for filename in glob.glob(openSoundingFile):
    spamReader = csv.reader(open(filename, 'rb'), delimiter=';',quotechar='"')
    # Note that filename is the full path already! Just [:-4] to remove .txt
    workbook = xlsxwriter.Workbook(filename[:-4] + '.xlsx')
    sheet = workbook.add_worksheet('Original data')
    for rowx, row in enumerate(spamReader):
        for colx, value in enumerate(row):
            sheet.write(rowx, colx, value)
    workbook.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM