Advertisement
nicuf

Convert Doc and Docx to PDF from the same Folder

Mar 21st, 2022
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. -----------------
  2. EXPLANATION:
  3.  
  4. ENGLISH: https://neculaifantanaru.com/en/python-full-code-how-to-convert-doc-and-docx-files-to-pdf-from-the-folder.html
  5.  
  6. ROMANIAN: https://neculaifantanaru.com/python-full-code-how-to-convert-doc-and-docx-files-to-pdf-from-the-folder.html
  7. -----------------
  8.  
  9. #-------------------------------------------------------------------------------
  10. # Name:        Convert Doc and DocX to PDF
  11. #
  12. #
  13. # Author:      Fantanaru Neculai
  14. #
  15. # Created:     18/03/2022
  16. # Copyright:   (c) Fantanaru Neculai 2022
  17. #-------------------------------------------------------------------------------
  18.  
  19.  
  20. # pip install pywin32
  21. # pip install docx2txt
  22.  
  23. import docx2txt
  24. from win32com import client
  25.  
  26. import os
  27.  
  28. files_from_folder = r"c:\\doc"
  29.  
  30. directory = os.fsencode(files_from_folder)
  31.  
  32. amount = 1
  33.  
  34. word = client.DispatchEx("Word.Application")
  35. word.Visible = True
  36.  
  37. for file in os.listdir(directory):
  38.     filename = os.fsdecode(file)
  39.     print(filename)
  40.  
  41.     if filename.endswith('docx'):
  42.         text = docx2txt.process(os.path.join(files_from_folder, filename))
  43.  
  44.         print(f'{filename} transfered ({amount})')
  45.         amount += 1
  46.         new_filename = filename.split('.')[0] + '.txt'
  47.  
  48.         try:
  49.             with open(os.path.join(files_from_folder + r'\txt_files', new_filename), 'w', encoding='utf-8') as t:
  50.                 t.write(text)
  51.         except:
  52.             os.mkdir(files_from_folder + r'\txt_files')
  53.             with open(os.path.join(files_from_folder + r'\txt_files', new_filename), 'w', encoding='utf-8') as t:
  54.                 t.write(text)
  55.     elif filename.endswith('doc'):
  56.         doc = word.Documents.Open(os.path.join(files_from_folder, filename))
  57.         text = doc.Range().Text
  58.         doc.Close()
  59.  
  60.         print(f'{filename} transfered ({amount})')
  61.         amount += 1
  62.         new_filename = filename.split('.')[0] + '.txt'
  63.  
  64.         try:
  65.             with open(os.path.join(files_from_folder + r'\txt_files', new_filename), 'w', encoding='utf-8') as t:
  66.                 t.write(text)
  67.         except:
  68.             os.mkdir(files_from_folder + r'\txt_files')
  69.             with open(os.path.join(files_from_folder + r'\txt_files', new_filename), 'w', encoding='utf-8') as t:
  70.                 t.write(text)
  71. word.Quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement