-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFROM_WEB_NO_GUI_FINAL.py
More file actions
478 lines (416 loc) · 21.8 KB
/
FROM_WEB_NO_GUI_FINAL.py
File metadata and controls
478 lines (416 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import os
from io import open
import shutil
import collections
import collections.abc
from os.path import basename
import pptx.util
from pptx import Presentation
from pptx.util import Pt
from bs4 import BeautifulSoup
import requests
import deepl
#from pygoogletranslation import Translator
from deep_translator import GoogleTranslator
import openai
import time
"""
This program uses different APIs to do a text translation and creates a powerpoint slides to show the output.
APIs: Google Translate , Azure Translate, ChatGPT Translate, and DeepL Translate.
Text translation using DeepL API!
-To use the DeepL Python Library, you'll need an API authentication key.
To get a key, please create an account here -->https://www.deepl.com/docs-api/api-access/
With a DeepL API Free account you can translate up to 500,000 characters/month for free.
Installation
The library can be installed from PyPI using pip:
pip install --upgrade deepl """
methods = "Web"
slideOption = 5
textFont = titleFont = "NATS"
textSize = 13
titleSize = 2
output_file = 'project_abcd.pptx'
dest_language = "ar" # Arabic language
dest_language_DeepL="FR" # French language
TranslationEngines = {}
TranslationEngines["DeepLAPI"] = "DeepL"
TranslationEngines['AzureAPI'] = 'Azure'
TranslationEngines['GoogleAPI'] = 'Google'
TranslationEngines['ChatGPTAPI'] = 'ChatGPT'
#function to provide users with a list of options to choose from
def selectFromDict(options, Engine):
index = 0
indexValidList = []
print('Select an ' + Engine + ':')
for optionName in options:
index = index + 1
indexValidList.extend([options[optionName]])
print(str(index) + ') ' + optionName)
inputValid = False
while not inputValid:
inputRaw = input(Engine + ': ')
inputNo = int(inputRaw) - 1
if inputNo > -1 and inputNo < len(indexValidList):
selected = indexValidList[inputNo]
print('Selected ' + Engine + ': ' + selected)
inputValid = True
break
else:
print('Please select a valid ' + Engine + ' number')
return selected
print(selected)
inp = str(selectFromDict(TranslationEngines, 'Engine'))
#Select with slide layout option to use
print("\nSelect The Slide layout option:\n\n- Enter a number from 1 to 5 to select a layout for the output powerpoint.\n")
layoutInput = input()
print("You have selected layout " + str(layoutInput) + ".")
slideOption = int(layoutInput)
#print("input: ", inp)
""" DeepL API integration"""
auth_key = "6bd0d1df-69ed-1ff3-374c-*******************" # (auth_key) Replace with your key
translator = deepl.Translator(auth_key)
# DeepL API testing
result = translator.translate_text("Hello, world!", target_lang="FR")
# print(result.text) # "Bonjour, le monde !"
do_translation = True
# all_pages = [26, 27, 28, 29, 30, 39, 50, 52, 53, 110, 111, 112, 116, 196, 206, 262, 265, 275, 276, 314, 317, 318, 319, 320, 321, 322, 324, 325, 326, 327, 328, 329, 401, 405, 406, 407, 409, 410, 411, 412, 413, 415, 418, 419, 422, 423, 424, 425, 426, 427, 428, 431, 432, 433, 434, 437, 438, 439, 440, 441, 442, 443, 444, 445, 462, 463, 468, 469, 470, 471, 472, 475, 476, 477, 478, 483, 484, 491, 492, 493, 502, 506, 520, 542, 544, 549, 568, 574, 578, 581, 582, 601, 605, 611, 626, 627, 631, 649, 654, 655, 658, 659, 660, 662, 664, 665, 666, 667, 670, 671, 672, 674, 678, 681, 682, 683, 684, 686, 688, 689, 690, 691, 693, 695, 696, 697, 698, 699, 700]
all_pages = [26, 27, 28, 29, 30, 39, 50, 52, 53, 110]
def buildPresentation():
prs = Presentation()
presentationLength = len(all_pages)
pictureSlide = 0
if (slideOption == 2):
pictureSlide = 1
# web scrapes the URL to get all needed information from the page
for i in range(0, presentationLength):
URL = "https://projectabcd.com/display_the_dress.php?id=" + str(all_pages[i])
page = requests.get(URL, headers={"User-Agent": "html"})
soup = BeautifulSoup(page.content, "html.parser")
logo = soup.find("img")
printLogo = logo.attrs["src"]
logoURL = "http://projectabcd.com/" + printLogo
r = requests.get(logoURL, headers={"User-Agent": "html"}, stream=True)
if r.status_code == 200:
with open(basename(printLogo), "wb") as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
pageInfo = soup.find("div", class_="containerTitle")
pageInfoImg = soup.find("div", class_="container")
name = pageInfo.find("h2", class_="headTwo")
printName = name.text
image = pageInfoImg.find("div", class_="containerImage")
img = image.find("image", class_="image")
printImage = img.get("src")
pictureURL = "http://projectabcd.com/" + printImage
r = requests.get(pictureURL, headers={"User-Agent": "html"}, stream=True)
if r.status_code == 200:
with open(basename(printImage), "wb") as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
pagetext = pageInfoImg.find("div", class_="containerText")
description = pagetext.find("p", class_="words")
printDescription = (description.text)
fact = description.find_next_sibling("p")
printFact = fact.text
# We now got printDescription and printFact
if do_translation:
if inp == "DeepL": # DeepL API
print('DeepL API Engine is translating the text...')
printDescription = DeepL(printDescription)
printFact = DeepL(printFact)
elif inp == 'Azure': #Azure API
print('MS-Azure API Engine is translating the text...')
printDescription = translate_text_Azure(printDescription, "en", dest_language)
printFact = translate_text_Azure(printFact, "en", dest_language)
elif inp == "Google": # GoogleTranslate API
print('GoogleTranslate API Engine is translating the text...')
printDescription = GoogleTranslator1(printDescription, dest_language)
printFact = GoogleTranslator2(printFact, dest_language)
elif inp == 'ChatGPT': # ChatGPT API
print('ChatGPT API Engine is translating the text...')
printDescription = translate_text_CHATGPTAPI(printDescription, 'en', dest_language)
printFact = translate_text_CHATGPTAPI(printDescription, 'en', dest_language)
else:
print("Translations failed")
# creates the slide presentation if slide option 1 is choosen
if (slideOption == 1):
# creates the slides and sets layout preferences
slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(slide_layout)
prs.slide_width = pptx.util.Inches(8)
prs.slide_height = pptx.util.Inches(11)
# places the logo on the slide
logoHolder = slide.shapes.add_picture(basename(printLogo), pptx.util.Inches(7), pptx.util.Inches(0),
width=pptx.util.Inches(1), height=pptx.util.Inches(1))
# places the title on the slide
titleBox = slide.shapes.add_textbox(pptx.util.Inches(2.5), pptx.util.Inches(.5),
width=pptx.util.Inches(3), height=pptx.util.Inches(1))
titleBoxtf = titleBox.text_frame
title = titleBoxtf.add_paragraph()
title.text = printName
title.font.name = titleFont
title.font.size = Pt(titleSize)
# places the picture on the slide
pictureHolder = prs.slides[i].shapes
pictureHolder.add_picture(basename(printImage), pptx.util.Inches(2.5), pptx.util.Inches(2),
width=pptx.util.Inches(3), height=pptx.util.Inches(4))
# creates a textbox for the description and fun fact
contentBox = slide.shapes.add_textbox(pptx.util.Inches(1), pptx.util.Inches(6),
width=pptx.util.Inches(6), height=pptx.util.Inches(5))
contentBoxtf = contentBox.text_frame
contentBoxtf.word_wrap = True
descriptionTitle = contentBoxtf.add_paragraph()
descriptionTitle.font.name = textFont
descriptionTitle.font.bold = True
descriptionTitle.font.size = Pt(textSize)
descriptionTitle.text = "Description: "
descriptionParagraph = contentBoxtf.add_paragraph()
descriptionParagraph.font.name = textFont
descriptionParagraph.font.size = Pt(textSize)
descriptionParagraph.text = printDescription
FunFactTitle = contentBoxtf.add_paragraph()
FunFactTitle.font.name = textFont
FunFactTitle.font.bold = True
FunFactTitle.font.size = Pt(textSize)
FunFactTitle.text = "\nFun Fact:"
FunFactParagraph = contentBoxtf.add_paragraph()
FunFactParagraph.font.name = textFont
FunFactParagraph.font.size = Pt(textSize)
FunFactParagraph.text = printFact
elif (slideOption == 2):
# creates the slides and sets layout preferences
slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(slide_layout)
prs.slide_width = pptx.util.Inches(8)
prs.slide_height = pptx.util.Inches(11)
#Puts picture on new slide indicated under option 2
textSlide = prs.slides.add_slide(slide_layout)
# places the logo on the slide
logoHolder = slide.shapes.add_picture(basename(printLogo), pptx.util.Inches(7), pptx.util.Inches(0),
width=pptx.util.Inches(1), height=pptx.util.Inches(1))
# places the title on the slide
titleBox = slide.shapes.add_textbox(pptx.util.Inches(2.5), pptx.util.Inches(.5),
width=pptx.util.Inches(3), height=pptx.util.Inches(1))
titleBoxtf = titleBox.text_frame
title = titleBoxtf.add_paragraph()
title.text = printName
title.font.name = titleFont
title.font.size = Pt(titleSize)
# places the picture on the slide or on the new slide
pictureHolder = textSlide.shapes
pictureHolder.add_picture(basename(printImage), pptx.util.Inches(2.5), pptx.util.Inches(3.5),
width=pptx.util.Inches(3), height=pptx.util.Inches(4))
# creates a textbox for the description and fun fact
contentBox = slide.shapes.add_textbox(pptx.util.Inches(1), pptx.util.Inches(2),
width=pptx.util.Inches(6), height=pptx.util.Inches(5))
contentBoxtf = contentBox.text_frame
contentBoxtf.word_wrap = True
descriptionTitle = contentBoxtf.add_paragraph()
descriptionTitle.font.name = textFont
descriptionTitle.font.bold = True
descriptionTitle.font.size = Pt(textSize)
descriptionTitle.text = "Description: "
descriptionParagraph = contentBoxtf.add_paragraph()
descriptionParagraph.font.name = textFont
descriptionParagraph.font.size = Pt(textSize)
descriptionParagraph.text = printDescription
FunFactTitle = contentBoxtf.add_paragraph()
FunFactTitle.font.name = textFont
FunFactTitle.font.bold = True
FunFactTitle.font.size = Pt(textSize)
FunFactTitle.text = "\nFun Fact:"
FunFactParagraph = contentBoxtf.add_paragraph()
FunFactParagraph.font.name = textFont
FunFactParagraph.font.size = Pt(textSize)
FunFactParagraph.text = printFact
elif (slideOption == 3):
# creates the slides and sets layout preferences
slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(slide_layout)
prs.slide_width = pptx.util.Inches(8)
prs.slide_height = pptx.util.Inches(11)
# places the logo on the slide
logoHolder = slide.shapes.add_picture(basename(printLogo), pptx.util.Inches(7), pptx.util.Inches(0),
width=pptx.util.Inches(1), height=pptx.util.Inches(1))
# places the title on the slide
titleBox = slide.shapes.add_textbox(pptx.util.Inches(2.5), pptx.util.Inches(.5),
width=pptx.util.Inches(3), height=pptx.util.Inches(1))
titleBoxtf = titleBox.text_frame
title = titleBoxtf.add_paragraph()
title.text = printName
title.font.name = titleFont
title.font.size = Pt(titleSize)
# places the picture on the slide or on the new slide
pictureHolder = prs.slides[i].shapes
pictureHolder.add_picture(basename(printImage), pptx.util.Inches(2.5), pptx.util.Inches(6),
width=pptx.util.Inches(3), height=pptx.util.Inches(4))
# creates a textbox for the description and fun fact
contentBox = slide.shapes.add_textbox(pptx.util.Inches(1), pptx.util.Inches(2),
width=pptx.util.Inches(6), height=pptx.util.Inches(5))
contentBoxtf = contentBox.text_frame
contentBoxtf.word_wrap = True
descriptionTitle = contentBoxtf.add_paragraph()
descriptionTitle.font.name = textFont
descriptionTitle.font.bold = True
descriptionTitle.font.size = Pt(textSize)
descriptionTitle.text = "Description: "
descriptionParagraph = contentBoxtf.add_paragraph()
descriptionParagraph.font.name = textFont
descriptionParagraph.font.size = Pt(textSize)
descriptionParagraph.text = printDescription
FunFactTitle = contentBoxtf.add_paragraph()
FunFactTitle.font.name = textFont
FunFactTitle.font.bold = True
FunFactTitle.font.size = Pt(textSize)
FunFactTitle.text = "\nFun Fact:"
FunFactParagraph = contentBoxtf.add_paragraph()
FunFactParagraph.font.name = textFont
FunFactParagraph.font.size = Pt(textSize)
FunFactParagraph.text = printFact
# creates the slide presentation if slide option 4 is chosen
elif (slideOption == 4):
# creates slide preferences
slide_layout = prs.slide_layouts[6]
prs.slide_width = pptx.util.Inches(11)
prs.slide_height = pptx.util.Inches(8)
slide2 = prs.slides.add_slide(slide_layout)
# places picture to cover whole slide
pictureHolder = prs.slides[pictureSlide].shapes
pictureHolder.add_picture(basename(printImage), pptx.util.Inches(6), pptx.util.Inches(1),
width=pptx.util.Inches(4), height=pptx.util.Inches(6))
# creates next slide
# place logo on the slide
logoHolder = slide2.shapes.add_picture(basename(printLogo), pptx.util.Inches(5.5), pptx.util.Inches(0),
width=pptx.util.Inches(1), height=pptx.util.Inches(1))
# places the title
titleBox = slide2.shapes.add_textbox(pptx.util.Inches(4), pptx.util.Inches(1.5),
width=pptx.util.Inches(2), height=pptx.util.Inches(1))
titleBoxtf = titleBox.text_frame
title = titleBoxtf.add_paragraph()
title.text = printName
title.font.size = Pt(titleSize)
title.font.name = titleFont
# creates textbox for description and fun fact
contentBox = slide2.shapes.add_textbox(pptx.util.Inches(1), pptx.util.Inches(1),
width=pptx.util.Inches(3), height=pptx.util.Inches(4))
contentBoxtf = contentBox.text_frame
contentBoxtf.word_wrap = True
descriptionTitle = contentBoxtf.add_paragraph()
descriptionTitle.font.name = textFont
descriptionTitle.font.bold = True
descriptionTitle.font.size = Pt(textSize)
descriptionTitle.text = "Description: "
descriptionParagraph = contentBoxtf.add_paragraph()
descriptionParagraph.font.name = textFont
descriptionParagraph.font.size = Pt(textSize)
descriptionParagraph.text = printDescription
FunFactTitle = contentBoxtf.add_paragraph()
FunFactTitle.font.bold = True
FunFactTitle.font.name = textFont
FunFactTitle.font.size = Pt(textSize)
FunFactTitle.text = "\nFun Fact:"
FunFactParagraph = contentBoxtf.add_paragraph()
FunFactParagraph.font.name = textFont
FunFactParagraph.font.size = Pt(textSize)
FunFactParagraph.text = printFact
pictureSlide = pictureSlide + 1
# creates the slide presentation if slide option 5 is chosen
elif (slideOption == 5):
# creates slide preferences
slide_layout = prs.slide_layouts[6]
prs.slide_width = pptx.util.Inches(11)
prs.slide_height = pptx.util.Inches(8)
slide2 = prs.slides.add_slide(slide_layout)
# places picture to cover whole slide
pictureHolder = prs.slides[pictureSlide].shapes
pictureHolder.add_picture(basename(printImage), pptx.util.Inches(1), pptx.util.Inches(1),
width=pptx.util.Inches(4), height=pptx.util.Inches(6))
# creates next slide
# place logo on the slide
logoHolder = slide2.shapes.add_picture(basename(printLogo), pptx.util.Inches(5), pptx.util.Inches(0),
width=pptx.util.Inches(1), height=pptx.util.Inches(1))
# places the title
titleBox = slide2.shapes.add_textbox(pptx.util.Inches(4), pptx.util.Inches(1.5),
width=pptx.util.Inches(2), height=pptx.util.Inches(1))
titleBoxtf = titleBox.text_frame
title = titleBoxtf.add_paragraph()
title.text = printName
title.font.size = Pt(titleSize)
title.font.name = titleFont
# creates textbox for description and fun fact
contentBox = slide2.shapes.add_textbox(pptx.util.Inches(7), pptx.util.Inches(1),
width=pptx.util.Inches(3), height=pptx.util.Inches(4))
contentBoxtf = contentBox.text_frame
contentBoxtf.word_wrap = True
descriptionTitle = contentBoxtf.add_paragraph()
descriptionTitle.font.name = textFont
descriptionTitle.font.bold = True
descriptionTitle.font.size = Pt(textSize)
descriptionTitle.text = "Description: "
descriptionParagraph = contentBoxtf.add_paragraph()
descriptionParagraph.font.name = textFont
descriptionParagraph.font.size = Pt(textSize)
descriptionParagraph.text = printDescription
FunFactTitle = contentBoxtf.add_paragraph()
FunFactTitle.font.bold = True
FunFactTitle.font.name = textFont
FunFactTitle.font.size = Pt(textSize)
FunFactTitle.text = "\nFun Fact:"
FunFactParagraph = contentBoxtf.add_paragraph()
FunFactParagraph.font.name = textFont
FunFactParagraph.font.size = Pt(textSize)
FunFactParagraph.text = printFact
pictureSlide = pictureSlide + 1
prs.save(output_file)
return output_file
# MS-Azure API
def translate_text_Azure(text, source_language, target_language):
azure_subscription_key = "9f3df45b03754a2************" # replace with your key
azure_endpoint = "https://api.cognitive.microsofttranslator.com/"
headers = {
"Ocp-Apim-Subscription-Key": azure_subscription_key,
"Content-type": "application/json",
"Ocp-Apim-Subscription-Region": "centralus"
}
url = f"{azure_endpoint}/translate?api-version=3.0&from={source_language}&to={target_language}"
body = [{"text": text}]
response = requests.post(url, headers=headers, json=body)
if response.status_code == 200:
translated_text = response.json()[0]["translations"][0]["text"]
return translated_text
else:
print(f"Translation error: {response.text}")
return None
# GoogleTranslateAPI
def GoogleTranslator1(printDescription, dest_language):
t1 = GoogleTranslator(source='auto', target=dest_language).translate(printDescription)
return t1
def GoogleTranslator2(printFact, dest_language):
t2 = GoogleTranslator(source='auto', target=dest_language).translate(printFact)
return t2
# Function to translate text using ChatGPT API
APIKey = 'sk-r6WS4k99p0z3CUMAuAaLT3BlbkFJ*****************' # replace with your key
openai.api_key = APIKey
def translate_text_CHATGPTAPI(text, source_language, target_language):
prompt = f"Translate the following '{source_language}' text to '{target_language}': {text}"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that translates text."},
{"role": "user", "content": prompt}
],
max_tokens=150,
n=1,
stop=None,
temperature=0.5,
)
translation = response.choices[0].message.content.strip()
return translation
#DeepL API
def DeepL(printDescription):
result = (
translator.translate_text(printDescription, target_lang=dest_language_DeepL, split_sentences=1)).__str__().encode(
"utf-8")
return result
ppt_file = buildPresentation()
os.startfile(ppt_file)