我正在使用Outlook API,并从特定电子邮件中请求附件时得到了以下响应:
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('myname%40hotmail.com.au')/messages('AQMkADAwATMwMAItYTZiNi05ZDgxLTAwAi0wMAoAtytTDBXFHqNdTw8vNERkHALm9xdAw4PxPkb4h1HfN4XIAAAIBDAAAALm9xdAw4PxPkb4h1HfN4XIAAmBXCV8AAAA%3D')/attachments",
"value": [
{
"@odata.type": "#microsoft.graph.fileAttachment",
"id": "AQMkADAwATMwMAItYTZiNi05ZDgxLTAwAi0wMAoARgAAAyyLyoTDBXFHqNdTw8vNERkHALm9xdAw4PxPkb4h1HfN4XIAAAIBDAAAALm9xdAw4PxPkb4h1HfN4XIAAmBXCV8AAAABEgAQAIqjEp9FM2tKv4ahCWsIb7w=",
"lastModifiedDateTime": "2019-05-01T11:58:33Z",
"name": "Testfile.pdf",
"contentType": "application/pdf",
"size": 1013696,
"isInline": False,
"contentId": "ffeefe77-9bab-4736-83a3-79d921d3907b@icloud.com",
"contentLocation": None,
"contentBytes": "JVBERi0xLjYKJeLj...
我只显示了contentBytes
值的一小部分,因为它太长了,但它是我感兴趣的那一部分。
我尝试使用base64模块解码contentBytes
值并得到了它(仅显示结果的一部分)
%PDF-1.6\n%âãÏÓ\n68 0 obj<</Length 1121/Filter/FlateDecode>>stream\nH\x89¬WÛnÛ8\x10}×WÌÛÊ\x0f¡y\x11%1\x08\x0cÄn\x12´@Òl¬E±\x08\x8a\x85bÓ±x12Ý*SÉöïw(É\x89ÝjÓ@.\x0c[\x129\x9a3sfxH\x8fgs\n³9Ðæ\x03óÙ\x95C\x89\x84gðà\x12\x9cñ\x05ÎÞo\x9coÀ\x9ay\x06\x8c\x86ÄçL\x02c\x8a\x84\x8a+Xd\x8e\x9dÊ\x1c¡|¼¦ÎÜùÓ\x19[·\x8b\x8d}\x01?°YäÖ`....
如何查看pdf的实际内容?
答案 0 :(得分:1)
以下代码将写入文件。您可以使用pdf阅读器打开文件
import json
jsondata=json.loads(outlook-response)
f = open(jsondata['name'], 'w+b')
f.write(base64.b64decode(jsondata['contentbytes']))
f.close()