@@ -153,13 +153,13 @@ def sample_single_document_translation():
153
153
154
154
# absolute path to your document
155
155
file_path = " C:/{your-file-path}/document-translation-sample.docx"
156
- file_name = os.path.path. basename(file_path)
156
+ file_name = os.path.basename(file_path)
157
157
file_type = (
158
158
" application/vnd.openxmlformats-officedocument.wordprocessingml.document"
159
159
)
160
160
print (f " File for translation: { file_name} " )
161
161
162
- with open (file_name , " r " ) as file :
162
+ with open (file_path , " rb " ) as file :
163
163
file_contents = file .read()
164
164
165
165
document_content = (file_name, file_contents, file_type)
@@ -168,8 +168,12 @@ def sample_single_document_translation():
168
168
response_stream = client.document_translate(
169
169
body = document_translate_content, target_language = target_language
170
170
)
171
- translated_response = response_stream.decode(" utf-8-sig" ) # type: ignore [ attr -defined ]
172
- print (f " Translated response: { translated_response} " )
171
+ # Save the response_stream to a file
172
+ output_file_path = " ./translated-document.docx"
173
+ with open (output_file_path, " wb" ) as output_file:
174
+ output_file.write(response_stream)
175
+
176
+ print (f " Translated document saved to: { output_file_path} " )
173
177
174
178
175
179
if __name__ == " __main__" :
0 commit comments