我基本上是一个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')发布于 2019-01-19 14:17:59
一个朋友帮了我,用这个IRL,它说:
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发布于 2019-01-18 19:28:37
print(response)接收响应的状态代码,因此它被接收到200,这意味着请求是成功的
以获取您可以使用.url的url
print(response.url)发布于 2019-01-18 22:53:57
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)https://stackoverflow.com/questions/54257785
复制相似问题