With GroupDocs.Conversion for Python via .NET you to load and convert documents that are protected with a password. This feature is useful when you need to handle documents that require authentication to access their contents.
To load and convert a password-protected document, follow the steps outlined in the code example below:
fromgroupdocs.conversionimportConverterfromgroupdocs.conversion.options.convertimportPdfConvertOptionsfromgroupdocs.conversion.options.loadimportWordProcessingLoadOptionsdefload_password_protected_file():# Set file pathfile_path="./password-protected.docx"# Instantiate load options and set passwordwp_load_options=WordProcessingLoadOptions()wp_load_options.password="12345"# Specify source file stream and load optionsconverter=Converter(file_path,wp_load_options)# Specify output file location and convert optionsoutput_path="./password-protected.pdf"pdf_convert_options=PdfConvertOptions()pdf_convert_options.password="67890"# Convert and save to output pathconverter.convert(output_path,pdf_convert_options)if__name__=="__main__":load_password_protected_file()
password-protected.docx is sample file used in this example. Click here to download it.
password-protected.pdf is converted PDF document. Click here to download it. The file is password-protected. Password is 67890.
In case the provided password is incorrect, a runtime error will be thrown. The expected error and error message are as follows:
RuntimeError: Proxy error(CorruptOrDamagedFileException): Cannot convert. The file is corrupt or damaged. The document password is incorrect. ---> IncorrectPasswordException: The document password is incorrect.
Explanation
File Path Setup: The file path for the password-protected document is specified. In this example, it assumes that the document is named password-protected.docx.
Load Options: An instance of WordProcessingLoadOptions is created, and the password required to open the document is set.
Converter Initialization: A Converter instance is created using the file path and the load options that include the password.
Convert Options: An instance of PdfConvertOptions is created for the conversion process. You can also set the output password for the resulting PDF if required.
Conversion Execution: Finally, the convert method is called on the Converter instance to convert the password-protected document and save it as a PDF.
Conclusion
This example demonstrates how to efficiently load and convert password-protected documents using the GroupDocs.Conversion for Python API. Make sure to replace the passwords and file paths with your actual values before executing the code.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.