Geting path of Syntax file with Python using SPSS 18

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Geting path of Syntax file with Python using SPSS 18

la volta statistics

Hi All

 

I have used os.path and os.getcwd() in SPSS 15 to get the path of the syntax file I am working with.

Now I have SPSS 18 installed on my computer as well.

 

When I run the same code, I get now 'C:\Programme\SPSSInc\PASWStatistics18', the path where the program is located.

 

What do I have to do to get the path of the syntax file I am working with?

 

Thanks, Christian

 

 

* This worked for SPSS 15: Assuming I have a file saved at D:\Temp\test.sps, I get 'D:\Temp' .

* With SPSS 18: I get 'C:\Programme\SPSSInc\PASWStatistics18'.

BEGIN PROGRAM.

import os,spss

myPath =  os.path.abspath(".")

myPath2 = os.getcwd().replace("\\","/")

 

print myPath

print myPath2

 

END PROGRAM.

 

 

**********************************
la volta statistics

Christian Schmidhauser, Dr.phil.II
Weinbergstrasse 108
CH-8006 Zürich
Tel: +41 (043) 233 98 01
Fax: +41 (043) 233 98 02
email:
[hidden email] 
Web: www.lavolta.ch

 

Reply | Threaded
Open this post in threaded view
|

Linking SPSS with MS Office (Macro)

Dean Tindall

Hi List,

 

I was just wondering if anyone has managed to write macros interlinking MS Office (Excel) and SPSS, namely for the automation of reporting,

 

And more importantly if anyone knew of a good resource where this approach was outlined

 

J

 

Thanks in advance,

 

Dean

This email was sent from:- Nunwood Consulting Ltd. (registered in England and Wales no. 3135953) whose head office is based at:- 7, Airport West, Lancaster Way, Yeadon, Leeds, LS19 7ZA. Tel +44 (0) 845 372 0101 Fax +44 (0) 845 372 0102 Web http://www.nunwood.com Email [hidden email] This e-mail is confidential and intended solely for the use of the individual(s) to whom it is addressed. If you are not the intended recipient, be advised that you have received this e-mail in error and that any use, dissemination, forwarding, printing, copying of, or any action taken in reliance upon it, is strictly prohibited and may be illegal. To review our privacy policy please visit: www.nunwood.com/privacypolicy.html
Reply | Threaded
Open this post in threaded view
|

Re: Geting path of Syntax file with Python using SPSS 18

John F Hall
In reply to this post by la volta statistics
Go to your file and pick up the address inside your computer.  If it's a Word file the address should be displayed somewhere on your screen.  Otherwise right click on the file, then Properties
 
I'm working on some tutorials for 18.  The very first file students need to use is a data set from my website which they have to download and copy into a folder on their own computer.  They can do it either on a floppy a: or preferably on Desktop.
 
First they have to create a folder myclass then copy my data file to it as myclass.txt.
 
Depending on which method they use, the computer address of this file is now:

 

'a:\myclass\myclass.txt'  or  

'c:\Documents and Settings\Owner\Desktop\myclass\myclass.txt'

 

For their very first shot at SPSS they have to use DATA LIST to read three variables.  For simplicity, in this example, I use floppy drive a: and the name 'a:\myclass\myclass.txt'.  I'm also using lower case as it's easier to read.  The full data list command now looks like this:

 

data list       file = 'a:\myclass\myclass.txt'

                    records = 1

                    /1  serial 1-2

                         sex 22

                         age 26-27 .

 

To read the data from folder myclass on desktop the first line should read (more complex):

 

data list  file 'C:\Documents and Settings\Owner\Desktop\myclass\myclass.txt'  

 

If it works, they get a message, "You have just written your first SPSS command.  Well done!"

 

This is the only time they have to use such a pathway.  Thereafter they can work entirely by by double clicking files or by downloading them.

 

The original tutorial for SPSS 15 is on my website as:

 

1.3.3 Stage 1: From questionnaire to first SPSS saved file ,but has now been split into bite-sized chunks for SPSS/PASW 18 which will be uploaded as soon as they are finished and th hyperlinks checked.

 

----- Original Message -----
Sent: Thursday, April 22, 2010 1:27 PM
Subject: Geting path of Syntax file with Python using SPSS 18

Hi All

 

I have used os.path and os.getcwd() in SPSS 15 to get the path of the syntax file I am working with.

Now I have SPSS 18 installed on my computer as well.

 

When I run the same code, I get now 'C:\Programme\SPSSInc\PASWStatistics18', the path where the program is located.

 

What do I have to do to get the path of the syntax file I am working with?

 

Thanks, Christian

 

 

* This worked for SPSS 15: Assuming I have a file saved at D:\Temp\test.sps, I get 'D:\Temp' .

* With SPSS 18: I get 'C:\Programme\SPSSInc\PASWStatistics18'.

BEGIN PROGRAM.

import os,spss

myPath =  os.path.abspath(".")

myPath2 = os.getcwd().replace("\\","/")

 

print myPath

print myPath2

 

END PROGRAM.

 

 

**********************************
la volta statistics

Christian Schmidhauser, Dr.phil.II
Weinbergstrasse 108
CH-8006 Zürich
Tel: +41 (043) 233 98 01
Fax: +41 (043) 233 98 02
email:
[hidden email] 
Web: www.lavolta.ch

 

Reply | Threaded
Open this post in threaded view
|

Automatic reply: Geting path of Syntax file with Python using SPSS 18

Genevieve Odoom
Hello,
Thank you for your email. I will be out of the office on Thursday, April 22nd, returning on Firday, April 23rd and will respond to your email upon my return.

Thanks!
Genevieve Odoom
Policy and Program Analyst
OANHSS
Suite 700 - 7050 Weston Rd. Woodbridge,
ON L4L 8G7
Tel: (905) 851-8821 x 241 Fax: (905) 851-0744
[hidden email]
www.oanhss.org

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Getting path of Syntax file with Python using SPSS 18

Jon K Peck
In reply to this post by la volta statistics

Actually, I don't think os.getcwd() would ever have been a reliable way to get the syntax document path.  In V15 it would have returned the working directory of the backend process, which is not necessarily where the syntax document would be located.  The process structure of Statistics is different in V18, so getcwd is returning the current directory of the Python process, which need not be related to the current spssengine process working directory.  

In V15, if you opened a syntax file, that became the process working directory, but if you opened a data file, then that became the working directory.  INSERT and INCLUDE files are another story.

Subsequently we stabilized the working directory of the backend process in order to make it more predictable.

But the following code returns the path, if any, of the document in the current designated syntax window.

begin program.
import spss, SpssClient
SpssClient.StartClient()
path = SpssClient.GetDesignatedSyntaxDoc().GetDocumentPath()
print path
end program.

You could iterate through all the syntax windows if you have more than one instead of just getting the designated one.

This code is written to run as a program, but you could equally well run it as a script (without the begin/end program statements).
Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: la volta statistics <[hidden email]>
To: [hidden email]
Date: 04/22/2010 05:19 AM
Subject: [SPSSX-L] Geting path of Syntax file with Python using SPSS 18
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Hi All
 
I have used os.path and os.getcwd() in SPSS 15 to get the path of the syntax file I am working with.
Now I have SPSS 18 installed on my computer as well.
 
When I run the same code, I get now 'C:\Programme\SPSSInc\PASWStatistics18', the path where the program is located.
 
What do I have to do to get the path of the syntax file I am working with?
 
Thanks, Christian
 
 
* This worked for SPSS 15: Assuming I have a file saved at D:\Temp\test.sps, I get 'D:\Temp' .
* With SPSS 18: I get 'C:\Programme\SPSSInc\PASWStatistics18'.
BEGIN PROGRAM.
import os,spss
myPath =  os.path.abspath(".")
myPath2 = os.getcwd().replace("\\","/")
 
print myPath
print myPath2
 
END PROGRAM.
 
 
**********************************
la volta statistics

Christian Schmidhauser, Dr.phil.II
Weinbergstrasse 108
CH-8006 Zürich
Tel: +41 (043) 233 98 01
Fax: +41 (043) 233 98 02
email:
mailto:schmidhauser@...
Web:
www.lavolta.ch
 

Reply | Threaded
Open this post in threaded view
|

AW: Linking SPSS with MS Office (Macro)

la volta statistics
In reply to this post by Dean Tindall

Hi Dean

 

Here is a script (named CreateWord.sps) I used a while ago to open Word and save the files with a parameter (strParameters) in its file name.

I assume similar code could be used for Excel.

 

The script was called with a macro. The path of the script file was !Path_M (you may replace this with the actual path of the script).

The parameter (a name, !Name) was from a list of names and will be the name of the Word document.

 

Hope this helps

Christian

 

 

* Export to Word.

DEFINE !DoData   (Name !TOKENS(1)).

 

SCRIPT file= !Path_M + '\CreateWord.SBS'

          (!Name).

 

!ENDDEFINE.

 

You may replace the syntax part with:

 

SCRIPT file= 'D:\Temp\CreateWord.SBS'

          ("YourWordFileName").

 

 

 

 

 

Option Explicit

 

Dim objWord As Object

Dim objWordApp As Object

Dim objDocument As Object

 

Sub Main

CreateWord

 

End Sub

Sub CreateWord

Dim myFile          As String

Dim myPath          As String

Dim myPath2         As String

Dim mReturn             As Integer

Dim strParameters       As String

Dim strMakroDir   As String

Dim strSynDir           As String

Dim strChartDir         As String

Dim intLoc              As Integer

 

'On Error Resume Next

On Error GoTo X

 

strParameters = objSpssApp.ScriptParameter(0)  'GetParameters()

 

Set objWord = CreateObject("Word.Application")

 

strMakroDir = MacroDir()

myPath = strMakroDir & "\HOC Template.doc"

intLoc = InStrRev(strMakroDir,"\")

strSynDir = Left(strMakroDir,intLoc-1)

'Debug.Print strSynDir

 

strChartDir = "Documents"

myPath2 =  strSynDir & "\" & strChartDir & "\" & strParameters & "\" & "HOC_2 " & strParameters & ".doc"

 

 

Set objDocument = objWord.Documents.Open(myPath )

objWord.Visible = True

 

 

objDocument.SaveAs(myPath2)

objDocument.Close SaveChanges:=0     'wdDoNotSaveChanges

 

objWord.Quit

 

 

X:  Debug.Print "Err=";Err

    Err.Clear

    Debug.Print "Err=";Err

    Resume Next

 

 

End Sub

 

**********************************
la volta statistics

Christian Schmidhauser, Dr.phil.II
Weinbergstrasse 108
CH-8006 Zürich
Tel: +41 (043) 233 98 01
Fax: +41 (043) 233 98 02
email:
[hidden email] 
Web: www.lavolta.ch


Von: SPSSX(r) Discussion [mailto:[hidden email]] Im Auftrag von Dean Tindall
Gesendet: Donnerstag, 22. April 2010 14:09
An: [hidden email]
Betreff: Linking SPSS with MS Office (Macro)

 

Hi List,

 

I was just wondering if anyone has managed to write macros interlinking MS Office (Excel) and SPSS, namely for the automation of reporting,

 

And more importantly if anyone knew of a good resource where this approach was outlined

 

J

 

Thanks in advance,

 

Dean

This email was sent from:- Nunwood Consulting Ltd. (registered in England and Wales no. 3135953) whose head office is based at:- 7, Airport West, Lancaster Way, Yeadon, Leeds, LS19 7ZA. Tel +44 (0) 845 372 0101 Fax +44 (0) 845 372 0102 Web http://www.nunwood.com Email [hidden email] This e-mail is confidential and intended solely for the use of the individual(s) to whom it is addressed. If you are not the intended recipient, be advised that you have received this e-mail in error and that any use, dissemination, forwarding, printing, copying of, or any action taken in reliance upon it, is strictly prohibited and may be illegal. To review our privacy policy please visit: www.nunwood.com/privacypolicy.html