How to Translate PDF with Python (Google vs AWS Translate) — Part 1: extract and translate text | by Agnese Kapnere | Towards Data Science

I needed to translate a PDF file containing text from English to Latvian. It turned out to be slightly more challenging than I initially thought, so I decided to write a tutorial to share what I learned and hopefully save some time for you. I have split my project into two parts.
This article is part one, and it focuses on how to read your PDF file, extract text, and translate it. It looks at two ways to translate the text — using Google Translate and AWS Translate.
Part 2 will look at how to create, format, and save a new PDF file from the obtained translation. You will find the link to my project in GitHub with the full code at the end of this article.
How to read PDF files using Python PyPDF2 library and extract text from PDF
2 ways to translate the text: python googletrans library and AWS Translate.
To read a PDF file programmatically, you can use various 3rd party libraries, this article uses PyPDF2 library.
PyPDF2 can be installed using the pip package manager:
pip install PyPDF2
To read the file, we would first open the file in binary reading mode and create a PdfFileReader.
file = open("example.pdf", 'rb')reader = PdfFileReader(file)
Now you can read the PDF file one page at a time. You can obtain the number of pages in the document by calling a numPages attribute on the reader object. Then iterate through all the pages one by one and extract text. To get page content, use reader.getPage(<page number>) and to extract text from the page content, use extractText method.
num_pages = reader.numPagesfor p in range(num_pages):    page = reader.getPage(p)    text = page.extractText()
Once the text is extracted, it can be translated. I was curious to compare googletrans python library that uses Google Translate with the AWS Translator. Thegoogletrans library is open source, and there are no extra costs to use it. AWS Translate is using neural machine learning algorithms to translate and takes into account the context when translating. To use AWS Translate, you need to have an AWS account. They offer a free tier for the first 12 months, and you can translate 2 million characters during this time. After that, pay-as-you-go pricing applies, and there is a cost of $15 per million characters. Please note that the free tier is activated the first time you use the service.
You can install this library using pip:
pip install googletrans
Behind the scenes, this library uses Google Translate Ajax API to make calls to the translate method. You can also specify Google service domain URL. In the below example, I am declaring a list of two different service domains: translate.google.com and translate.google.lv.
You need to create a translator client instance and then call the translate method to translate the text. The default destination language is English. It can be changed by specifying the dest parameter in the translate method. The default source language is automatically detected, but you can specifically define the source language using thesrc parameter. In the example copied below, it automatically detects the source language and translates to Latvian.
from googletrans import TranslatorURL_COM = 'translate.google.com'URL_LV = 'translate.google.lv'LANG = "lv"translator = Translator(service_urls=[URL_COM, URL_LV])translation = translator.translate(text, dest=LANG)
You are required to have an AWS account to use AWS Translate. Please note that if you are using the AWS services programmatically using python, then the easiest way to do it is to use python SDK that is provided by AWS called boto3. The way AWS works is, it sends an HTTP API call to the AWS Translate endpoint in your selected region. This request is signed cryptographically using your access key and secret access key and the current timestamp. You do need to configure your aws_access_key_id and aws_secret_access_key. It uses something called AWS Signature Version 4 protocol to sign your request. The clock on your laptop must be correct as this protocol is taking into account the current time. It would incorporate timestamp when signing your request and the request you send to AWS is valid only for 15 minutes. If it is received after 15 minutes, then AWS will reject the request for security reasons.
To install AWS Python boto3 SDK:
pip install boto3
As already mentioned above, then AWS offers a free tier to try AWS Translate for 12 months, giving you 2 million characters free to translate. After that, it is $15 per 1 million characters. Please note that AWS is also counting whitespaces as characters.
To translate, we first create a client for translate service and specify the region. Then call the translate_text method on the client object, providing the Text parameter which is the text that you would like translated. SourceLanguageCode allows specifying the source language. If the value auto is used, then the source language will be automatically detected. TargetLanguageCode allows specifying the target language. Once you have received the result back from AWS, there is an extra step to get the Translated text, and it is to call result.get(‘TranslatedText’).
import boto3LANG = "lv"AWS_REGION = 'eu-west-1'translate = boto3.client(service_name='translate',                         region_name=AWS_REGION,                         use_ssl=True)result = translate.translate_text(Text=page.extractText(),                                  SourceLanguageCode="auto",                                  TargetLanguageCode=LANG)translation = result.get('TranslatedText')
Thank you for taking the time to read. I hope you found this article useful. You learned how to read PDF file using python and how to extract the text from the PDF files. Then you looked at two different ways how this text can be automatically translated.
Look out for Part 2 that will look at how to save a translated text into a new PDF file using the python library reportlab. It will also go into formating and saving the newly created file.
See full code of the project in GitHub: https://github.com/akapne01/translator