有没有办法通过代码与 OneNote 对话?

发布于 2024-07-26 03:44:39 字数 214 浏览 7 评论 0原文

我喜欢使用 OneNote,但我希望更好地控制笔记的位置以及笔记的生成方式。 我非常精通 VBA,也可以使用 Python(这些是我可以在我的机器上编写代码的唯一语言),但是我还没有找到通过代码与 OneNote 交互的好方法。 我使用的是 OneNote 2003,它看起来不像 2007 那样具有 xml 导出功能。 有人找到了一种使用 Python 或 VBA 读写 OneNote 文件的简单方法吗?

I love using OneNote, however I want more control over the locations of my notes and how notes are generated. I'm very versed in VBA and ok with Python (and those are the only languages I can code with on my machine) however I haven't found a decent way to interact with OneNote through code. I'm using OneNote 2003 which doesn't look like it has an xml export like 2007 does. Has anyone figured out an easy way to be able to read and write OneNote files with Python or VBA?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

澜川若宁 2024-08-02 03:44:39

OneNote 2007 有更多选项,但 OneNote 2003 仅支持CSimpleImporterClass API,用于以编程方式将数据导入 OneNote。 CSimpleImporterClass 允许您使用 VB 将图像、墨迹和 HTML 引入 OneNote。

读取 OneNote 2003 文件可能有点困难。 我不知道有什么资源可以让您轻松使用 Python 或 VBA 准备 OneNote 2003 文件。

OneNote 2007 has more options, but OneNote 2003 only supports the CSimpleImporterClass API for importing data programmatically into OneNote. CSimpleImporterClass will let you bring in images, ink, and HTML into OneNote using VB.

Reading OneNote 2003 files might be a little harder. I don't know of any resources to make it easy to ready OneNote 2003 files with Python or VBA.

我一向站在原地 2024-08-02 03:44:39

对于 OneNote 笔记本、分区或页面上的基本 CRUD(创建、读取、更新、删除)操作,您还可以使用 REST API< /a> 来自 Python。

For basic CRUD (Create, Read, Update, Delete) operations on OneNote notebooks, sections or pages, you can also use the REST API from Python.

寄意 2024-08-02 03:44:39

这是来自 这个 Microsoft 示例。 该代码需要进行一些更正才能使其与 Microsoft 365 保持同步,这些更正显示在注释中、注释掉的原始行及其下面的新行中。 该代码托管在 Excel 中。

OneNote 版本 - Microsoft® OneNote® for Microsoft 365 MSO(版本 2406 内部版本 16.0.17726.20078)64 位

Option Explicit

' https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2010/hh377183(v=office.14)

Sub sbCreateNewPage()
    ' Connect to OneNote 2010.
    ' To see the results of the code,
    ' you'll want to ensure the OneNote 2010 user
    ' interface is visible.
'    Dim oneNote As oneNote14.Application
    Dim oneNote As oneNote.Application
'    Set oneNote = New oneNote14.Application
    Set oneNote = New oneNote.Application

    ' Get all of the Notebook nodes.
    Dim nodes As MSXML2.IXMLDOMNodeList
    Set nodes = GetFirstOneNoteNotebookNodes(oneNote)
    If Not nodes Is Nothing Then
        ' Get the first OneNote Notebook in the XML document.
        Dim node As MSXML2.IXMLDOMNode
        Set node = nodes(0)
        Dim noteBookName As String
        noteBookName = node.Attributes.getNamedItem("name").Text

        ' Get the ID for the Notebook so the code can retrieve
        ' the list of sections.
        Dim notebookID As String
        notebookID = node.Attributes.getNamedItem("ID").Text

        ' Load the XML for the Sections for the Notebook requested.
        Dim sectionsXml As String
'        oneNote.GetHierarchy notebookID, hsSections, sectionsXml, xs2010
        oneNote.GetHierarchy notebookID, hsSections, sectionsXml, xs2013

'        Dim secDoc As MSXML2.DOMDocument
'        Set secDoc = New MSXML2.DOMDocument
        Dim secDoc As MSXML2.DOMDocument60
        Set secDoc = New MSXML2.DOMDocument60

        If secDoc.LoadXML(sectionsXml) Then
            ' select the Section nodes
            Dim secNodes As MSXML2.IXMLDOMNodeList
            
            secDoc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
            ' change from the original to define the one: abbreviation
            
            Set secNodes = secDoc.DocumentElement.SelectNodes("//one:Section")

            If Not secNodes Is Nothing Then
                ' Get the first section.
                Dim secNode As MSXML2.IXMLDOMNode
                Set secNode = secNodes(0)

                Dim sectionName As String
                sectionName = secNode.Attributes.getNamedItem("name").Text
                Dim sectionID As String
                sectionID = secNode.Attributes.getNamedItem("ID").Text

                ' Create a new blank Page in the first Section
                ' using the default format.
                Dim newPageID As String
                oneNote.CreateNewPage sectionID, newPageID, npsDefault

                ' Get the contents of the page.
                Dim outXML As String
'                oneNote.GetPageContent newPageID, outXML, piAll, xs2010
                oneNote.GetPageContent newPageID, outXML, piAll, xs2013

                Dim doc As MSXML2.DOMDocument60
                Set doc = New MSXML2.DOMDocument60
                ' Load Page's XML into a MSXML2.DOMDocument object.
                If doc.LoadXML(outXML) Then
                    ' Get Page Node.
                    Dim pageNode As MSXML2.IXMLDOMNode
                    
                    doc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
                    ' change from the original to define the one: abbreviation
                    
                    Set pageNode = doc.SelectSingleNode("//one:Page")

                    ' Find the Title element.
                    Dim titleNode As MSXML2.IXMLDOMNode
                    Set titleNode = doc.SelectSingleNode("//one:Page/one:Title/one:OE/one:T")

                    ' Get the CDataSection where OneNote store's the Title's text.
                    Dim cdataChild As MSXML2.IXMLDOMNode
                    Set cdataChild = titleNode.SelectSingleNode("text()")

                    ' Change the title in the local XML copy.
                    cdataChild.Text = "A Page Created from VBA" & Format(Now(), " - YYYYMMDD HH:MM")
                    ' Write the update to OneNote.
                    oneNote.UpdatePageContent doc.XML

                    Dim newElement As MSXML2.IXMLDOMElement
                    Dim newNode As MSXML2.IXMLDOMNode

                    ' Create Outline node.
                    Set newElement = doc.createElement("one:Outline")
                    Set newNode = pageNode.appendChild(newElement)
                    ' Create OEChildren.
                    Set newElement = doc.createElement("one:OEChildren")
                    Set newNode = newNode.appendChild(newElement)
                    ' Create OE.
                    Set newElement = doc.createElement("one:OE")
                    Set newNode = newNode.appendChild(newElement)
                    ' Create TE.
                    Set newElement = doc.createElement("one:T")
                    Set newNode = newNode.appendChild(newElement)

                    ' Add the text for the Page's content.
                    Dim cd As MSXML2.IXMLDOMCDATASection
                    Set cd = doc.createCDATASection("Text added to a new OneNote page via VBA." & Format(Now(), " - YYYYMMDD HH:MM"))

                    newNode.appendChild cd

                    ' Update OneNote with the new content.
                    oneNote.UpdatePageContent doc.XML

                    ' Print out information about the update.
                    Debug.Print "A new page was created in "
                    Debug.Print "Section " & sectionName & " in"
                    Debug.Print "Notebook " & noteBookName & "."
                    Debug.Print "Contents of new Page:"
                    Debug.Print doc.XML
                End If
            Else
                MsgBox "OneNote 2010 Section nodes not found. error 01"
            End If
        Else
            MsgBox "OneNote 2010 Section XML Data failed to load. error 02"
        End If
    Else
        MsgBox "OneNote 2010 XML Data failed to load. error 03"
    End If

End Sub

Private Function GetAttributeValueFromNode(node As MSXML2.IXMLDOMNode, attributeName As String) As String
    If node.Attributes.getNamedItem(attributeName) Is Nothing Then
        GetAttributeValueFromNode = "Not found. error 04"
    Else
        GetAttributeValueFromNode = node.Attributes.getNamedItem(attributeName).Text
    End If
End Function

'Private Function GetFirstOneNoteNotebookNodes(oneNote As oneNote14.Application) As MSXML2.IXMLDOMNodeList
Private Function GetFirstOneNoteNotebookNodes(oneNote As oneNote.Application) As MSXML2.IXMLDOMNodeList
    ' Get the XML that represents the OneNote notebooks available.
    Dim notebookXml As String
    ' OneNote fills notebookXml with an XML document providing information
    ' about what OneNote notebooks are available.
    ' You want all the data and thus are providing an empty string
    ' for the bstrStartNodeID parameter.
'    oneNote.GetHierarchy "", hsNotebooks, notebookXml, xs2010
    oneNote.GetHierarchy "", hsNotebooks, notebookXml, xs2013

    ' Use the MSXML Library to parse the XML.
'    Dim doc As MSXML2.DOMDocument
'    Set doc = New MSXML2.DOMDocument
    Dim doc As MSXML2.DOMDocument60
    Set doc = New MSXML2.DOMDocument60

    If doc.LoadXML(notebookXml) Then
                    
        doc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
        ' change from the original to define the one: abbreviation
                    
        Set GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes("//one:Notebook")
    Else
        Set GetFirstOneNoteNotebookNodes = Nothing
    End If
End Function

Here is the working code from this Microsoft example. The code needed a few corrections to bring it up to date with Microsoft 365 these are shown in the comments, the original line commented out and the new line below it. The code is hosted in Excel.

The OneNote version - Microsoft® OneNote® for Microsoft 365 MSO (Version 2406 Build 16.0.17726.20078) 64-bit

Option Explicit

' https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2010/hh377183(v=office.14)

Sub sbCreateNewPage()
    ' Connect to OneNote 2010.
    ' To see the results of the code,
    ' you'll want to ensure the OneNote 2010 user
    ' interface is visible.
'    Dim oneNote As oneNote14.Application
    Dim oneNote As oneNote.Application
'    Set oneNote = New oneNote14.Application
    Set oneNote = New oneNote.Application

    ' Get all of the Notebook nodes.
    Dim nodes As MSXML2.IXMLDOMNodeList
    Set nodes = GetFirstOneNoteNotebookNodes(oneNote)
    If Not nodes Is Nothing Then
        ' Get the first OneNote Notebook in the XML document.
        Dim node As MSXML2.IXMLDOMNode
        Set node = nodes(0)
        Dim noteBookName As String
        noteBookName = node.Attributes.getNamedItem("name").Text

        ' Get the ID for the Notebook so the code can retrieve
        ' the list of sections.
        Dim notebookID As String
        notebookID = node.Attributes.getNamedItem("ID").Text

        ' Load the XML for the Sections for the Notebook requested.
        Dim sectionsXml As String
'        oneNote.GetHierarchy notebookID, hsSections, sectionsXml, xs2010
        oneNote.GetHierarchy notebookID, hsSections, sectionsXml, xs2013

'        Dim secDoc As MSXML2.DOMDocument
'        Set secDoc = New MSXML2.DOMDocument
        Dim secDoc As MSXML2.DOMDocument60
        Set secDoc = New MSXML2.DOMDocument60

        If secDoc.LoadXML(sectionsXml) Then
            ' select the Section nodes
            Dim secNodes As MSXML2.IXMLDOMNodeList
            
            secDoc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
            ' change from the original to define the one: abbreviation
            
            Set secNodes = secDoc.DocumentElement.SelectNodes("//one:Section")

            If Not secNodes Is Nothing Then
                ' Get the first section.
                Dim secNode As MSXML2.IXMLDOMNode
                Set secNode = secNodes(0)

                Dim sectionName As String
                sectionName = secNode.Attributes.getNamedItem("name").Text
                Dim sectionID As String
                sectionID = secNode.Attributes.getNamedItem("ID").Text

                ' Create a new blank Page in the first Section
                ' using the default format.
                Dim newPageID As String
                oneNote.CreateNewPage sectionID, newPageID, npsDefault

                ' Get the contents of the page.
                Dim outXML As String
'                oneNote.GetPageContent newPageID, outXML, piAll, xs2010
                oneNote.GetPageContent newPageID, outXML, piAll, xs2013

                Dim doc As MSXML2.DOMDocument60
                Set doc = New MSXML2.DOMDocument60
                ' Load Page's XML into a MSXML2.DOMDocument object.
                If doc.LoadXML(outXML) Then
                    ' Get Page Node.
                    Dim pageNode As MSXML2.IXMLDOMNode
                    
                    doc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
                    ' change from the original to define the one: abbreviation
                    
                    Set pageNode = doc.SelectSingleNode("//one:Page")

                    ' Find the Title element.
                    Dim titleNode As MSXML2.IXMLDOMNode
                    Set titleNode = doc.SelectSingleNode("//one:Page/one:Title/one:OE/one:T")

                    ' Get the CDataSection where OneNote store's the Title's text.
                    Dim cdataChild As MSXML2.IXMLDOMNode
                    Set cdataChild = titleNode.SelectSingleNode("text()")

                    ' Change the title in the local XML copy.
                    cdataChild.Text = "A Page Created from VBA" & Format(Now(), " - YYYYMMDD HH:MM")
                    ' Write the update to OneNote.
                    oneNote.UpdatePageContent doc.XML

                    Dim newElement As MSXML2.IXMLDOMElement
                    Dim newNode As MSXML2.IXMLDOMNode

                    ' Create Outline node.
                    Set newElement = doc.createElement("one:Outline")
                    Set newNode = pageNode.appendChild(newElement)
                    ' Create OEChildren.
                    Set newElement = doc.createElement("one:OEChildren")
                    Set newNode = newNode.appendChild(newElement)
                    ' Create OE.
                    Set newElement = doc.createElement("one:OE")
                    Set newNode = newNode.appendChild(newElement)
                    ' Create TE.
                    Set newElement = doc.createElement("one:T")
                    Set newNode = newNode.appendChild(newElement)

                    ' Add the text for the Page's content.
                    Dim cd As MSXML2.IXMLDOMCDATASection
                    Set cd = doc.createCDATASection("Text added to a new OneNote page via VBA." & Format(Now(), " - YYYYMMDD HH:MM"))

                    newNode.appendChild cd

                    ' Update OneNote with the new content.
                    oneNote.UpdatePageContent doc.XML

                    ' Print out information about the update.
                    Debug.Print "A new page was created in "
                    Debug.Print "Section " & sectionName & " in"
                    Debug.Print "Notebook " & noteBookName & "."
                    Debug.Print "Contents of new Page:"
                    Debug.Print doc.XML
                End If
            Else
                MsgBox "OneNote 2010 Section nodes not found. error 01"
            End If
        Else
            MsgBox "OneNote 2010 Section XML Data failed to load. error 02"
        End If
    Else
        MsgBox "OneNote 2010 XML Data failed to load. error 03"
    End If

End Sub

Private Function GetAttributeValueFromNode(node As MSXML2.IXMLDOMNode, attributeName As String) As String
    If node.Attributes.getNamedItem(attributeName) Is Nothing Then
        GetAttributeValueFromNode = "Not found. error 04"
    Else
        GetAttributeValueFromNode = node.Attributes.getNamedItem(attributeName).Text
    End If
End Function

'Private Function GetFirstOneNoteNotebookNodes(oneNote As oneNote14.Application) As MSXML2.IXMLDOMNodeList
Private Function GetFirstOneNoteNotebookNodes(oneNote As oneNote.Application) As MSXML2.IXMLDOMNodeList
    ' Get the XML that represents the OneNote notebooks available.
    Dim notebookXml As String
    ' OneNote fills notebookXml with an XML document providing information
    ' about what OneNote notebooks are available.
    ' You want all the data and thus are providing an empty string
    ' for the bstrStartNodeID parameter.
'    oneNote.GetHierarchy "", hsNotebooks, notebookXml, xs2010
    oneNote.GetHierarchy "", hsNotebooks, notebookXml, xs2013

    ' Use the MSXML Library to parse the XML.
'    Dim doc As MSXML2.DOMDocument
'    Set doc = New MSXML2.DOMDocument
    Dim doc As MSXML2.DOMDocument60
    Set doc = New MSXML2.DOMDocument60

    If doc.LoadXML(notebookXml) Then
                    
        doc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
        ' change from the original to define the one: abbreviation
                    
        Set GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes("//one:Notebook")
    Else
        Set GetFirstOneNoteNotebookNodes = Nothing
    End If
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文