Text to HTML Converter - Python Programming Exercise

In this exercise, you will develop a Python program that functions as a "Text to HTML converter". This exercise is perfect for practicing file handling, string manipulation, and HTML generation in Python. By implementing this program, you will gain hands-on experience in handling file operations, string manipulation, and HTML generation in Python. This exercise not only reinforces your understanding of file handling but also helps you develop efficient coding practices for managing user interactions.

 Category

Managing Files

 Exercise

Text To HTML Converter

 Objective

Develop a Python program that functions as a "Text to HTML converter". This program will read the contents of a given text file and generate an HTML file based on it. For example, if the text file contains:

Hello
It's Me
I finished

The output HTML file should have the same name as the source file but with a ".html" extension (replacing the ".txt" if applicable). The "title" tag in the HTML's "head" section should be populated with the name of the source file.

 Example Python Exercise

 Copy Python Code
# This program converts a text file to an HTML file with the same name but with a ".html" extension.

def text_to_html(input_filename):
    try:
        # Read the contents of the text file
        with open(input_filename, 'r') as file:
            text_content = file.readlines()

        # Generate the output HTML filename by replacing ".txt" with ".html"
        output_filename = input_filename.replace(".txt", ".html")

        # Open the output HTML file in write mode
        with open(output_filename, 'w') as html_file:
            # Write the HTML structure
            html_file.write('\n')
            html_file.write('\n')
            html_file.write('\n')
            html_file.write(f'\t{input_filename}\n')  # Set title as the input file name
            html_file.write('\n')
            html_file.write('\n')

            # Write each line from the text file as a paragraph in the HTML file
            for line in text_content:
                html_file.write(f'\t

{line.strip()}

\n') html_file.write('\n') html_file.write('\n') print(f"HTML file generated: {output_filename}") except FileNotFoundError: print("The specified file does not exist.") except Exception as e: print(f"An error occurred: {e}") # Example usage input_file = "example.txt" # Replace with your text file name text_to_html(input_file)

 Output




    example.txt


    

Hello

It's Me

I finished

 Share this Python Exercise

 More Python Programming Exercises of Managing Files

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Reverse Binary File V2

    In this exercise, you will develop a Python program to "reverse" a binary file using a "FileStream". This exercise is perfect for practicing file handling, byt...

  •  BMP Dimensions, Using FileStream

    In this exercise, you will develop a Python program to display the width and height of a BMP image file using a FileStream. This exercise is perfect for practi...

  •  File duplicator

    In this exercise, you will develop a Python program that duplicates a source file to a target file using FileStream and processes the file in 512 KB blocks. This e...

  •  MP3 file reader

    In this exercise, you will develop a Python program to read the ID3 tags from an MP3 file. This exercise is perfect for practicing file handling, byte manipula...

  •  File Divider

    In this exercise, you will develop a Python program to divide a file (of any type) into smaller segments of a specified size. This exercise is perfect for prac...

  •  BMP File Encryption

    In this exercise, you will develop a Python program to encrypt or decrypt a BMP image file by swapping the "BM" signature in the first two bytes with "MB" and vice ve...