简体   繁体   中英

How do I convert excel file with n worksheets into n csv files?

有没有办法将excel文件中的工作表转换为多个csv文件?

Yes. There is.

  1. xlrd will allow you to read a spreadsheet. http://pypi.python.org/pypi/xlrd

  2. csv will allow you to write CSV files. http://docs.python.org/library/csv.html

This happened to me so often, that I uploaded a spreadsheet reader library to SourceForge. http://sourceforge.net/projects/stingrayreader/ .

xlrd is fine but won't handle the newer excel format (xlsx). (yet)

One alternative, on a windows machine with excel installed, is to use the Python/COM interface to drive excel.

A similar approach (but not reliant on platform and vendor) is to use Open Office to read the spreadsheet via its python api. An article with python script is: Convert Spreadsheets to CSV files with Python and PyUno

One gotcha: if you use the native 'save as', either of excel or OO, to do the conversion, you get the data as formatted in the sheet . (This is the approach used by the above OO solution and the usual excel/COM solution.) So numeric data, for example, will be rounded to the number of decimal places that excel would display. (For dates, this is usually a good thing, since you get the formatted date string, rather than the underlying numerical value.)

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