google-translategoogle-translation-apigoogle-cloud-translate

google.cloud translation pdf documents (en to zh), return protobuff.repeated,not write pdf,how use python write pdf?


python code : en pdf translate zh pdf,but translate result not write pdf enter image description here


Solution

  • The returned object of response.document_translation.byte_stream_outputs is <class 'proto.marshal.collections.repeated.Repeated'> which means it is a list. You need to loop through the bytes_stream object then write it to file.

    See code snippet below for the implementation:

    bytes_stream = response.document_translation.byte_stream_outputs
    
    f = open('2_after.pdf', 'wb')
    for data in bytes_stream:
        f.write(data)
    f.close()