2

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?

1
  • What happens when you run the program? Commented Dec 23, 2014 at 9:11

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.