@@ -540,6 +540,11 @@ def translate_document_from_filepath(
540540 translation. The exception includes information about the document
541541 request.
542542 """
543+ # Determine output_format from output path
544+ in_ext = pathlib .PurePath (input_path ).suffix .lower ()
545+ out_ext = pathlib .PurePath (output_path ).suffix .lower ()
546+ output_format = None if in_ext == out_ext else out_ext [1 :]
547+
543548 with open (input_path , "rb" ) as in_file :
544549 with open (output_path , "wb" ) as out_file :
545550 try :
@@ -550,6 +555,7 @@ def translate_document_from_filepath(
550555 source_lang = source_lang ,
551556 formality = formality ,
552557 glossary = glossary ,
558+ output_format = output_format ,
553559 )
554560 except Exception as e :
555561 out_file .close ()
@@ -566,6 +572,7 @@ def translate_document(
566572 formality : Union [str , Formality ] = Formality .DEFAULT ,
567573 glossary : Union [str , GlossaryInfo , None ] = None ,
568574 filename : Optional [str ] = None ,
575+ output_format : Optional [str ] = None ,
569576 ) -> DocumentStatus :
570577 """Upload document, translate it into the target language, and download
571578 result.
@@ -585,6 +592,8 @@ def translate_document(
585592 translation. Must match specified source_lang and target_lang.
586593 :param filename: (Optional) Filename including extension, only required
587594 if uploading string or bytes containing file content.
595+ :param output_format: (Optional) Desired output file extension, if
596+ it differs from the input file format.
588597 :return: DocumentStatus when document translation completed, this
589598 allows the number of billed characters to be queried.
590599
@@ -599,6 +608,7 @@ def translate_document(
599608 formality = formality ,
600609 glossary = glossary ,
601610 filename = filename ,
611+ output_format = output_format ,
602612 )
603613
604614 try :
@@ -625,6 +635,7 @@ def translate_document_upload(
625635 formality : Union [str , Formality , None ] = None ,
626636 glossary : Union [str , GlossaryInfo , None ] = None ,
627637 filename : Optional [str ] = None ,
638+ output_format : Optional [str ] = None ,
628639 ) -> DocumentHandle :
629640 """Upload document to be translated and return handle associated with
630641 request.
@@ -642,12 +653,16 @@ def translate_document_upload(
642653 translation. Must match specified source_lang and target_lang.
643654 :param filename: (Optional) Filename including extension, only required
644655 if uploading string or bytes containing file content.
656+ :param output_format: (Optional) Desired output file extension, if
657+ it differs from the input file format.
645658 :return: DocumentHandle with ID and key identifying document.
646659 """
647660
648661 request_data = self ._check_language_and_formality (
649662 source_lang , target_lang , formality , glossary
650663 )
664+ if output_format :
665+ request_data ["output_format" ] = output_format
651666
652667 files : Dict [str , Any ] = {}
653668 if isinstance (input_document , (str , bytes )):
0 commit comments