Adjoumani commited on
Commit
47dc2d9
·
verified ·
1 Parent(s): f531db6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -58,7 +58,8 @@ if doc_file and st.button("Lancer la traduction"):
58
 
59
  # Initialisation de la traduction
60
  translator = GoogleTranslator(source=source_language, target=target_language)
61
-
 
62
  # Création d'un groupe de calques pour le texte traduit
63
  ocg_xref = doc.add_ocg(f"translated-{target_language}", on=True)
64
 
@@ -68,7 +69,7 @@ if doc_file and st.button("Lancer la traduction"):
68
  start_time = time.time()
69
 
70
  for page_num, page in enumerate(doc):
71
- blocks = page.get_text("blocks")
72
 
73
  for block in blocks:
74
  bbox = block[:4]
@@ -78,12 +79,24 @@ if doc_file and st.button("Lancer la traduction"):
78
  translated_text = translator.translate(english_text)
79
 
80
  # Remplacement du texte
81
- page.draw_rect(bbox, color=None, fill=fitz.pdfcolor["white"], oc=ocg_xref)
82
- page.insert_textbox(
83
- bbox,
84
- translated_text,
85
- fontsize=12,
86
- color=fitz.pdfcolor["black"],
 
 
 
 
 
 
 
 
 
 
 
 
87
  oc=ocg_xref,
88
  )
89
 
 
58
 
59
  # Initialisation de la traduction
60
  translator = GoogleTranslator(source=source_language, target=target_language)
61
+ blanc = fitz.pdfcolor["white"]
62
+ textflags = fitz.TEXT_DEHYPHENATE
63
  # Création d'un groupe de calques pour le texte traduit
64
  ocg_xref = doc.add_ocg(f"translated-{target_language}", on=True)
65
 
 
69
  start_time = time.time()
70
 
71
  for page_num, page in enumerate(doc):
72
+ blocks = page.get_text("blocks", flags=textflags)
73
 
74
  for block in blocks:
75
  bbox = block[:4]
 
79
  translated_text = translator.translate(english_text)
80
 
81
  # Remplacement du texte
82
+ page.draw_rect(bbox, color=None, fill=blanc, oc=ocg_xref)
83
+
84
+ # Correction : s'assurer que french est une chaîne de caractères
85
+ if isinstance(translated_text, list):
86
+ translated_text = " ".join(translated_text) # Joint les éléments de la liste en une seule chaîne
87
+ elif translated_text is None: # Gère le cas où la traduction est None
88
+ translated_text = "" # Remplace par une chaîne vide
89
+ #page.insert_textbox(
90
+ # bbox,
91
+ # translated_text,
92
+ # fontsize=12,
93
+ # color=fitz.pdfcolor["black"],
94
+ # oc=ocg_xref,
95
+ #)
96
+
97
+ page.insert_htmlbox(
98
+ bbox,
99
+ translated_text,
100
  oc=ocg_xref,
101
  )
102