问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/33262623/how-do-i-construct-typed-constants-for-the-onenote-com-api-in-python