Skip to content

Microsoft outlook

OutlookLocalCalendarReader #

Bases: BaseReader

用于Windows的Outlook本地日历阅读器。 从本地Outlook日历副本中读取事件。

Source code in llama_index/readers/microsoft_outlook/base.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class OutlookLocalCalendarReader(BaseReader):
    """用于Windows的Outlook本地日历阅读器。
从本地Outlook日历副本中读取事件。"""

    def load_data(
        self,
        number_of_results: Optional[int] = 100,
        start_date: Optional[Union[str, datetime.date]] = None,
        end_date: Optional[Union[str, datetime.date]] = None,
        more_attributes: Optional[List[str]] = None,
    ) -> List[Document]:
        """从用户本地日历加载数据。

Args:
    number_of_results(可选[int]):要返回的事件数量。默认为100。
    start_date(可选[Union[str, datetime.date]]):要从中返回事件的开始日期。默认为今天。
    end_date(可选[Union[str, datetime.date]]):要从中返回事件的最后日期(包括在内)。默认为2199-01-01。
    more_attributes(可选[List[str]]):要从日历条目中检索的其他属性。不存在的属性将被忽略。

返回适合由llam_index索引的文档列表。始终返回Start、End、Subject、Location和Organizer属性,并可选择返回more_attributes参数中指定的其他属性。
"""
        if platform.system().lower() != "windows":
            return []
        attributes = [
            "Start",
            "End",
            "Subject",
            "Location",
            "Organizer",
        ]  # base attributes to return
        if more_attributes is not None:  # if the user has specified more attributes
            attributes += more_attributes
        if start_date is None:
            start_date = datetime.date.today()
        elif isinstance(start_date, str):
            start_date = datetime.date.fromisoformat(start_date)

        # Initialize the Outlook application
        winstuff = importlib.import_module("win32com.client")
        outlook = winstuff.Dispatch("Outlook.Application").GetNamespace("MAPI")

        # Get the Calendar folder
        calendar_folder = outlook.GetDefaultFolder(9)

        # Retrieve calendar items
        events = calendar_folder.Items

        if not events:
            return []
        events.Sort("[Start]")  # Sort items by start time
        numberReturned = 0
        results = []
        for event in events:
            converted_date = datetime.date(
                event.Start.year, event.Start.month, event.Start.day
            )
            if converted_date > start_date:  # if past start date
                numberReturned += 1
                eventstring = ""
                for attribute in attributes:
                    if hasattr(event, attribute):
                        eventstring += f"{attribute}: {getattr(event,attribute)}, "
                results.append(Document(text=eventstring))
            if numberReturned >= number_of_results:
                break

        return results

load_data #

load_data(
    number_of_results: Optional[int] = 100,
    start_date: Optional[Union[str, date]] = None,
    end_date: Optional[Union[str, date]] = None,
    more_attributes: Optional[List[str]] = None,
) -> List[Document]

从用户本地日历加载数据。

返回适合由llam_index索引的文档列表。始终返回Start、End、Subject、Location和Organizer属性,并可选择返回more_attributes参数中指定的其他属性。

Source code in llama_index/readers/microsoft_outlook/base.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    def load_data(
        self,
        number_of_results: Optional[int] = 100,
        start_date: Optional[Union[str, datetime.date]] = None,
        end_date: Optional[Union[str, datetime.date]] = None,
        more_attributes: Optional[List[str]] = None,
    ) -> List[Document]:
        """从用户本地日历加载数据。

Args:
    number_of_results(可选[int]):要返回的事件数量。默认为100。
    start_date(可选[Union[str, datetime.date]]):要从中返回事件的开始日期。默认为今天。
    end_date(可选[Union[str, datetime.date]]):要从中返回事件的最后日期(包括在内)。默认为2199-01-01。
    more_attributes(可选[List[str]]):要从日历条目中检索的其他属性。不存在的属性将被忽略。

返回适合由llam_index索引的文档列表。始终返回Start、End、Subject、Location和Organizer属性,并可选择返回more_attributes参数中指定的其他属性。
"""
        if platform.system().lower() != "windows":
            return []
        attributes = [
            "Start",
            "End",
            "Subject",
            "Location",
            "Organizer",
        ]  # base attributes to return
        if more_attributes is not None:  # if the user has specified more attributes
            attributes += more_attributes
        if start_date is None:
            start_date = datetime.date.today()
        elif isinstance(start_date, str):
            start_date = datetime.date.fromisoformat(start_date)

        # Initialize the Outlook application
        winstuff = importlib.import_module("win32com.client")
        outlook = winstuff.Dispatch("Outlook.Application").GetNamespace("MAPI")

        # Get the Calendar folder
        calendar_folder = outlook.GetDefaultFolder(9)

        # Retrieve calendar items
        events = calendar_folder.Items

        if not events:
            return []
        events.Sort("[Start]")  # Sort items by start time
        numberReturned = 0
        results = []
        for event in events:
            converted_date = datetime.date(
                event.Start.year, event.Start.month, event.Start.day
            )
            if converted_date > start_date:  # if past start date
                numberReturned += 1
                eventstring = ""
                for attribute in attributes:
                    if hasattr(event, attribute):
                        eventstring += f"{attribute}: {getattr(event,attribute)}, "
                results.append(Document(text=eventstring))
            if numberReturned >= number_of_results:
                break

        return results