3

I'm loading up a .xlsx with win32com and would like to save the results as a csv when I'm done.

myworkbook.SaveAs('results.csv')

gives me an xlsx file with a csv extension. How do I save as an actual CSV?

Jason Goldstein
  • 1,117
  • 2
  • 11
  • 20

4 Answers4

4

I think that if you add the type after the filename, it should work. (Can't test right now.)

I think the type for CSV (DOS) is 24.

myworkbook.SaveAs('results.csv', 24)
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
4

Here are the docs for saveAs: http://msdn.microsoft.com/en-us/library/bb214129.aspx

from win32com.client import constants as c
myWorkBook.SaveAs('results.csv', c.xlCSV)
Gerrat
  • 28,863
  • 9
  • 73
  • 101
  • If the file already exists, then Excel will show a pop-up window. Is there a way to deal with this, or direct the function to always overwrite? – rleelr Apr 25 '17 at 10:09
  • You can set `xl.DisplayAlerts = False` beforehand and Excel will just overwrite the file. – rleelr Apr 25 '17 at 10:25
3

You have to specify the type after the filename.

For CSV the following modes are available:

xlCSV = 6         # Comma separated value.
xlCSVMac = 22,    # Comma separated value.
xlCSVMSDOS = 24,  # Comma separated value.
xlCSVWindows =23, # Comma separated value.

Available file formats can be fond here, the spec of the saveAs method can be found here. Even as there is no example for python, the parameters and values should be the same.

circus
  • 2,470
  • 3
  • 21
  • 25
1

I have not used this library but it might be worth giving a shot:

http://pypi.python.org/pypi/ooxml

Wes McKinney
  • 101,437
  • 32
  • 142
  • 108
  • I need to load a plugin, so a regular xlsx parsing library is no-go. win32com does what I need, the problem is the SaveAs method docstring is unclear. – Jason Goldstein May 31 '11 at 17:02