Hi. I'm using a variant of the script found at http://superuser.com/questions/301431/how-to-batch-convert-csv-to-xls-xlsx to attempt to create an .xls file from multiple .csv files. A copy of my current code is below. I have three .csv files, but that may change. They're simply called 01.csv, 02.csv, 03.csv. I would like to create an .xls in which the contents of 01.csv go on the tab 01, and 02.csv and 03.csv similarly, giving 3 worksheets called 01, 02, 03 in a book called output.xls. I expected the add_sheet method to do just that-- add a sheet. But, that's not happening. I end up with a single sheet called 03 in a file called output.xls. How do I get these different .csv files onto their own sheets?
#!/usr/bin/env pythonimport osimport globimport csvimport xlwtfor csvfile in glob.glob(os.path.join('.', '*.csv')):
wb = xlwt.Workbook()fpath = csvfile.split("/", 1)fname = fpath[1].split(".", 1) ## fname[0] should be our worksheet name
ws = wb.add_sheet(fname[0])with open(csvfile, 'rb') as f:reader = csv.reader(f)for r, row in enumerate(reader):for c, col in enumerate(row):ws.write(r, c, col)
wb.save('output.xls')
I just realized I made a rather foolish mistake by placing wb = xlwt.Workbook() inside the loop. So, I'm now able to properly add worksheets as expected.
--
You received this message because you are subscribed to the Google Groups "python-excel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-excel...@googlegroups.com.
To post to this group, send email to python...@googlegroups.com.
Visit this group at http://groups.google.com/group/python-excel.
For more options, visit https://groups.google.com/d/optout.