我有一个本地文件default.xlsx。
我把它寄给https://v2.convertapi.com。
我从响应中获取内容,并尝试将其保存在default.pdf中。
我没有错误信息。
当我试图打开创建的PDF时,它看起来已经损坏了。
import requests
import base64
f = open("default.xlsx", "rb").read()
f = base64.b64encode(f)
headers = {'Content-Type': 'application/json'}
payload = {
    "Parameters": [
        {
            "Name": "File",
            "FileValue": {
                "Name": "default.xlsx",
                "Data": f
            }
        },
    ]
}
r = requests.post('https://v2.convertapi.com/convert/xlsx/to/pdf?secret=SECRET-KEY', headers=headers, json=payload)
open('default.pdf', 'wb').write(r.content)

发布于 2022-01-27 09:10:26
SECRET = "you can find yours in https://www.convertapi.com/"
def xlsx2pdf(xlsx_path):
    import requests
    import base64
    import json
    f = open(xlsx_path, "rb").read()
    f = base64.b64encode(f)
    headers = {'Content-Type': 'application/json'}
    payload = {
        "Parameters": [
            {
                "Name": "File",
                "FileValue": {
                    "Name": xlsx_path,
                    "Data": f
                }
            },
        ]
    }
    print("f", type(f))
    print("sending request...")
    r = requests.post('https://v2.convertapi.com/convert/xlsx/to/pdf?secret='+SECRET, headers=headers, json=payload)
    file_data = r.content
    file_dict = json.loads(file_data)
    data = file_dict["Files"][0]["FileData"]
    b64data = base64.b64decode(data)
    open(xlsx_path.replace(".xlsx",".pdf"), 'wb').write(b64data)
xlsx2pdf("default.xlsx")发布于 2022-01-26 14:46:18
收敛to有一个python模块来完成这个任务,而不是尝试使用.json语法。这方面的一个例子可以找到这里
https://stackoverflow.com/questions/70864933
复制相似问题