1

我正在尝试使用模块convertapi在 Python3.8 中合并 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.']}

我怀疑错误来自于 dict 是在合并之前创建的,因为直接在 中输入字典时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 来合并文件,因为当文件包含一些特定字符(主要是汉字)时我遇到了一些错误。

4

1 回答 1

0

如果您访问https://www.convertapi.com/pdf-to-merge并向下滚动,您会很容易找到代码片段生成器和所有编程代码片段,您会发现 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.']}

至于第二个错误,您没有提供代码,所以我无法帮助您。

于 2020-05-18T17:49:06.067 回答