Skip to content

Instantly share code, notes, and snippets.

View ThibaudLamothe's full-sized avatar
👨‍💻

Thib ThibaudLamothe

👨‍💻
View GitHub Profile
print('Hello World!')
@ThibaudLamothe
ThibaudLamothe / deepl_basic.py
Created May 9, 2020 11:39
Translating a sentence on deepL.com using selenium
# Make imports
import time
import clipboard
from selenium import webdriver
# Define text to translate
text_to_translate = 'This is a translation example for my article.'
# Start a Selenium driver
driver_path='../chromedriver'
@ThibaudLamothe
ThibaudLamothe / deepl_corpus_creation.py
Created May 9, 2020 13:23
Creating a corpus from different sentences to minimize the number of Translations using DeepL
# Importing our previous translation function
from run_translation import translate_sentence
# Sentences input
sentence1 = 'I want to translate a first sentence without any link to the second one.'
sentence2 = 'The starlings ate all the cherries in one afternoon, there won\'t be any more for us.''
# Creation of the corpus as a list of strings
corpus = [sentence1, sentence2]
@ThibaudLamothe
ThibaudLamothe / deepl_corpus_creation_maxlength.py
Created May 9, 2020 13:49
Preparation of a batch corpus with maximum size
def prepare_batch_corpus(corpus, max_caracter=5000):
# Size information
nb_sentence = len(corpus)
# Batch information (reset these values after each batch finalization)
batch = []
batch_length = 0
# All batches are stored in that list, which will bbe the output of the function
@ThibaudLamothe
ThibaudLamothe / deeepl_pasting_with_selenium.py
Created May 9, 2020 14:21
How to paste text into the DeepL textarea with Selenium
# Making necessary imports
import clipboard
from selenium.webdriver.common.keys import Keys
# Identifying the text area in the html structure
input_css = 'div.lmt__inner_textarea_container textarea'
# Connecting to it with our driver
input_area = driver.find_element_by_css_selector(input_css)
# Getting button location on the html tree
button_css = ' div.lmt__target_toolbar__copy button'
# Getting the button object
button = driver.find_element_by_css_selector(button_css)
# Extracting its position
y = button.location['y']
# Positionning the button into the screen
@ThibaudLamothe
ThibaudLamothe / deepl_ppt_basic.py
Created May 12, 2020 16:40
Extracting texts from text_frame in a ppt file
from pptx import Presentation
def get_texts_from_file(input_file):
# Instantiate variable to store the texts
texts = []
# Load the presentation
prs = Presentation(input_file)
@ThibaudLamothe
ThibaudLamothe / deepl_ppt_replace.py
Last active May 12, 2020 17:37
Replacing texts into a powerpoint
# parameters
input_file = 'original.pptx'
output_file = 'translated.pptx'
# Opening presentation
prs = Presentation(input_file)
# List of all texts [text1, text2, ....]
corpus = extract_text_frames_from(prs)
@ThibaudLamothe
ThibaudLamothe / deepl_replace_ppt_text_no_font.py
Created May 12, 2020 17:54
Replacing the text of a ppt text frame without changing it's font parameters
# For each paragraph of each text_frame
for text_frame in all_presentation_text_frames:
for paragraph in text_frame.paragraphs:
# We get the translated text
old_text = text_frame.paragraphs[idx].text
new_text = corpus_translated[old_text]
# And inspect the runs (for font analysis)
if len(paragraph.runs)>0:
@ThibaudLamothe
ThibaudLamothe / deepl_replace_ppt_text_no_font.py
Created May 12, 2020 17:54
Replacing the text of a ppt text frame without changing it's font parameters
# For each paragraph of each text_frame
for text_frame in all_presentation_text_frames:
for paragraph in text_frame.paragraphs:
# We get the translated text
old_text = text_frame.paragraphs[idx].text
new_text = corpus_translated[old_text]
# And inspect the runs (for font analysis)
if len(paragraph.runs)>0: