By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,761 Members | 1,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,761 IT Pros & Developers. It's quick & easy.

Convert multiple xlsx files to csv using xlsx2csv for Python

P: 2
Hi all

I have a large folder containing multiple xlsx (only one sheet) files which I need to convert to a csv format.
I can convert them individually using the [xlsx2csv](https://github.com/dilshod/xlsx2csv) but I cannot make a working function that loops over all the files in the folder and saves them with the same names as the xlsx-files but as csv.

I have been searching a lot of different forums, but I have been unable to find a helpful thread.

Can anyone help me?

Best,
Martin

OBS...
I do the individual conversion like the following:


Expand|Select|Wrap|Line Numbers
  1. from xlsx2csv import *
  2. xlsx2csv("dir\ID_01.xlsx", open("ID_01.csv", "w+"))
And that converts the file ID_01.xlsx into ID_01.csv.
Feb 14 '13 #1

✓ answered by bvdet

That can be accomplished with the os module. You will need to compile a list of file names from your directory and iterate on the list. The following example will demonstrate how you can use the os module and string formatting to do this.
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. dirname = your_directory_name
  4. new_ext = ".csv"
  5. fileList = []
  6. for fn in os.listdir(dirname):
  7.     fn1 = os.path.join(dirname, fn)
  8.     if os.path.isfile(fn1):
  9.         fileList.append(fn1)
  10. print "\n".join(fileList)
  11.  
  12. output = []
  13. for fn in fileList:
  14.     dn, fn1 = os.path.split(fn)
  15.     output.append("Old file name: %s\nNew file name: %s\n" %
  16.                   (fn, os.path.join(dn, os.path.splitext(fn1)[0]+new_ext)))
  17. print "\n".join(output)

Share this Question
Share on Google+
3 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
That can be accomplished with the os module. You will need to compile a list of file names from your directory and iterate on the list. The following example will demonstrate how you can use the os module and string formatting to do this.
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. dirname = your_directory_name
  4. new_ext = ".csv"
  5. fileList = []
  6. for fn in os.listdir(dirname):
  7.     fn1 = os.path.join(dirname, fn)
  8.     if os.path.isfile(fn1):
  9.         fileList.append(fn1)
  10. print "\n".join(fileList)
  11.  
  12. output = []
  13. for fn in fileList:
  14.     dn, fn1 = os.path.split(fn)
  15.     output.append("Old file name: %s\nNew file name: %s\n" %
  16.                   (fn, os.path.join(dn, os.path.splitext(fn1)[0]+new_ext)))
  17. print "\n".join(output)
Feb 14 '13 #2

P: 2
Thanks... that was just what I needed! Amazing :-)
Feb 15 '13 #3

P: 1
Hi I can not use "from xlsx2csv import *
xlsx2csv("dir\ID_01.xlsx", open("ID_01.csv", "w+"))" to convert a file. the issue is xlsx2csv is not defined.
Nov 8 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.