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:
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.
To set a different output format within the format family, use the format property. The following example demonstrates how to specify the target format as TXT when converting a DOCX file:
fromgroupdocs.conversionimportConverterfromgroupdocs.conversion.options.convertimportWordProcessingConvertOptionsfromgroupdocs.conversion.filetypesimportWordProcessingFileTypedefspecify_output_format():# Instantiate Converter with the input document withConverter("./business-plan.docx")asconverter:# Instantiate convert options to define the output format, by default it is DOCXword_convert_options=WordProcessingConvertOptions()# Change the output format within the format family from DOCX to TXTword_convert_options.format=WordProcessingFileType.TXT# Convert the input document to TXTconverter.convert("./business-plan.txt",word_convert_options)if__name__=="__main__":specify_output_format()
business-plan.docx is the sample file used in this example. Click here to download it.
HOME BASED
PROFESSIONAL SERVICES
Business Plan
[TRUNCATED]
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.
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.