python实现 Lotus Notes 取附件

本文介绍了如何使用Python调用Notescom组件从LotusNotesnsf数据库中获取所有附件的UID和名称,并讨论了与C++版本相比,Python版本在安全性上的局限性。
摘要由CSDN通过智能技术生成

python实现 Lotus Notes 取附件

​ 使用python 调用 notes com组件实现保存一个nsf数据库中所有的附件的uid和名字,并按需提取需要的附件。

​ 需要本地配置好 Lotus Notes 环境,需要 本地用户密码。

​ 因为python版本需要密码,实用性局限,所以用C++实现了不需要密码,检索条件更加全面的版本,python版本就当对照组使用了。

# -*-coding:utf-8 -*-

import argparse
import os
import noteslib
import json

def getFilePath():
    # 获取当前文件夹路径
    current_folder = os.getcwd()
    # 创建 "attach" 文件夹
    attach_folder = os.path.join(current_folder, 'attach')
    if not os.path.exists(attach_folder):
        os.makedirs(attach_folder)
    # 获取attach文件夹完整路径
    attach_folder_path = os.path.abspath(attach_folder)
    print("attach文件夹路径:", attach_folder_path)
    return attach_folder_path

def download(password,filePath,download_filename):
    handle = noteslib.Session(password).my_handle
    db = noteslib.Database("", filePath)

    print(db.Created)
    print(db.title, db.size)
    print(f'session={handle}')

    with open(download_filename, "r",encoding="utf-8") as file:
        data = file.read()
    data = json.loads(data)
    attach_path = getFilePath()
    for key,val in data.items():
        print(key,val)
        tmp = db.GetDocumentByUNID(key)
        obj = tmp.GetAttachment(val)
        print(f'objName={obj.Name}   type={obj.Type} {type(obj.Name)}')
        filePath_ = os.path.join(attach_path, str(obj.Name))
        obj.Extractfile(filePath_)
        
    pass

def run(password,filePath):

    handle=noteslib.Session(password).my_handle
    db=noteslib.Database("", filePath )

    print(db.Created)
    print(db.title, db.size)
    print(f'session={handle}')
    docCollection = db.AllDocuments

    doc = docCollection.GetFirstDocument()


    infofile = filePath.replace('.','_')+"_info.txt"  # 将 "文件路径" 替换为实际的文件路径
    file = open(infofile, "w",encoding="utf-8")

    while not doc is None:
        # print(f'doc={doc}  Subject={doc.GetItemValue("From")}  size={doc.Size} ')
        if doc.size:
            unid = doc.UniversalID
        else:
            doc = docCollection.GetNextDocument(doc)
            # print(f'doc={doc}')
            continue
        tmp=db.GetDocumentByUNID(unid)

        names = handle.Evaluate("@AttachmentNames", tmp)
        if len(names)>0 and names[0]!='':
            print(f'len={len(names)}')
            
            print(f'names={names}')
            file.write(f'{str(unid)}:\n')
            file.write(f'      {names}\n')
        doc = docCollection.GetNextDocument(doc)

    file.close()




if __name__ == '__main__':
    # 创建ArgumentParser对象
    parser = argparse.ArgumentParser(description='example')
    # 添加命令行参数规则
    parser.add_argument('-p', '--password', required=True, help='password')
    parser.add_argument('-f', '--filename', required=True, help='filename')
    parser.add_argument('-d', '--download', help='download')
    parser.add_argument('-H', '--show_help', action='store_true', help='帮助信息')
    args = parser.parse_args()
    if args.show_help:
        parser.print_help()
        exit()
    password = args.password
    filePath = args.filename
    if args.download:
        download_filename = args.download
        download(password=password,filePath=filePath,download_filename=download_filename)
    else:
        run(password=password,filePath=filePath)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值