使用 convertapi 合并 python 中的 PDF

Merging PDFs in python using convertapi

我正在尝试使用模块 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.']}

我怀疑错误是由于字典是在合并之前创建的,因为在 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 合并文件,因为当文件包含某些特定字符(主要是中文字符)时我会遇到一些错误。

如果您转到 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')

如果您花一些时间分析代码片段,您会发现复数用于文件数组,而不是像您的代码中那样使用单数。

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.']}

关于第二个错误,你没有提供代码,所以我帮不了你。