Advertisement
Python253

text2html

Mar 15th, 2024
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: text2html.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts a text file (.txt) to an HTML file (.html).
  10. It wraps the text content in an HTML body with a paragraph tag.
  11.  
  12. Requirements:
  13. - Python 3.x
  14.  
  15. Usage:
  16. 1. Save this script as 'text2html.py'.
  17. 2. Ensure your text file ('example.txt') is in the same directory as the script.
  18. 3. Run the script using the command: 'python text2html.py'
  19. 4. The converted HTML file ('text2html.html') will be generated in the same directory.
  20.  
  21. Note: Adjust the 'text_file' and 'html_file' variables in the script as needed.
  22. """
  23.  
  24. def text_to_html(text_file, html_file):
  25.     with open(text_file, 'r') as textfile:
  26.         text_data = textfile.read()
  27.         html_data = f'<html><body><p>{text_data}</p></body></html>'
  28.  
  29.     with open(html_file, 'w') as htmlfile:
  30.         htmlfile.write(html_data)
  31.  
  32. if __name__ == "__main__":
  33.     # Set the filenames for the text and HTML files
  34.     text_file = 'example.txt'
  35.     html_file = 'text2html.html'
  36.  
  37.     # Convert the text to an HTML file
  38.     text_to_html(text_file, html_file)
  39.  
  40.     print(f"Converted '{text_file}' to '{html_file}'.")
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement