我正尝试在Python3.8中使用模块convertapi来合并PDF。我尝试了多种方法,但我无法找出返回错误的来源。下面是我的函数:
def merger(output_path, input_paths):
dictFiles = {}
for i,path in enumerate(input_paths):
dictFiles[f'File[{i}]'] = path
convertapi.api_secret = 'my-api-secret'
result = convertapi.convert('merge', dictFiles, from_format = 'pdf')
result.save_files(output_path)下面是返回的错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python\Python38\lib\site-packages\convertapi\client.py", line 46, in handle_response
r.raise_for_status()
File "C:\Python\Python38\lib\site-packages\requests\models.py", line 941, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url:
https://v2.convertapi.com/convert/pdf/to/merge?Secret=my-api-secret
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
...
File "D:\Desktop\merger.py", line 46, in merger
result = convertapi.convert('merge', dictFiles, from_format = 'pdf')
File "C:\Python\Python38\lib\site-packages\convertapi\api.py", line 7, in convert
return task.run()
File "C:\Python\Python38\lib\site-packages\convertapi\task.py", line 26, in run
response = convertapi.client.post(path, params, timeout = timeout)
File "C:\Python\Python38\lib\site-packages\convertapi\client.py", line 16, in post
return self.handle_response(r)
File "C:\Python\Python38\lib\site-packages\convertapi\client.py", line 49, in handle_response
raise ApiError(r.json())
convertapi.exceptions.ApiError: Parameter validation error. Code: 4000. {'Files': ['Files array item
count must be greater than 0.']}我怀疑错误来自于在合并之前创建字典的事实,因为当在covertapi.convert()中直接输入字典时,我没有得到相同的错误:
def merger(output_path, input_paths):
convertapi.api_secret = 'my-api-secret'
convertapi.convert('merge', {
'Files[0]': 'path/to/file1.pdf',
'Files[1]': 'path/to/file2.pdf'
}, from_format = 'pdf').save_files(output_path)这里有一个不同的错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python\Python38\lib\site-packages\convertapi\client.py", line 46, in handle_response
r.raise_for_status()
File "C:\Python\Python38\lib\site-packages\requests\models.py", line 941, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url:
https://v2.convertapi.com/convert/pdf/to/merge?Secret=my-api-secret
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
...
File "D:\Desktop\merger.py", line 50, in merger
convertapi.convert('merge', {
File "C:\Python\Python38\lib\site-packages\convertapi\api.py", line 7, in convert
return task.run()
File "C:\Python\Python38\lib\site-packages\convertapi\task.py", line 26, in run
response = convertapi.client.post(path, params, timeout = timeout)
File "C:\Python\Python38\lib\site-packages\convertapi\client.py", line 16, in post
return self.handle_response(r)
File "C:\Python\Python38\lib\site-packages\convertapi\client.py", line 49, in handle_response
raise ApiError(r.json())
convertapi.exceptions.ApiError: Unable to download remote file. Code: 5007.请注意,这里我注意到使用PyPDF2合并文件,因为当文件包含某些特定字符(主要是中文字符)时,我会遇到一些错误。
发布于 2020-05-19 01:49:06
如果您将转到https://www.convertapi.com/pdf-to-merge并向下滚动,您将很容易地找到snippet builder和amount所有编程代码段,您将找到Python。
convertapi.api_secret = 'Your_secret'
convertapi.convert('merge', {
'Files[0]': '/path/to/dpa.pdf',
'Files[1]': '/path/to/sample.pdf'
}, from_format = 'pdf').save_files('/path/to/dir')如果您花一些时间分析代码片段,您会发现Files数组使用了复数,而不是像您的代码中那样使用单数。
def merger(output_path, input_paths):
dictFiles = {}
for i,path in enumerate(input_paths):
dictFiles[f'File[{i}]'] = path
convertapi.api_secret = 'my-api-secret'
result = convertapi.convert('merge', dictFiles, from_format = 'pdf')
result.save_files(output_path)
convertapi.exceptions.ApiError: Parameter validation error. Code: 4000. {'Files': ['Files array item
count must be greater than 0.']}至于第二个错误,您没有提供代码,所以我无法帮助您。
https://stackoverflow.com/questions/61873137
复制相似问题