本文介绍了如何在Python中为OneNote COM API构造类型化的常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 UpdatePageContent COM方法通过python win32com 模块。我可以做的事情,迄今为止包括获取层次结构,获得页面内容等。我甚至可以有效地操纵XML准备更改(使用MATLAB COM接口测试)。



这是我想要做的一个最小示例:

  import win32com 
from bs4 import BeautifulSoup
oneNoteApp = win32com.client.Dispatch('OneNote.Application')
pageID ='{603BD3F0-4DAB-4E5B-8E28-28CA0F2B0C83} {1} {B0}'#from GetHierarchy
content =一个NoteApp.GetPageContent(pageID)
soup = BeautifulSoup(content,'xml')
#...修改xml(更改有效并使用MATLAB测试)
oneNoteApp.UpdatePageContent(soup)问题在这里

现在最后一行是棘手的。根据COM文档, UpdatePageContent 的所有其他参数是可选的。但是,在 win32com 自动生成的绑定中似乎有一个错误。在示例中调用,得到一个错误,如

  C:\ ... \Temp\gen_py\3.4 \0EA692EE-BB50-4E3C-AEF0-356D91732725x0x1x0.py在UpdatePageContent(self,bstrPageChangesXmlIn,dateExpectedLastModified,xsSchema,force)
240 _prop_map_get_ = {
241COMAddIns:(104,2, ,0),(),COMAddIns,None),
- > 242Dummy1:(102,2,(11,0),(),Dummy1,None),
243LanguageSettings:(105,2,(9,0),()语言设置,无),
244#方法'Windows'返回类型'Windows'的对象

ValueError:astimezone()不能应用于naive datetime

因此, ExpectedDateLastModified 默认参数似乎被破坏。传递如 oneNoteApp.UpdatePageContent(content,0)的显式零会产生另一个错误。



尝试到目前为止构建一个 pywintypes.TimeType 自己,与.net中的 DateTime.minValue 常量的值相同运行时,但是缺少时区信息( astimezone()错误)。



我尝试了几种方法来添加时区信息,包括传递它在 pywintypes.TimeType 构造函数和使用数据时间(不能由生成的COM绑定自动转换,看起来)。

$另一种方法是使用显式时间戳,从xml中解析它们,但是那些使用毫秒的 datetime 模块有问题与。



如何构造一个有效的时间类型零传递到COM接口?在MATLAB中,我只能传递一个文本0。



编辑:我知道这与相关代码和几个博客文章在网络上。代码还调用onenote COM与文字0,但我不能得到的包甚至加载COM接口。



编辑2:我得到了运行并且它也有同样的问题。

解决方案

这个问题在此期间是老的,但是我想展示一个工作的解决方案,我发现,当试图acheive类似的东西。问题是,你需要一个时区感知日期为了调用 astimezone()它,这是什么API做某些原因。要创建时区感知日期,您必须首先本地化它。例如,您可以按如下所示修改代码:

  import datetime 
import pytz

...
date = pytz.utc.localize(datetime.datetime(year = 1899,month = 12,day = 30))
oneNoteApp.UpdatePageContent(soup,date)

实际的问题是一个合适的日期传递到 UpdatePageContent c $ c>。我的答案中的奇怪选择是详细。


I want to use the UpdatePageContent COM method documentation via the python win32com module. Things I can do so far include getting the hierarchy, getting page contents, etc. I can even validly manipulate the XML to prepare changes (tested using the MATLAB COM interface).

This is a minimal example of what I am trying to do:

    import win32com
    from bs4 import BeautifulSoup
    oneNoteApp = win32com.client.Dispatch('OneNote.Application')
    pageID = '{603BD3F0-4DAB-4E5B-8E28-28CA0F2B0C83}{1}{B0}' # from GetHierarchy
    content = oneNoteApp.GetPageContent(pageID)
    soup = BeautifulSoup(content, 'xml')
    # ... modify xml (changes are valid and tested using MATLAB)
    oneNoteApp.UpdatePageContent(soup) # problem is here

Now this last line is tricky. According to the COM documentation all other arguments to UpdatePageContent are optional. However there seems to be an error in the bindings that win32com auto-generates. Called as in the example i get an error like

    C:\...\Temp\gen_py\3.4\0EA692EE-BB50-4E3C-AEF0-356D91732725x0x1x0.py in UpdatePageContent(self, bstrPageChangesXmlIn, dateExpectedLastModified, xsSchema, force)
        240     _prop_map_get_ = {
        241                 "COMAddIns": (104, 2, (9, 0), (), "COMAddIns", None),
    --> 242                 "Dummy1": (102, 2, (11, 0), (), "Dummy1", None),
        243                 "LanguageSettings": (105, 2, (9, 0), (), "LanguageSettings", None),
        244                 # Method 'Windows' returns object of type 'Windows'

    ValueError: astimezone() cannot be applied to a naive datetime

So the ExpectedDateLastModified default argument seems to be broken. Passing an explicit zero like oneNoteApp.UpdatePageContent(content, 0) gives another error.

The other things I have tried so far is constructing a pywintypes.TimeType myself with the same value as the DateTime.minValue constant from the .net runtime, but that is missing timezone information (astimezone() error).

I tried several ways to add timezone information, including passing it in the pywintypes.TimeType constructor and using datetimes (which can't be converted automatically by the generated COM bindings, it seems).

Another way to go would be to use explicit timestamps, parsing them from the xml, but those use milliseconds which the datetime module has problems with.

How can I construct a valid time type zero to pass to the COM interface? In MATLAB i could just pass a literal 0.

EDIT: I am aware of this git repo with related code and several blog posts on the web. The code calls the onenote COM with a literal 0 as well, but I couldn't get the package to even load the COM interface.

EDIT 2: I got onepy running and it has the same problem.

解决方案

This question is old in the meantime, but nonetheless I want to show a working solution, which I found while trying to acheive something similar. The problem is that you need a timezone-aware date in order to call astimezone() on it, which is what the API does for some reason. To make a timezone-aware date, you have to localize it first. For example, you could modify your code as follows:

import datetime
import pytz

...
date = pytz.utc.localize(datetime.datetime(year = 1899, month = 12, day = 30))
oneNoteApp.UpdatePageContent(soup, date)

The actual problem is coming up with a suitable date to pass to to UpdatePageContent(). The weird choice in my answer is detailed here.

这篇关于如何在Python中为OneNote COM API构造类型化的常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:42