web-dev-qa-db-ja.com

Python

Outlookには、複数の月のビューを表示するなど、いくつかの要望があります。

pythonを介してイベントデータを引き出すことで試してみることにしました(そして、それをうまく表示する方法を考え出します)。Googleは気孔の結果を提供していますが、stackoverflowは非常に役に立ちました以前はwin32comとOutlookの使用に関して。

私の目標は次のとおりです

  • 共有カレンダーを読む
  • 開始、終了、件名、作成者などのイベント情報を読み取る

私は遠くまで行っていませんが、これは私が一緒にしたものです( このサイト からのインスピレーションで)

import win32com.client, datetime
from dateutil.relativedelta import relativedelta

Outlook = win32com.client.Dispatch("Outlook.Application")
ns = Outlook.GetNamespace("MAPI")

appointments = namespace.GetDefaultFolder(9).Items 
# TODO: Need to figure out howto get the shared calendar instead Default [9] 
# (I have placed the shared folder into a separate folder - don't know if it matters)
# I would just like the user to select which calendar to execute on
appointments.Sort("[Start]")
appointments.IncludeRecurrences = "True"
begin = date.today().strftime("%m%d%Y")
end = (date.today() + relativedelta( months = 3 )).strftime("%m%d%Y")
appointments = appointments.Restrict("[Start] >= '" +begin+ "' AND [END] >= '" +end+ "'")

ここから、イベントのループ処理とそれらの読み取りについてサポートが必要です。どんな助けでも大歓迎です。

16
Norfeldt

ここから、イベントのループ処理と読み取りについてサポートが必要です。

基本的には、MicrosoftのCOM APIドキュメントに従うだけです。たとえば、Restrict()メソッドは、Outlook 2010の AppointmentItem Object に記載されているAppointmentItemオブジェクトを返します。そのため、フォルダーから始めて、次のように予定:

# Get the AppointmentItem objects
# http://msdn.Microsoft.com/en-us/library/office/aa210899(v=office.11).aspx
appointments = someFolder.Items

# Restrict to items in the next 30 days (using Python 3.3 - might be slightly different for 2.7)
begin = datetime.date.today()
end = begin + datetime.timedelta(days = 30);
restriction = "[Start] >= '" + begin.strftime("%m/%d/%Y") + "' AND [End] <= '" +end.strftime("%m/%d/%Y") + "'"
restrictedItems = appointments.Restrict(restriction)

# Iterate through restricted AppointmentItems and print them
for appointmentItem in restrictedItems:
    print("{0} Start: {1}, End: {2}, Organizer: {3}".format(
          appointmentItem.Subject, appointmentItem.Start, 
          appointmentItem.End, appointmentItem.Organizer))

制限式には少し異なる時間形式を使用する必要があることに注意してください("%m/%d/%Y"ではなく"%m%d%Y")。適切な解決策は、 http://msdn.Microsoft.com/en-us/library/office/ff869597(v = office.14).aspxに記載されているOutlookのFormat関数を使用することです。 、セクションDateまた、Python 3.3を使用していたため、日付を作成するために別の関数を使用する必要がある場合があります。いずれの場合でも、テストの目的で、"[Start] >= '02/03/2014' AND [End] <= '03/05/2014'"

共有カレンダーを取得するには、次のコードが機能する必要があります-これはAPIドキュメントにある通常のシーケンスですが、実際に機能させることはできませんでしたが、(Exchangeサーバーを使用していない)別のバックエンドサーバーが原因である可能性があります):

recipient = namespace.createRecipient("User Name")
resolved = recipient.Resolve()
sharedCalendar = namespace.GetSharedDefaultFolder(recipient, 9)

使用可能なすべてのフォルダをツリーとして表示するには、次のようなものを使用できます

def folderTree(folders, indent = 0):
    prefix = ' ' * (indent*2)
    i = 0
    for folder in folders:
        print("{0}{1}. {2} ({3})".format(prefix, i, folder.Name, folder.DefaultItemType))
        folderTree(folder.Folders, indent + 1)
        i = i + 1

...
folderTree(namespace.Folders)

パスでフォルダーを検索するには(たとえば、「インターネットカレンダー」フォルダーの下にあるカレンダーフォルダー「[email protected]」を見つけるには)、次のようにします。

def findFolder(folders, searchPath, level = 0):
    for folder in folders:
        if folder.Name == searchPath[level]:
            if level < len(searchPath)-1:
                # Search sub folder
                folder = findFolder(folder.folders, searchPath, level+1)
            return folder
    return None

...
sharedCalendar = findFolder(namespace.Folders, ["Internet Calendars", "[email protected]"])

以下も参照してください。

20
Andreas Fester