Advertisement
nicuf

Convert txt to docx

Feb 12th, 2023
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. ---------------
  2. ROMANIAN: https://neculaifantanaru.com/python-how-to-convert-txt-files-to-word-docx.html
  3. ENGLISH: https://neculaifantanaru.com/en/python-how-to-convert-txt-files-to-word-docx.html
  4. ---------------
  5.  
  6. # pachetul docx nu merge cu python 3
  7. # pip install docx
  8. # Ruleaza urmatoarea comanda
  9. # pip install docx
  10. # Apoi instaleaza
  11. # pip install python-docx
  12. # E nevoie si de urmatoarele pachete
  13. # pip install pathlib
  14.  
  15. import re
  16. import os
  17. from pathlib import Path
  18. import sys
  19. from docx import Document
  20.  
  21. # Locatia unde se afla fisierele
  22. input_path = r'c:\Folder7\input'
  23. # Locatia unde vom scrie fisierele docx
  24. output_path = r'c:\Folder7\output'
  25. # Creeaza structura de foldere daca nu exista
  26. os.makedirs(output_path, exist_ok=True)
  27.  
  28. # Verifica existenta folder-ului
  29. directory_path = Path(input_path)
  30. if directory_path.exists() and directory_path.is_dir():
  31.     print(directory_path, "exists")
  32. else:
  33.     print(directory_path, "is invalid")
  34.     sys.exit(1)
  35.  
  36. for file_path in directory_path.glob("*"):
  37.     # file_path is a Path object
  38.  
  39.     print("Procesez fisierul:", file_path)
  40.     document = Document()
  41.     # file_path.name is the name of the file as str without the Path
  42.     document.add_heading(file_path.name, 0)
  43.  
  44.     # remove all non-XML-compatible characters
  45.     #file_content = re.sub(r"[^\x00-\x7F]+|\x0c", " ", file_path.read_text(encoding='UTF-8'))
  46.     file_content = file_path.read_text(encoding='UTF-8')
  47.     document.add_paragraph(file_content)
  48.  
  49.     # build the new path where we store the files
  50.     output_file_path = os.path.join(output_path, file_path.name + ".docx")
  51.  
  52.     document.save(output_file_path)
  53.     print("Am convertit urmatorul fisier:", file_path, "in: ", output_file_path)
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement