我試圖使用Python將PDF上傳到OneNote。根據OneNote API,我需要發佈這樣的請求:如何使用Python將多部分PDF請求發送到OneNote
Content-Type:multipart/form-data; boundary=MyAppPartBoundary
Authorization:bearer tokenString
--MyAppPartBoundary
Content-Disposition:form-data; name="Presentation"
Content-type:text/html
<!DOCTYPE html>
<html>
<head>
<title>A page with an embedded and displayed PDF file</title>
</head>
<body>
<p>Attached is the lease agreement for the expanded offices!</p>
<object
data-attachment="OfficeLease.pdf"
data="name:OfficeLeasePartName"
type="application/pdf" />
<p>Here's the contents of our new lease.</p>
<img data-render-src="name:OfficeLeasePartName" width="900"/>
</body>
</html>
--MyAppPartBoundary
Content-Disposition:form-data; name="OfficeLeasePartName"
Content-type:application/pdf
... PDF binary data ...
--MyAppPartBoundary--
但是,我不知道如何在Python中執行多部分請求。我可以做一個基本的文本/ HTML請求就好了:
url = ROOT_URL+"pages"
headers = {"Content-Type":"text/html",
"Authorization" : "bearer " + access_token}
# Format html (title & text)
html = "<html><head><title>" + title + "</title></head>"
html += "<body><p>" + text + "</p></body></html>"
# Send request
session = requests.Session()
request = requests.Request(method="POST", headers=headers,
url=url, data=html)
prepped = request.prepare()
response = session.send(prepped)
我該如何修改多部分的Python代碼?
[########### UPDATE ############]
基於jayongg的建議下,我嘗試以下。當我這樣做時,我從「頁面創建請求」中切換的錯誤需要內容爲多部分,並將「演示文稿部分」改爲「多部分有效內容格式錯誤」。我認爲這是因爲我實際上並沒有將pdf文件附加到某處?我也不確定OneNote api示例中的OfficeLease.pdf和OfficeLeasePartName之間的區別。
這是我的當前代碼:
url = ROOT_URL+"pages"
path = os.path.join(pdfFolder, pdfName + ".pdf")
headers = {"Content-Type":"multipart/form-data; boundary=MyAppPartBoundary",
"Authorization" : "bearer " + access_token}
f = open(path, "rb").read()
txt = """--MyAppPartBoundary
Content-Disposition:form-data; name="Presentation"
Content-type:text/html
<!DOCTYPE html>
<html>
<head>
<title>A page with an embedded and displayed PDF file</title>
</head>
<body>
<p>Attached is the lease agreement for the expanded offices!</p>
<object
data-attachment="Sample5.pdf"
data="name:Sample5"
type="application/pdf" />
<p>Here's the contents of our new lease.</p>
<img data-render-src="name:Sample5" width="900"/>
</body>
</html>
--MyAppPartBoundary
Content-Disposition:form-data; name="OfficeLeasePartName"
Content-type:application/pdf
""" + f + """
--MyAppPartBoundary--"""
session = requests.Session()
request = requests.Request(method="POST", headers=headers,
url=url, data=txt)
prepped = request.prepare()
response = session.send(prepped)
[########## UPDATE 2 ##############]
如果我使代碼更簡單,它仍然導致格式錯誤:
headers = {"Content-Type":"multipart/form-data; boundary=MyAppPartBoundary",
"Authorization" : "bearer " + access_token}
txt = """--MyAppPartBoundary
Content-Disposition:form-data; name="Presentation"
Content-type:text/html
<!DOCTYPE html>
<html>
<head>
<title>One Note Text</title>
</head>
<body>
<p>Hello OneNote World</p>
</body>
</html>
--MyAppPartBoundary--
"""
session = requests.Session()
request = requests.Request(method="POST", headers=headers,
url=url, data=txt)
我也試過這樣。同樣的事情:
headers = {"Content-Type":"multipart/form-data; boundary=MyAppPartBoundary",
"Authorization" : "bearer " + access_token}
txt = """<!DOCTYPE html>
<html>
<head>
<title>One Note Text</title>
</head>
<body>
<p>Hello OneNote World</p>
</body>
</html>"""
files = {'file1': ('Presentation', txt, 'text/html')}
session = requests.Session()
request = requests.Request(method="POST", headers=headers,
url=url, files=files)
prepped = request.prepare()
response = session.send(prepped)
嘗試用最後--MyAppPartBoundary-- – jayongg
@jayongg我做了後加入一個換行符,但它仍然給出同樣的錯誤。即使我使代碼更簡單,它也會給出錯誤的錯誤(參見上面的update2)。 – Elliptica