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)