比较两个 PDF 文档

问题描述 投票:0回答:0

我正在尝试编写一些代码,通过文件路径从我的桌面比较两个 pdf 文档,然后使用可草拟的 api 来处理比较。由于某种原因,代码无法有效运行。任何帮助表示赞赏

import json
import tempfile
import shutil
import requests

api_key = "key"
pdf1_path = r"C:\Users\s\2.pdf"
pdf2_path = r"C:\Users\s\1.pdf"


def compare_pdfs(api_key, pdf1_path, pdf2_path):
    # Set up the headers for the request
    headers = {"Authorization": f"Token {api_key}"}

    # Step 1: Upload the first PDF to Draftable.
    # This returns a JSON response with a "draft_id" property.
    # Store this ID in a variable called "draft_id1".
    with open(pdf1_path, "rb") as f:
        response1 = requests.post(
            "https://api.draftable.com/v1/documents",
            headers=headers,
            json={"source_identifier": "pdf1"},
            files={"file": f},
        )
    draft_id1 = response1.json()["draft_id"]
    pdf1_url = response1.json()["url"]

    print(f"Response1 status code: {response1.status_code}")
    print(f"Response1 content: {response1.content}")

    # Step 2: Upload the second PDF to Draftable.
    # This returns a JSON response with a "draft_id" property.
    # Store this ID in a variable called "draft_id2".
    with open(pdf2_path, "rb") as f:
        response2 = requests.post(
            "https://api.draftable.com/v1/documents",
            headers=headers,
            json={"source_identifier": "pdf2"},
            files={"file": f},
        )
    draft_id2 = response2.json()["draft_id"]
    pdf2_url = response2.json()["url"]

    print(f"Response2 status code: {response2.status_code}")
    print(f"Response2 content: {response2.content}")

    # Step 3: Create a comparison of the two PDFs and get a public URL to view the comparison.
    comparison_response = requests.post(
        "https://api.draftable.com/v1/comparisons",
        headers=headers,
        json={"left": {"document_id": draft_id1}, "right": {"document_id": draft_id2}},
    )

    comparison_id = comparison_response.json()["identifier"]
    public_view_url = f"https://my.draftable.com/comparison/{comparison_id}"

    return public_view_url


comparison_url = compare_pdfs(api_key, pdf1_path, pdf2_path)
print(f"Comparison URL: {comparison_url}")

'

似乎没有处理 pdf 进行比较。请求大小超时可能是一些问题。

python python-requests shutil
© www.soinside.com 2019 - 2024. All rights reserved.