Python and OpenOffice.org Basic

Creating a macro - Writing a Script - Using the API

Python and OpenOffice.org Basic

Postby Lazy-legs » Thu Aug 28, 2008 3:58 pm

Hello,

To email a backup copy of the currently opened document. I've written a simple OpenOffice.org Basic macro that executes the following Python script:

Code: Select all   Expand viewCollapse view
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
import sys

GMAIL_USER = 'username@gmail.com'
GMAIL_PASSWD = "password"
RECIPIENT = "backup@gmail.com"
SUBJECT = "Writer's Tools document backup: " + sys.argv[2]
MESSAGE = "A backup version of an OpenOffice.org document is attached."

def mail(to, subject, text, attach):
   msg = MIMEMultipart()

   msg['From'] = GMAIL_USER
   msg['To'] = to
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(GMAIL_USER, GMAIL_PASSWD)
   mailServer.sendmail(GMAIL_USER, to, msg.as_string())
   mailServer.close()

mail(RECIPIENT, SUBJECT, MESSAGE, sys.argv[1])


The sys.argv[1] and sys.argv[1] values are parameters that are sent from the macro using the Shell() routine. Here is the macro:

Code: Select all   Expand viewCollapse view
Sub EmailBackup()

Dim Args(0) As New com.sun.star.beans.PropertyValue

ThisDoc=ThisComponent

If ThisDoc.hasLocation=False Then
MsgBox (MsgSaveDocument, 16, MsgAttn) :End
End If
ThisDocURL=ThisDoc.getURL()

If ThisDoc.isModified Then
ThisDoc.storeAsURL(ThisDocURL, Args)
End if

ThisDocURL=ThisDoc.getURL()

PathSubstService = CreateUnoService("com.sun.star.util.PathSubstitution")
HomeDir = ConvertFromURL(PathSubstService.substituteVariables("$(home)", true))

If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
GlobalScope.BasicLibraries.LoadLibrary("Tools")
End If

FileName=Dir(ThisDocURL, 0)

If GetGuiType()=1 Then

DocPath=ConvertFromURL(ThisDocURL)
Shell("C:\Python25\python.exe", 1, "C:\Python25\gmail_backup.py " & """" & DocPath & """" & """" & FileName & """")

Else

DocDir=DirectoryNameoutofPath(ThisDocURL, "/")
DocPath=ConvertFromURL(DocDir)

Shell("python", 1, HomeDir & "/gmail_backup.py " & DocPath & "/" & FileName & " " & FileName)
End If

End Sub


I realize that OpenOffice.org comes with its own version of Python and the Python-UNO bridge. I know nothing about it and how it works, and I wonder whether it's possible to adapt the script to work with Python-UNO instead of relying on an "external" Python installation and the OOo Basic macro?
User avatar
Lazy-legs
 
Posts: 71
Joined: Mon Oct 08, 2007 1:33 am
Location: Århus-Berlin

Re: Python and OpenOffice.org Basic

Postby ccornell » Thu Aug 28, 2008 4:25 pm

Lazy-legs wrote: I realize that OpenOffice.org comes with its own version of Python and the Python-UNO bridge. I know nothing about it and how it works, and I wonder whether it's possible to adapt the script to work with Python-UNO instead of relying on an "external" Python installation and the OOo Basic macro?


Probably the best place to dig up info on the PyUNO thing is on the PyUNO Wiki pages, and a combination of the BASIC Guide and the Developer's Guide.

http://wiki.services.openoffice.org/wiki/Python
http://wiki.services.openoffice.org/wiki/Documentation/BASIC_Guide
http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/OpenOffice.org_Developers_Guide
openSUSE 11.4, KDE4.6 with OpenOffice.org 3.3
User avatar
ccornell
Volunteer
 
Posts: 611
Joined: Sun Oct 07, 2007 7:21 am

Re: Python and OpenOffice.org Basic

Postby Lazy-legs » Thu Aug 28, 2008 4:29 pm

Thanks for the pointers. Actually, I was hoping that someone would do the heavy lifting for me and provide a working example. :D

Kind regards,
Dmitri
User avatar
Lazy-legs
 
Posts: 71
Joined: Mon Oct 08, 2007 1:33 am
Location: Århus-Berlin


Return to Macros and UNO API

Who is online

Users browsing this forum: floris v, Zizi64 and 6 guests