473,377 Members | 1,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,377 software developers and data experts.

SaveAs to CSV from XLS, Excel

34
Greetings!

A user will supply a .xls or .csv file (with location/directory) as a system argument. If it is a CSV file, the script moves on. If it is a XLS/XLSX file, the script should save it as a CSV file. I wrote the below code, which works, but in actuality it doesn't save the CSV file properly as a CSV file, if you know what I mean. I looked on MSDN site and found some guidance -- line 12 below should be something like

xlApp.ActiveWorkbook.SaveAs(csvOutFile, xlApp.XlFileFormat.xlCSV)

... but that isn't working for me (AttributeError: Excel.Application.XlFileFormat).

Can someone help me with this?

Thanks!

Expand|Select|Wrap|Line Numbers
  1. inFile = sys.argv[1]
  2. print inFile
  3. whereDot = inFile.find('.')
  4. print whereDot
  5. end = inFile[whereDot:]
  6. if end <> ".csv":
  7.     csvOutFile = inFile[0:whereDot] + ".csv"
  8.     print "xls to csv"
  9.     xlApp = win32com.client.Dispatch("Excel.Application")
  10.     xlApp.Visible = 0
  11.     xlApp.Workbooks.Open(inFile)
  12.     xlApp.ActiveWorkbook.SaveAs(csvOutFile)
  13.     xlApp.Quit()
  14. else:
  15.     print "csv already exists, ok"
  16.     csvOutFile = inFile
  17.  
Jan 25 '08 #1
4 13728
jld730
34
Still no luck on my end of figuring this out - surely its simple. I did run across the CSV module for Python, but I don't think that is what I need. I simply need to save an XLS file as a CSV file and then move on -- no need to access the content inside the CSV file. Essentially, after save the file as a true CSV file, I execute GIS/ESRI code (I don't imagine many of you deal in this realm, but if interested, once I have the CSV file I immediately turn it into a geographic layer with
Expand|Select|Wrap|Line Numbers
  1. gp.MakeXYEventLayer_management(csvOutFile, "POINT_X", "POINT_Y", "XY_event_lyr", "")
). This code works on a CSV file that I saveAs a CSV file in Excel, but if I just do what I did first (replace xlsx wqith csv) in the code, it doesn't work. Other testing direct with my GIS sw proves this -- that I need to do a true saveAs CSV.
So, please any help would be greatly appreciated.
Jan 28 '08 #2
dazzler
75
bad guess, maybe it's
xlApp.ActiveWorkbook.SaveAs(csvOutFile, xlApp.FileFormat.CSV) ?

can you point out the msdn page where did you get that info =)
Jan 29 '08 #3
jld730
34
Yeah, I tried that form, too! It was one of my first bad guesses! ;-)

MSDN code library is at > http://msdn2.microsoft.com/en-us/lib...zh(VS.80).aspx

But anyway... yea! I finally found another posting on a GIS forum that gave me this code, which works.
Expand|Select|Wrap|Line Numbers
  1. xlApp.ActiveWorkbook.SaveAs(csvOutFile, FileFormat=24)
I haven't found any documentation out there that states what the various codes are -- like, what if I wanted to save as a txt file? Can any of you experts direct me to it?
Jan 29 '08 #4
jld730
34
I finally ran across a method to get at the value of Excel constants for use in Pyhton scripting.

From http://aspn.activestate.com/ASPN/Coo.../Recipe/528870 ...
To get the value of Excel Constants such as xlEdgeLeft (7) or xlThin (2)
type e.g. Debug.Print xlEdgeLeft in the Immediate window of the VBA editor and press enter.
Mar 3 '08 #5

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

Similar topics

3
by: Vikram | last post by:
Hi, Given below is my task. An user can open the xls file from my website (loaded on the top frame). After filling the Excel, he can click a send button at the bottom frame. By clicking the...
1
by: u7djo | last post by:
Hi, I'm currently building a function in Access that creates an Excel spreadsheet but it doesn't look like the Excel object is being destroyed correctly as the Excel module is still showing in the...
7
by: Gal Zilberman | last post by:
Hi I've found a code which should work, but it doesn't. Please Help Imports Microsoft.Office.Interop ' Refence in the Class Dim oExcel As New Object Dim oBook As Object Dim oSheet As Object...
5
by: Erik Foreman | last post by:
I am trying to use vb.net to programatically share an excel spreadsheet. is there any way to do that? I can't find it. help. *** Sent via Developersdex http://www.developersdex.com *** Don't...
0
by: OutdoorGuy | last post by:
Greetings, I have the code below where I am instantiating Excel and then attempting to the save the new workbook (as "Test.xls"). However, on the line of code that attempts to save the...
4
by: =?Utf-8?B?QnJpYW5ESA==?= | last post by:
Hi Does anyone know of a way (via code behind) to pull a single sheet out of a Excel workbook and convert it to a stand alone html document? Thanks Brian
0
by: DPK | last post by:
Hello friends!!! I made a prog. to make the excel. It is running well but when it saves the excel file it open the save dialog box to save the file. Becasue I am using SAVE function here instead...
10
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
3
by: =?Utf-8?B?Um9k?= | last post by:
The applicacion sends me the next message "Method not found: Excel._Worksheet.SaveAs (..." I thinks that its about permissions but I do not know how to fix it. I have the same source in two...
1
by: Abhishek Chauhan04 | last post by:
Hi, I an new to this forum, I trying to open an excel workbook update it with some details\data and the save it using excelWorkbook.SaveAs method but the exceution freezes at the SaveAs method...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.