Python and Lotus Notes

Graham Fawcett graham.fawcett at gmail.com
Wed Nov 2 09:24:29 EST 2005


Grzegorz Slusarek wrote:
> 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




More information about the Python-list mailing list