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

Method translate_document_from_filepath

deepl/translator.py:619–684  ·  view source on GitHub ↗

Upload document at given input path, translate it into the target language, and download result to given output path. :param input_path: Path to document to be translated. :param output_path: Path to store translated document. :param source_lang: (Optional) Language

(
        self,
        input_path: Union[str, pathlib.PurePath],
        output_path: Union[str, pathlib.PurePath],
        *,
        source_lang: Optional[str] = None,
        target_lang: str,
        formality: Union[str, Formality] = Formality.DEFAULT,
        glossary: Union[
            str, GlossaryInfo, MultilingualGlossaryInfo, None
        ] = None,
        timeout_s: Optional[int] = None,
        extra_body_parameters: Optional[dict] = None,
    )

Source from the content-addressed store, hash-verified

617 )
618
619 def translate_document_from_filepath(
620 self,
621 input_path: Union[str, pathlib.PurePath],
622 output_path: Union[str, pathlib.PurePath],
623 *,
624 source_lang: Optional[str] = None,
625 target_lang: str,
626 formality: Union[str, Formality] = Formality.DEFAULT,
627 glossary: Union[
628 str, GlossaryInfo, MultilingualGlossaryInfo, None
629 ] = None,
630 timeout_s: Optional[int] = None,
631 extra_body_parameters: Optional[dict] = None,
632 ) -> DocumentStatus:
633 """Upload document at given input path, translate it into the target
634 language, and download result to given output path.
635
636 :param input_path: Path to document to be translated.
637 :param output_path: Path to store translated document.
638 :param source_lang: (Optional) Language code of input document, for
639 example "DE", "EN", "FR". If omitted, DeepL will auto-detect the
640 input language.
641 :param target_lang: Language code to translate document into, for
642 example "DE", "EN-US", "FR".
643 :param formality: (Optional) Desired formality for translation, as
644 Formality enum, "less", "more", "prefer_less", or "prefer_more".
645 :param glossary: (Optional) glossary or glossary ID to use for
646 translation. Must match specified source_lang and target_lang.
647 :param timeout_s: (beta) (Optional) Maximum time to wait before
648 the call raises an error. Note that this is not accurate to the
649 second, but only polls every 5 seconds.
650 :param extra_body_parameters: (Optional) Additional key/value pairs to
651 include in the JSON request body sent to the API. If provided,
652 keys in this dict will be added to the request body. Existing
653 keys set by the client will not be overwritten by entries in
654 extra_body_parameters.
655 :return: DocumentStatus when document translation completed, this
656 allows the number of billed characters to be queried.
657
658 :raises DocumentTranslationException: If an error occurs during
659 translation. The exception includes information about the document
660 request.
661 """
662 # Determine output_format from output path
663 in_ext = pathlib.PurePath(input_path).suffix.lower()
664 out_ext = pathlib.PurePath(output_path).suffix.lower()
665 output_format = None if in_ext == out_ext else out_ext[1:]
666
667 with open(input_path, "rb") as in_file:
668 with open(output_path, "wb") as out_file:
669 try:
670 return self.translate_document(
671 in_file,
672 out_file,
673 target_lang=target_lang,
674 source_lang=source_lang,
675 formality=formality,
676 glossary=glossary,

Calls 2

translate_documentMethod · 0.95
closeMethod · 0.45