gauravchand11 commited on
Commit
ee3825b
·
verified ·
1 Parent(s): ee20cf3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -6
app.py CHANGED
@@ -322,24 +322,40 @@ def translate_document(file, source_lang, target_lang):
322
  temp_dir = tempfile.gettempdir()
323
  output_path = os.path.join(temp_dir, output_filename)
324
 
325
- # Create appropriate file format
326
  if input_ext == '.pdf':
327
  # Save as txt file
328
  with open(output_path, 'w', encoding='utf-8') as f:
329
  f.write(translated_text)
330
 
331
  elif input_ext == '.docx':
332
- # Create DOCX
333
  doc = Document()
 
 
334
  for paragraph in translated_text.split('\n'):
335
  if paragraph.strip():
336
- doc.add_paragraph(paragraph)
 
 
 
 
 
 
 
337
  doc.save(output_path)
338
 
339
- else:
340
- # Default to txt
 
 
 
 
 
 
 
341
  with open(output_path, 'w', encoding='utf-8') as f:
342
- f.write(translated_text)
343
 
344
  return translated_text, output_path
345
 
 
322
  temp_dir = tempfile.gettempdir()
323
  output_path = os.path.join(temp_dir, output_filename)
324
 
325
+ # Create appropriate file format with formatting
326
  if input_ext == '.pdf':
327
  # Save as txt file
328
  with open(output_path, 'w', encoding='utf-8') as f:
329
  f.write(translated_text)
330
 
331
  elif input_ext == '.docx':
332
+ # Create DOCX with formatting
333
  doc = Document()
334
+
335
+ # Add paragraphs with spacing
336
  for paragraph in translated_text.split('\n'):
337
  if paragraph.strip():
338
+ p = doc.add_paragraph(paragraph)
339
+ # Add spacing after each paragraph (12 points = 1 line)
340
+ p.paragraph_format.space_after = docx.shared.Pt(12)
341
+ else:
342
+ # Add empty paragraph for extra line spacing
343
+ p = doc.add_paragraph()
344
+ p.paragraph_format.space_after = docx.shared.Pt(18) # 1.5 lines
345
+
346
  doc.save(output_path)
347
 
348
+ else: # .txt file
349
+ # Save as txt with extra line breaks for spacing
350
+ formatted_text = ""
351
+ for line in translated_text.split('\n'):
352
+ if line.strip():
353
+ formatted_text += line + "\n"
354
+ if not line.endswith('\n'):
355
+ formatted_text += "\n" # Add extra line break for spacing
356
+
357
  with open(output_path, 'w', encoding='utf-8') as f:
358
+ f.write(formatted_text)
359
 
360
  return translated_text, output_path
361