ConvertAPI:Python 2.7.10中PDF为JPEG-如何获取转换结果URL?

时间:2019-01-18 16:19:43

标签: python json zapier convertapi

我对Python(和一般的编码)非常了解,所以如果我很笨,请原谅。

我正在为自定义Zapier步骤编写一个简短的脚本,该脚本应遍历URL列表,选择以.pdf结尾的URL,然后将其发送到ConvertAPI以便转换为JPG。

到目前为止,将请求发送到ConvertAPI一直有效,并且ConvertAPI表示测试文件已转换。我的问题是:如何获取转换后的文件的最终URL?如果我打印响应,则得到Response [200],但没有其他可用的东西。

我尝试打开Async参数,但到目前为止没有任何效果。据我了解,StoreFile必须设置为true,但这似乎没有什么不同。

import requests
import json

url = 'https://v2.convertapi.com/convert/pdf/to/jpg?Secret=******' # Hidden
headers = {'content-type': 'application/json'}
payload = {
    'Parameters': [
        {
            'Name': 'File',
            'FileValue': {
                'Url': 'to be populated'
            }
        },
        {
            'Name': 'StoreFile',
            'Value': 'true'
        }
    ]
}

a = ['https://www.bachmann.com/fileadmin/02_Produkte/03_Anschlussfelder/CONI/Downloads/CONI_3-4-6-way_Mounting_instructions_REV05.pdf','test2.jpg','test3.jpeg','test4.png','test4.exe']

for x in a:

  if x[-3:] == 'pdf':
    payload['Parameters'][0]['FileValue']['Url'] = x
    response = requests.post(url, data=json.dumps(payload), headers=headers)
    print(response)

  elif x[-3:] == 'jpg' or x[-3:] == 'png' or x[-4:] == 'jpeg':
    print('thats an image, nothing to do here')

3 个答案:

答案 0 :(得分:1)

一个朋友通过此IRL帮助了我,

Option Explicit

Sub SplitToMultiWorksheets()

    Const cSheets As String = "Sheet1,Sheet2,Sheet3,Sheet4"
    Const cStr1 As String = "Are"
    Const cStr2 As String = "You"
    Const cStr3 As String = "me"
    Const cFirstR As Long = 1
    Const cCol1 As Variant = "A"
    Const cCol2 As Variant = "B"
    Const cCol3 As Variant = "C"
    Const cCol4 As Variant = "D"
    Const cFirst1 As Long = 12
    Const cFirst2 As Long = 20

    'Const cLast1  As Long = 19
    'Const cLast2 As Long = 29

    Dim vntSheets As Variant
    Dim vnt1(2) As Variant
    Dim vnt2(2) As Variant
    Dim lastR As Long
    Dim i As Long
    Dim wsName As String
    Dim First1 As Long
    Dim First2 As Long


    vntSheets = Split(cSheets, ",")

    For i = 0 To UBound(vnt1)
        vnt1(i) = cFirst1 - 1
        vnt2(i) = cFirst2 - 1
    Next

    With ThisWorkbook.Worksheets(vntSheets(0))
        lastR = .Cells(.Rows.Count, cCol1).End(xlUp).Row
        For i = cFirstR To lastR
            If .Cells(i, cCol1) = cStr1 Then
                wsName = vntSheets(.Cells(i, cCol2) - 1)
                Select Case .Cells(i, cCol3)
                    Case cStr2
                        First1 = vnt1(.Cells(i, cCol2) - 2) + 1
                        vnt1(.Cells(i, cCol2) - 2) = First1
                        .Parent.Worksheets(wsName).Cells(First1, cCol3) _
                                = .Cells(i, cCol4)
                    Case cStr3
                        First2 = vnt2(.Cells(i, cCol2) - 2) + 1
                        vnt2(.Cells(i, cCol2) - 2) = First2
                        .Parent.Worksheets(wsName).Cells(First2, cCol3) _
                                = .Cells(i, cCol4)
                End Select
            End If
        Next
    End With

End Sub

答案 1 :(得分:0)

print(response)

已收到响应的状态码,因此已收到200,表示请求成功

要获取网址,您可以使用.url

print(response.url)

答案 2 :(得分:0)

ConvertAPI具有Python库https://github.com/ConvertAPI/convertapi-python 使用以下代码,可以帮助您轻松地将pdf转换为jpg。

import convertapi
import os
import tempfile

convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret

jpg_result = convertapi.convert(
    'jpg',
    {
        'File': 'files/test.pdf',
        'ScaleImage': True,
        'ScaleProportions': True,
        'ImageHeight': 300,
        'ImageWidth': 300,
    }
)

saved_files = jpg_result.save_files(tempfile.gettempdir())

print("The thumbnail saved to %s" % saved_files)