onenote使用python开发_如何在Python中为OneNote COM API构造类型化的常量?

本文介绍了一位开发者使用 Python 的 win32com 模块调用 OneNote 的 UpdatePageContent 方法时遇到的问题及解决办法。主要问题是默认的时间参数无法正确处理,导致更新页面内容失败。最终通过创建一个带有时区信息的日期对象解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值