我对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')
一位朋友通过此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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句