MCPcopy Create free account
hub / github.com/DeepL/deepl-python / translate_document_upload

Method translate_document_upload

deepl/translator.py:763–832  ·  view source on GitHub ↗

Upload document to be translated and return handle associated with request. :param input_document: Document to translate as a file-like object, or string or bytes containing file content. :param source_lang: (Optional) Language code of input document, for

(
        self,
        input_document: Union[TextIO, BinaryIO, str, bytes, Any],
        *,
        source_lang: Optional[str] = None,
        target_lang: str,
        formality: Union[str, Formality, None] = None,
        glossary: Union[
            str, GlossaryInfo, MultilingualGlossaryInfo, None
        ] = None,
        filename: Optional[str] = None,
        output_format: Optional[str] = None,
        extra_body_parameters: Optional[dict] = None,
    )

Source from the content-addressed store, hash-verified

761 return status
762
763 def translate_document_upload(
764 self,
765 input_document: Union[TextIO, BinaryIO, str, bytes, Any],
766 *,
767 source_lang: Optional[str] = None,
768 target_lang: str,
769 formality: Union[str, Formality, None] = None,
770 glossary: Union[
771 str, GlossaryInfo, MultilingualGlossaryInfo, None
772 ] = None,
773 filename: Optional[str] = None,
774 output_format: Optional[str] = None,
775 extra_body_parameters: Optional[dict] = None,
776 ) -> DocumentHandle:
777 """Upload document to be translated and return handle associated with
778 request.
779
780 :param input_document: Document to translate as a file-like object, or
781 string or bytes containing file content.
782 :param source_lang: (Optional) Language code of input document, for
783 example "DE", "EN", "FR". If omitted, DeepL will auto-detect the
784 input language.
785 :param target_lang: Language code to translate document into, for
786 example "DE", "EN-US", "FR".
787 :param formality: (Optional) Desired formality for translation, as
788 Formality enum, "less", "more", "prefer_less", or "prefer_more".
789 :param glossary: (Optional) glossary or glossary ID to use for
790 translation. Must match specified source_lang and target_lang.
791 :param filename: (Optional) Filename including extension, only required
792 if uploading string or bytes containing file content.
793 :param output_format: (Optional) Desired output file extension, if
794 it differs from the input file format.
795 :param extra_body_parameters: (Optional) Additional key/value pairs to
796 include in the JSON request body sent to the API. If provided,
797 keys in this dict will be added to the request body. Existing
798 keys set by the client will not be overwritten by entries in
799 extra_body_parameters.
800 :return: DocumentHandle with ID and key identifying document.
801 """
802
803 request_data = self._check_language_and_formality(
804 source_lang, target_lang, formality, glossary
805 )
806 if output_format:
807 request_data["output_format"] = output_format
808
809 files: Dict[str, Any] = {}
810 if isinstance(input_document, (str, bytes)):
811 if filename is None:
812 raise ValueError(
813 "filename is required if uploading file content as string "
814 "or bytes"
815 )
816 files = {"file": (filename, input_document)}
817 else:
818 files = {"file": input_document}
819 if extra_body_parameters:
820 for k, v in extra_body_parameters.items():

Calls 4

_api_callMethod · 0.95
_raise_for_statusMethod · 0.95
DocumentHandleClass · 0.90