Python and Lotus Notes

  • Thread starter =?ISO-8859-2?Q?Grzegorz_=A6lusarek?=
  • Start date
?

=?ISO-8859-2?Q?Grzegorz_=A6lusarek?=

Hello everyone. I have to get data from Lotus Notes and i curious is it
possible doing it with Python. I heard that Lotus Notes using COM, so
the Python does so maybe it can be done? Anyone have any experiences
doing that?
Ane help will by apreciated
Gregor
 
G

Graham Fawcett

Grzegorz said:
Hello everyone. I have to get data from Lotus Notes and i curious is it
possible doing it with Python. I heard that Lotus Notes using COM, so
the Python does so maybe it can be done? Anyone have any experiences
doing that?
Ane help will by apreciated

Yes, it's pretty simple. Quick example:

from win32com.client import Dispatch
session = Dispatch('Lotus.NotesSession')
session.Initialize(MY_NOTES_PASSWORD)

db = session.getDatabase(SERVER_NAME, DB_NAME)
...

The LotusScript reference guide is almost completely applicable to
Notes via COM.

It can be useful to define some helper functions to make working with
Notes a bit easier. For example, this is the (very clunky) way to
iterate all documents in a Notes database, without helper code:

view = db.getView('Important Docs')
doc = db.getFirstDocument()
while doc:
do_something_with(doc)
doc = view.getNextDocument(doc)

You probably recognize the pattern from any LotusScript you've written.
(It sure is easy to forget that last line...)

But Python gives you far better control structures, like generators.
It's worthwhile to define a few helper functions like this, in a common
module:

def iterateDocuments(docs):
doc = docs.getFirstDocument()
while doc:
yield doc
doc = docs.getNextDocument(doc)

Then you can write much cleaner code, like this:

for doc in iterateDocuments(view):
do_something_with(doc)

Similarly you can write iterator functions for traversing databases,
ACL entries, etc.

I also find these two defnitions useful:

def get(doc, attr):
return doc.getItemValue(attr)

def get1(doc, attr):
return get(doc, attr)[0]

because it's a lot easier to write:

user_name = get1(user_doc, 'FullName')

than:

user_name = user_doc.getItemValue('FullName')[0]

Note that the attribute-access style that LotusScript allows, e.g.
"user_doc.FullName(0)" does not work in Python/COM, hence you'll need
getItemValue(), replaceItemValue(), etc.. You could write a wrapper for
the NotesDocument class that would give you attributes like this.
Personally, I find that to be more trouble than it's worth, but your
needs be very different.

Last warning: once you've tried using Python and COM, you'll scream
whenever you have to write LotusScript, and you'll wish like crazy that
Notes/Domino supported Python as a native scripting language. :)
Jython kind of works, but I never got it happily running for
server-side code, alas.

Graham
 
G

Guest

thank you Graham
Now I know how to get it thru
And i have last question is it possible send mail from Lotus via
Python/COM?
Once Again Thanks
 
M

Marco Aschwanden

The second line of your code is already a show stopper in my case:

from win32com.client import Dispatch
session = Dispatch('Lotus.NotesSession')
session.Initialize('my_secret_passwort')

When started, ends:

File
"C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\temp\notes_init.py", line 3, in ?
session.Initialize('my_secret_passwort')
File "c:\Python24\lib\site-packages\win32com\client\dynamic.py", line
489, in __getattr__
raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: Lotus.NotesSession.Initialize

It worked before though with Version 5.x of Notes. In Notes Version 6.X
they introduced the session.Initialize() - that was the point, when I
couldn't create an instance anymore. I found no hint on the net... Do you
have any idea what is going wrong here?

Regards,
Marco
 
G

Graham Fawcett

Marco said:
The second line of your code is already a show stopper in my case:

from win32com.client import Dispatch
session = Dispatch('Lotus.NotesSession')
session.Initialize('my_secret_passwort')

When started, ends:
[snip]
AttributeError: Lotus.NotesSession.Initialize

It worked before though with Version 5.x of Notes. In Notes Version 6.X
they introduced the session.Initialize() - that was the point, when I
couldn't create an instance anymore. I found no hint on the net... Do you
have any idea what is going wrong here?

I'm using 6.x here, so that's not the problem.

Is there any chance you have an old gen_py file in your path? E.g.,
does re-running the PythonWin "makepy" utility again make a difference?
Just a thought, but something might be "hard-coded" to your earlier 5.x
COM interface, and gen_py would be a likely suspect.

Also, If you have another language that you can use to do COM, e.g.
Visual Basic (in any of the MS Office apps, or in Visual Studio),
verify that you can create a Lotus.NotesSession using those
environments as well. It might be a lower-level (non-Python-related)
problem.

Graham
 
T

thakadu

I have had success with jython using the notes.jar classes, for
example:

import lotus.domino
import java.io
import java.net
import java.lang
import java.util

lotus.notes.NotesThread.sinitThread()
S = lotus.notes.Session.newInstance();
db=S.getDatabase("server/domain","domlog.nsf")
agent=db.getAgent("DeleteOldDocs")
for x in range(50):
print x
result=agent.runOnServer()
print "res %d" % result
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top