Bentham commited on
Commit
c262799
·
verified ·
1 Parent(s): 4accb2f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +26 -9
main.py CHANGED
@@ -60,8 +60,7 @@ FORMAT_MAP = {
60
  '.commonmark': 'commonmark',
61
  '.cm': 'commonmark',
62
  '.wiki': 'mediawiki',
63
- '.opml': 'opml',
64
- '.ppt': 'pptx'
65
  }
66
 
67
  ALLOWED_EXTENSIONS_FOR_ACCESSIBILITY = list(FORMAT_MAP.keys()) + ['.doc', '.ppt', '.pptx']
@@ -296,8 +295,6 @@ def pdf_to_html(input_filename: str) -> str:
296
  return str(soup)
297
 
298
  def convert_with_pandoc(input_filename: str, input_format: str) -> str:
299
- # On force l'utilisation de --self-contained pour les docx, afin d'inclure les images.
300
- # Pour les autres formats, on garde la logique de fallback.
301
  if input_format == 'docx':
302
  try:
303
  output = pypandoc.convert_file(
@@ -309,11 +306,33 @@ def convert_with_pandoc(input_filename: str, input_format: str) -> str:
309
  )
310
  return output
311
  except RuntimeError as e:
312
- # Si on ne peut pas faire --self-contained pour docx, on lève une erreur car sinon pas d'images.
313
  logging.error(f"Pandoc a rencontré une erreur avec --self-contained sur un docx : {str(e)}")
314
  raise RuntimeError("Impossible de convertir le docx avec --self-contained. Les images ne peuvent pas être traitées.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
  else:
316
- # Comportement inchangé pour les autres formats
317
  try:
318
  output = pypandoc.convert_file(
319
  input_filename,
@@ -334,7 +353,6 @@ def convert_with_pandoc(input_filename: str, input_format: str) -> str:
334
  )
335
  return output
336
 
337
-
338
  def text_to_html(text: str) -> str:
339
  lines = text.split('\n')
340
  html_lines = ['<p>' + line.strip() + '</p>' for line in lines if line.strip()]
@@ -786,9 +804,8 @@ async def convert_file_to_txt(
786
  elif ext == '.pptx':
787
  html_content = convert_pptx_to_html(input_filename)
788
  elif ext == '.ppt':
789
- input_format = get_pandoc_format(ext)
790
  try:
791
- html_content = convert_with_pandoc(input_filename, input_format)
792
  except Exception as e:
793
  logging.error(f"Erreur lors de la conversion de .ppt avec pypandoc: {e}")
794
  raise HTTPException(status_code=500, detail=f"Erreur lors de la conversion du fichier .ppt: {e}")
 
60
  '.commonmark': 'commonmark',
61
  '.cm': 'commonmark',
62
  '.wiki': 'mediawiki',
63
+ '.opml': 'opml'
 
64
  }
65
 
66
  ALLOWED_EXTENSIONS_FOR_ACCESSIBILITY = list(FORMAT_MAP.keys()) + ['.doc', '.ppt', '.pptx']
 
295
  return str(soup)
296
 
297
  def convert_with_pandoc(input_filename: str, input_format: str) -> str:
 
 
298
  if input_format == 'docx':
299
  try:
300
  output = pypandoc.convert_file(
 
306
  )
307
  return output
308
  except RuntimeError as e:
 
309
  logging.error(f"Pandoc a rencontré une erreur avec --self-contained sur un docx : {str(e)}")
310
  raise RuntimeError("Impossible de convertir le docx avec --self-contained. Les images ne peuvent pas être traitées.")
311
+ elif os.path.splitext(input_filename)[1].lower() == '.ppt':
312
+ try:
313
+ output = pypandoc.convert_file(
314
+ input_filename,
315
+ 'html',
316
+ format='auto',
317
+ outputfile=None,
318
+ extra_args=['--strip-comments', '--quiet']
319
+ )
320
+ return output
321
+ except RuntimeError as e:
322
+ logging.error(f"Pandoc a rencontré une erreur avec le format 'auto' sur un ppt : {str(e)}, tentative avec 'ppt'.")
323
+ try:
324
+ output = pypandoc.convert_file(
325
+ input_filename,
326
+ 'html',
327
+ format='ppt',
328
+ outputfile=None,
329
+ extra_args=['--strip-comments', '--quiet']
330
+ )
331
+ return output
332
+ except RuntimeError as e:
333
+ logging.error(f"Pandoc a rencontré une erreur avec le format 'ppt' sur un ppt : {str(e)}.")
334
+ raise
335
  else:
 
336
  try:
337
  output = pypandoc.convert_file(
338
  input_filename,
 
353
  )
354
  return output
355
 
 
356
  def text_to_html(text: str) -> str:
357
  lines = text.split('\n')
358
  html_lines = ['<p>' + line.strip() + '</p>' for line in lines if line.strip()]
 
804
  elif ext == '.pptx':
805
  html_content = convert_pptx_to_html(input_filename)
806
  elif ext == '.ppt':
 
807
  try:
808
+ html_content = convert_with_pandoc(input_filename, get_pandoc_format(ext))
809
  except Exception as e:
810
  logging.error(f"Erreur lors de la conversion de .ppt avec pypandoc: {e}")
811
  raise HTTPException(status_code=500, detail=f"Erreur lors de la conversion du fichier .ppt: {e}")