首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ConvertAPI: PDF在Python2.7.10中-如何获得转换结果URL?

ConvertAPI: PDF在Python2.7.10中-如何获得转换结果URL?
EN

Stack Overflow用户
提问于 2019-01-18 16:19:42
回答 3查看 363关注 0票数 0

我基本上是一个Python的新手(和一般的编码),所以请原谅我是否愚蠢。

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

向ConvertAPI发送请求到目前为止还能工作,ConvertAPI说测试文件已经被转换了。下面是我的问题:如何获得转换文件的最终URL?如果我打印响应,我将得到Response [200],但没有任何其他可以处理的内容。

我试过打开Async参数,但到目前为止都没有效果。据我所知,StoreFile必须设置为true,但它似乎没有什么区别。

代码语言:javascript
复制
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')
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-01-19 14:17:59

一个朋友帮了我,用这个IRL,它说:

代码语言:javascript
复制
import requests
import json

output = {'output_urls' : []} 
url = 'https://v2.convertapi.com/convert/pdf/to/jpg?Secret=xxxxxxx' # Hidden
headers = {'content-type': 'application/json'}
payload = {
    'Parameters': [
        {
            'Name': 'File',
            'FileValue': {
                'Url': 'to be populated'
            }
        },
        {
            'Name': 'StoreFile',
            'Value': 'true'
        },
        {
            'Name': 'ScaleImage',
            'Value': 'true'
        },
        {
            'Name': 'ScaleProportions',
            'Value': 'true'
        },
        {
            'Name': 'ScaleIfLarger',
            'Value': 'true'
        },
        {
            'Name': 'ImageHeight',
            'Value': '2200'
        },
        {
            'Name': 'ImageWidth',
            'Value': '1625'
        }
    ]
}

for x in input_data['input_urls'].split(',') : # input_data is passed by Zapier
  if x[-3:] == 'pdf':
    payload['Parameters'][0]['FileValue']['Url'] = x

    response = requests.post(url, data=json.dumps(payload), headers=headers)
    response_obj = json.loads(response._content)

    for file_url in response_obj['Files'] :
      output['output_urls'].append(file_url['Url'])

  elif x[-3:] == 'jpg' or x[-3:] == 'png' or x[-4:] == 'jpeg' :
    output['output_urls'].append(x)

return output
票数 1
EN

Stack Overflow用户

发布于 2019-01-18 19:28:37

代码语言:javascript
复制
print(response)

接收响应的状态代码,因此它被接收到200,这意味着请求是成功的

以获取您可以使用.url的url

代码语言:javascript
复制
print(response.url)
票数 0
EN

Stack Overflow用户

发布于 2019-01-18 22:53:57

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

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54257785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档