This documentation topic covers the conversion of a single document to another format, where only one document is produced as output. The following diagram illustrates the process of converting a file from one format to another:
flowchart LR
%% Nodes
A["Input Document (e.g. DOCX)"]
B["Conversion"]
C["Converted Document (e.g. PDF)"]
%% Edge connections between nodes
A --> B --> C
To convert and save a document, use the following Converter class methods:
convert(file_path, convert_options): Converts a document to a specified single output format and saves it to a file, such as converting a DOCX to PDF.
convert(stream, convert_options): Converts the document and writes it to a provided stream instead of a file path.
Convert a Complete Document
The following list of ConvertOptions classes can be used to convert a document to a specific single output format:
PdfConvertOptions – Options for converting to PDF format.
WordProcessingConvertOptions – Options for converting to Word Processing formats.
SpreadsheetConvertOptions – Options for converting to Spreadsheet formats.
PresentationConvertOptions – Options for converting to Presentation formats.
ImageConvertOptions – Options for converting to Image formats (e.g., PNG, JPEG).
WebConvertOptions – Options for converting to Web formats (e.g., HTML).
PageDescriptionLanguageConvertOptions – Options for converting to Page Description Language formats (e.g., PostScript).
EBookConvertOptions – Options for converting to EBook formats (e.g., EPUB, MOBI).
EmailConvertOptions – Options for converting to Email formats (e.g., EML, MSG).
DiagramConvertOptions – Options for converting to Diagram formats (e.g., VSDX).
CadConvertOptions – Options for converting to CAD formats (e.g., DWG).
ThreeDConvertOptions – Options for converting to 3D formats.
ProjectManagementConvertOptions – Options for converting to Project Management formats (e.g., MPP).
GisConvertOptions – Options for converting to GIS formats.
FontConvertOptions – Options for converting to Font formats (e.g., TTF, OTF).
FinanceConvertOptions – Options for converting to Finance formats (e.g., XBRL).
CompressionConvertOptions – Options for converting to Compression formats (e.g., ZIP).
NoConvertOptions – A special option class that instructs the converter to copy the source document without any modifications.
Example 1: Convert a Document to Another Format
The following example demonstrates how to convert a DOCX file to PDF:
fromgroupdocs.conversionimportConverterfromgroupdocs.conversion.options.convertimportPdfConvertOptionsdefconvert_document_to_another_format():# Instantiate Converter with the input document withConverter("./business-plan.docx")asconverter:# Instantiate convert options to define the output formatpdf_convert_options=PdfConvertOptions()# Convert the input document to PDFconverter.convert("./business-plan.pdf",pdf_convert_options)if__name__=="__main__":convert_document_to_another_format()
business-plan.docx is the sample file used in this example. Click here to download it.
business-plan.pdf is the expected output PDF file. Click here to download it.
To convert specific document pages, you can use the following ConvertOptions classes, which provide pages, page_number, and pages_count attributes. These options allow you to specify individual pages or a range of pages to convert.
PdfConvertOptions – Options for converting to PDF format.
ImageConvertOptions – Options for converting to Image formats (e.g., PNG, JPEG).
WordProcessingConvertOptions – Options for converting to Word Processing formats.
SpreadsheetConvertOptions – Options for converting to Spreadsheet formats.
PresentationConvertOptions – Options for converting to Presentation formats.
WebConvertOptions – Options for converting to Web formats (e.g., HTML).
EBookConvertOptions – Options for converting to EBook formats (e.g., EPUB, MOBI).
DiagramConvertOptions – Options for converting to Diagram formats (e.g., VSDX).
PageDescriptionLanguageConvertOptions – Options for converting to Page Description Language formats (e.g., PostScript).
CadConvertOptions – Options for converting to CAD formats (e.g., DWG).
ThreeDConvertOptions – Options for converting to 3D formats.
FinanceConvertOptions – Options for converting to Finance formats (e.g., XBRL).
Example 1: Convert Specific Document Pages to Another Format
You can specify which document pages you would like to convert, as shown in the following example:
fromgroupdocs.conversionimportConverterfromgroupdocs.conversion.options.convertimportPdfConvertOptionsdefconvert_specific_document_pages():# Instantiate Converter with the input document withConverter("./business-plan.docx")asconverter:# Instantiate convert options to define the output formatpdf_convert_options=PdfConvertOptions()# Specify which document pages to convertpdf_convert_options.pages=[1,3,5]# Convert the specified pages of the input document to PDFconverter.convert("./pages-1-3-5.pdf",pdf_convert_options)if__name__=="__main__":convert_specific_document_pages()
business-plan.docx is the sample file used in this example. Click here to download it.
pages-1-3-5.pdf is the expected output PDF file. Click here to download it.
Example 2: Convert N Consecutive Pages
As an alternative, you can specify a number of consecutive pages to convert, as shown in the following example:
fromgroupdocs.conversionimportConverterfromgroupdocs.conversion.options.convertimportPdfConvertOptionsdefconvert_consecutive_document_pages():# Instantiate Converter with the input document withConverter("./business-plan.docx")asconverter:# Instantiate convert options to define the output formatpdf_convert_options=PdfConvertOptions()# Specify the starting page and number of pages to convertpdf_convert_options.page_number=1pdf_convert_options.pages_count=5# Convert the specified range of pages in the document to PDFconverter.convert("./pages-1-through-5.pdf",pdf_convert_options)if__name__=="__main__":convert_consecutive_document_pages()
business-plan.docx is the sample file used in this example. Click here to download it.
pages-1-through-5.pdf` is the expected output PDF file. Click here to download it.
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.