gpt4 book ai didi

python - 如何使用 Python 向 OneNote 发送多部分 PDF 请求

转载 作者:可可西里 更新时间:2023-11-01 17:07:25 24 4
gpt4 key购买 nike

我正在尝试使用 Python 将 PDF 上传到 OneNote。根据 OneNote API,我需要发布这样的请求:

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 代码?

[###########更新############]

根据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)

[########## 更新 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)

最佳答案

事实证明,Python 将空行编码为“\n”,但 OneNote 需要“\r\n”。它还需要在最终边界之后有一个空行(“\r\n”)。最后,对于 Content-Type 和 Content-Disposition 行,正文中不能有任何前导空格(无缩进)。 (每个 Content-Disposition 行之后也应该有一个空行。)

例如,如果这是正文:

"""--MyBoundary
Content-Type: text/html
Content-Disposition: form-data; name="Presentation"

Some random text
--MyBoundary
Content-Type: text/text
Content-Disposition: form-data; name="more"; filename="more.txt"

More text
--MyBoundary--
"""

应该用字符串表示

'--MyBoundary\r\nContent-Type: text/html\r\nContent-Disposition: form-data; name="Presentation"\r\n\r\nSome random text\r\n--MyBoundary\r\nContent-Type: text/text\r\nContent-Disposition: form-data; name="more"; filename="more.txt"\r\n\r\nMore text\r\n--MyBoundary--\r\n' 

只需在三个“””引号内键入文本(这将在最后一个字符串中有空行的地方自动创建\n),然后将“\n”替换为“\r\n” :

body = body.replace("\n", "\r\n")

header 将是:

headers = {"Content-Type":"multipart/form-data; boundary=MyBoundary",
"Authorization" : "bearer " + access_token}

最后,您将像这样发布调用:

session = requests.Session()
request = requests.Request(method="POST", headers=headers,
url=url, data=body)
prepped = request.prepare()
response = session.send(prepped)

关于python - 如何使用 Python 向 OneNote 发送多部分 PDF 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36050269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com