向已签名的Excel文件添加数字签名
Contents
[
Hide
]
可能的使用场景
Aspose.Cells for Python via .NET提供Workbook.add_digital_signature方法,用于给已签名的Excel文件添加数字签名。
请注意,在向已签名的Excel文件添加数字签名时,如果原始文件由Aspose.Cells for Python via .NET生成,效果良好;但如果由其他引擎(如Microsoft Excel)生成,Aspose.Cells for Python via .NET在加载和重新保存后可能无法保持原样,这会导致原签名失效。
如何向已经签名的Excel文件添加数字签名
以下示例代码演示了如何使用 Workbook.add_digital_signature 方法向已签名的Excel文件添加数字签名。请查看此代码中使用的 示例Excel文件。该文件已经数字签名。请查看代码生成的 输出Excel文件。这里我们使用了一个名为 AsposeDemo.pfx 的演示证书,密码为 aspose。屏幕截图展示了示例代码对示例Excel文件执行后的效果。
示例代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import Workbook | |
from aspose.cells.digitalsignatures import DigitalSignature, DigitalSignatureCollection | |
from datetime import datetime | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Load the workbook which is already digitally signed to add new digital signature | |
workbook = Workbook(sourceDir + "sampleDigitallySignedByCells.xlsx") | |
# Create the digital signature collection | |
dsCollection = DigitalSignatureCollection() | |
# Create new digital signature and add it in digital signature collection | |
# Cert must contain private key, it can be contructed from cert file or windows certificate collection. aa is password of cert | |
rawData = None | |
password = None | |
signature = DigitalSignature(rawData, password, "Aspose.Cells added new digital signature in existing digitally signed workbook.", datetime.now()) | |
dsCollection.add(signature) | |
# Add digital signature collection inside the workbook | |
workbook.add_digital_signature(dsCollection) | |
# Save the workbook and dispose it. | |
workbook.save(outputDir + "outputDigitallySignedByCells.xlsx") |