Converting a Word document to PDF while preserving comments
Converting Word documents to PDF with comments preserved — whether for contracts, technical specifications, or regulatory documents — ensures that all stakeholder input remains accessible in the final format.
How Nutrient helps you achieve this
Nutrient Python SDK handles DOCX-to-PDF conversion with comment preservation. With the SDK, you don’t need to worry about:
- Parsing Word document structures
- Managing comment extraction and placement
- Handling markup mode configuration
- Complex annotation conversion logic
Instead, Nutrient provides an API that handles all the complexity behind the scenes, letting you focus on your business logic.
Complete implementation
Below is a complete working example that demonstrates DOCX-to-PDF conversion with comments preserved. These lines set up the Python application. The import statements bring in all necessary classes from the Nutrient SDK:
from nutrient_sdk import Documentfrom nutrient_sdk import DocumentMarkupModefrom nutrient_sdk import NutrientExceptionThis line opens the Word document containing comments. The context manager(opens in a new tab) syntax ensures the document is automatically closed when you’re done, preventing resource leaks:
def main(): try: with Document.open("input_with_comments.docx") as document:This line configures the markup mode. Four modes are available:
ALL_MARKUP— Show all markups in different colors, underlined/struck through. Comments are converted to annotations. This is the default behavior.SIMPLE_MARKUP— Show the document as if all changes were accepted. Comments are converted to annotations.NO_MARKUP— Show the document as if all changes were accepted. Comments aren’t converted to annotations.ORIGINAL— Show the document as if none of the changes were made (as if all changes were rejected). Comments aren’t converted to annotations:
document.settings.word_settings.set_markup_mode(DocumentMarkupMode.ALL_MARKUP)This block exports the document to PDF with all comments preserved as annotations. The try-except block handles potential errors using NutrientException:
document.export_as_pdf("output.pdf") print("Successfully converted with comments preserved") except NutrientException as e: print(f"Error: {e}")
if __name__ == "__main__": main()Conclusion
The conversion logic consists of three steps:
- Open the document.
- Configure the markup mode.
- Export as PDF.
Nutrient handles Word document parsing and comment conversion so you don’t need to understand document internals or manage annotation placement manually.
You can download this ready-to-use sample package that’s fully configured to help you get started with the Python SDK.