首页 > 解决方案 > 将 pandas DataFrame 转换为 MS Graph APi 的字节

问题描述

我使用 MS Graph Api 将数据框作为电子邮件附件发送..下面的文档

https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http。.

附件需要在contentBytes现场发送

所以使用这个功能:

def convertToBytesString(mmcFile):
    mmcFileBytes = pickle.dumps(mmcFile.to_csv(index = False), protocol= 4)
    mmcFileBytes = base64.b64encode(mmcFileBytes).decode()
    return mmcFileBytes

然后我将返回mmcFileBytes的数据传递给 MS 图表,例如:

{
  "message": {
    "subject": "Meet for lunch?",
    "body": {
      "contentType": "Text",
      "content": "The new cafeteria is open."
    },
    "toRecipients": [
      {
        "emailAddress": {
          "address": "test@gmail.com"
        }
      }
    ],
    "attachments": [
      {
        "@odata.type": "#microsoft.graph.fileAttachment",
        "name": "attachment.csv",
        "contentType": "text/plain",
        "contentBytes": mmcFileBytes
      }
    ]
  }
}

.csv电子邮件按预期发送附件,但我在or 单元格的第一行有奇怪的字符A1€X£ÿMetavehicle Id我只是想要Metavehicle Id

如果我print()在上面的函数中添加一个...

def convertToBytesString(mmcFile):
    mmcFileBytes = pickle.dumps(mmcFile.to_csv(index = False), protocol= 4)
    print(mmcFileBytes)
    mmcFileBytes = base64.b64encode(mmcFileBytes).decode()
    return mmcFileBytes

返回的长字符串的第一部分是b'\x80\x04X\xa3\xff\x01\x00Metavehicle Id 所以我只能假设这\x80\x04X\xa3\xff\x01\x00是问题所在。

我的功能是否有任何遗漏可能导致这种情况?

标签: python

解决方案


推荐阅读