检测两个 Word 文件之间的差异是处理合同、报告或法律文档的开发人员的常见需求。 Aspose.BarCode Cloud SDK for Python 提供了一个强大的库,可与 Aspose.Words Cloud SDK for Python 结合,在 Python 中以编程方式比较 Word 文档中的文本。在本指南中,您将设置环境,将文档上传到 Aspose Cloud 存储,配置比较选项,运行比较,并检索带有修订痕迹的结果文档。您还将看到如何使用 cURL 直接进行 REST 调用来实现相同的结果。
环境准备 - 前置条件和设置
在开始之前,请确保您具备以下条件:
- 在本地安装了 Python 3.8 或更高版本。
- 拥有 Aspose Cloud 账户,并具备 client_id 和 client_secret(可在 Aspose Cloud 仪表板中获取)。
- 访问 Aspose Cloud 存储以进行临时文件处理。
通过 pip 安装 BarCode SDK:
pip install aspose-barcode-cloud
您还需要 Aspose.Words Cloud SDK for Python,它处理特定于 Word 的操作:
pip install asposewordscloud
将您的凭据设置为环境变量,或在代码中替换占位符:
import os
client_id = os.getenv("ASPOSE_CLIENT_ID")
client_secret = os.getenv("ASPOSE_CLIENT_SECRET")
SDK 将使用这些值对 Aspose Cloud 服务进行身份验证。环境准备就绪后,我们可以继续实现。
在 Python 中比较 Word 文档中的文本:逐步演练
步骤 1:加载源文档
首先,配置您的 Aspose Cloud 凭据,以便 SDK 能够进行身份验证,并且您可以在 Python 中比较 Word 文档中的文本。
from asposewordscloud import WordsApi, Configuration
config = Configuration(client_id=client_id, client_secret=client_secret)
words_api = WordsApi(config)
步骤 2:上传源文档和目标文档
上传您想要比较的两个 DOCX 文件。辅助函数抽象了上传过程。
def upload(local_file, remote_name):
with open(local_file, "rb") as f:
words_api.upload_file(request={"path": f"TempCompare/{remote_name}", "file": f})
upload("doc1.docx", "doc1.docx")
upload("doc2.docx", "doc2.docx")
第 3 步:设置比较选项
接下来,设置 CompareOptions 来定义 SDK 在 Python 中比较 Word 文档文本的方式,例如忽略大小写或注释。
from asposewordscloud.models import CompareOptions
compare_opts = CompareOptions(
author="PythonComparer",
compare_options="IgnoreCase,IgnoreComments,IgnoreFormatting",
file_path="TempCompare/doc2.docx",
revision_author="PythonComparer",
revision_date_time="2023-01-01T00:00:00Z"
)
第4步:执行文档比较
现在调用 compare_document 方法来实际比较 Python 中的 Word 文档文本并生成修订跟踪的结果。
compare_result = words_api.compare_document(
request={
"name": "doc1.docx",
"compare_data": compare_opts,
"folder": "TempCompare"
}
)
第5步:检索并保存结果
最后,将生成的文档写入磁盘并清理临时文件。
with open("compare_result.docx", "wb") as out_file:
out_file.write(compare_result.document)
words_api.delete_file(request={"path": "TempCompare/doc1.docx"})
words_api.delete_file(request={"path": "TempCompare/doc2.docx"})
在 Python 中比较 Word 文档中的文本 - 完整实现代码示例
以下代码演示了使用 Aspose APIs 在 Python 中比较 Word 文档中文本的完整工作流。
import os
from asposewordscloud import WordsApi, Configuration
from asposewordscloud.models import CompareOptions
# ----------------------------------------------------------------------
# Configuration – set your Aspose Cloud credentials in environment variables
# ----------------------------------------------------------------------
client_id = os.getenv("ASPOSE_CLIENT_ID")
client_secret = os.getenv("ASPOSE_CLIENT_SECRET")
config = Configuration(client_id=client_id, client_secret=client_secret)
# ----------------------------------------------------------------------
# Initialize the Words API client
# ----------------------------------------------------------------------
words_api = WordsApi(config)
# ----------------------------------------------------------------------
# Local file paths (replace with your actual files)
# ----------------------------------------------------------------------
source_path = "doc1.docx"
target_path = "doc2.docx"
result_path = "compare_result.docx"
# ----------------------------------------------------------------------
# Remote folder in Aspose Cloud storage where temporary files are kept
# ----------------------------------------------------------------------
remote_folder = "TempCompare"
# ----------------------------------------------------------------------
# Helper to upload a local file to Aspose Cloud storage
# ----------------------------------------------------------------------
def upload(local_file, remote_name):
with open(local_file, "rb") as f:
words_api.upload_file(request={"path": f"{remote_folder}/{remote_name}", "file": f})
# ----------------------------------------------------------------------
# Upload source and target documents
# ----------------------------------------------------------------------
upload(source_path, source_path)
upload(target_path, target_path)
# ----------------------------------------------------------------------
# Set up comparison options
# ----------------------------------------------------------------------
compare_opts = CompareOptions(
author="PythonComparer",
compare_options="IgnoreCase,IgnoreComments,IgnoreFormatting",
file_path=f"{remote_folder}/{target_path}",
revision_author="PythonComparer",
revision_date_time="2023-01-01T00:00:00Z"
)
# ----------------------------------------------------------------------
# Perform the comparison
# ----------------------------------------------------------------------
try:
compare_result = words_api.compare_document(
request={
"name": source_path,
"compare_data": compare_opts,
"folder": remote_folder
}
)
# The API returns the resulting document as a byte array
with open(result_path, "wb") as out_file:
out_file.write(compare_result.document)
finally:
# ------------------------------------------------------------------
# Clean up remote temporary files
# ------------------------------------------------------------------
words_api.delete_file(request={"path": f"{remote_folder}/{source_path}"})
words_api.delete_file(request={"path": f"{remote_folder}/{target_path}"})
注意: 此代码示例演示了核心功能。在将其用于项目之前,请确保更新所有文件路径和配置值以匹配实际环境,验证已正确安装所有必需的依赖项,并在开发环境中进行彻底测试。如果遇到任何问题,请参阅官方文档或联系支持团队获取帮助。
使用 cURL 通过 REST API 执行文档比较
以下是使用 REST API 在 Python 操作中执行相同的 Word 文档文本比较的 cURL 命令。
- 认证并获取访问令牌
curl -X POST "https://api.aspose.cloud/connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
- 上传源文件
curl -X PUT "https://api.aspose.cloud/v4.0/words/TempCompare/doc1.docx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary "@doc1.docx"
- 上传目标文件
curl -X PUT "https://api.aspose.cloud/v4.0/words/TempCompare/doc2.docx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary "@doc2.docx"
- 执行比较
curl -X POST "https://api.aspose.cloud/v4.0/words/doc1.docx/compare" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"compareOptions": {
"author": "PythonComparer",
"compareOptions": "IgnoreCase,IgnoreComments,IgnoreFormatting",
"filePath": "TempCompare/doc2.docx",
"revisionAuthor": "PythonComparer",
"revisionDateTime": "2023-01-01T00:00:00Z"
}
}' -o compare_result.docx
- 下载输出文件
curl -X GET "https://api.aspose.cloud/v4.0/words/TempCompare/compare_result.docx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o compare_result.docx
有关请求正文和其他参数的更多详细信息,请参阅官方 API 文档。
结论
本文演示了如何在 Python 中使用 Aspose.BarCode Cloud SDK for Python 对 Word 文档中的文本进行比较。通过按照设置凭据、上传文件、配置比较选项以及调用 compare API 的步骤,您可以在应用程序中实现文档差异工作流的自动化。请记得获取适用于生产环境的正式许可证;临时许可证可在临时许可证页面获取,完整的许可详情列在产品页面上。借助这些工具,将文档比较集成到您的 Python 项目中变得简单可靠。
常见问题
-
Aspose.BarCode Cloud SDK for Python 如何帮助我在 Python 中比较 Word 文档中的文本?
该 SDK 与 Aspose.Words Cloud SDK for Python 配合使用,可上传 DOCX 文件,设置比较选项,并生成带有修订跟踪的结果,以突出显示差异。请参阅 Aspose.BarCode Cloud SDK for Python 产品页面了解详情。 -
我可以高效比较大型 Word 文件吗?
是的。通过使用compare_documentAPI,您可以从 Aspose Cloud 存储流式传输大型 DOCX 文件,服务在服务器端处理这些文件,从而最大限度地减少本机的内存使用。有关性能技巧,请参阅官方文档。 -
比较支持哪些文件格式?
API 主要使用 DOCX 文件,但您也可以在使用 Aspose.Words 将 DOC 文件转换为 DOCX 后进行比较。所有格式名称均为大写,例如 DOCX、PDF。 -
我需要许可证才能在生产环境中运行吗?
临时许可证可在临时许可证页面获取。对于生产使用,您应按照产品页面上的说明购买完整许可证。