0

I have a local file default.xlsx.

I send it to https://v2.convertapi.com.

I take the content from the response and I try to save it in a default.pdf.

I have no error message.

When I try to open the created PDF, it looks corrupted.

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)

bug

Utopion
  • 935
  • 1
  • 4
  • 15

2 Answers2

1
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")
Utopion
  • 935
  • 1
  • 4
  • 15
0

convertapi has a python module for doing this instead of trying to use the .json syntax. An example of this can be found here

some1and2
  • 41
  • 3
  • I saw the documentation but I need to try the https way, to make it more general and without the need of the library import (I'm working on cloud env so the less lib, the better). – Utopion Jan 26 '22 at 14:51