This Python script allows converting a text article into a structured HTML document using the GPT-4o model from OpenAI. The script generates HTML code based on the provided article text and saves it to a file.
- Load article text from a file
- Generate structured HTML code
- Save the generated HTML code to a file
- Simple command-line interface
- Python 3.x+
- OpenAI API Key (requires an OpenAI account)
- A
.env
file containing the OpenAI API key - Installed
openai
andpython-dotenv
libraries
-
Clone or download the repository to your machine.
-
Install the required Python libraries using pip:
pip3 install -r requirements.txt
-
Create a
.env
file in the same directory as the script, with the following content:OPENAI_API_KEY=your_openai_api_key
Replace
your_openai_api_key
with your actual OpenAI API key.
-
Place the article you want to convert to HTML in a text file named
article_text.txt
. -
Run the script:
python3 main.py
-
The script will perform the following steps:
- Load the article from the specified file.
- Generate HTML code using OpenAI.
- Save the generated HTML code to a file named
article.html
. - Display a message indicating successful generation and saving of the HTML.
main()
The main function that sets up the program, loads the article, generates HTML, and saves it to a file.
configure()
Configures the OpenAI client by loading the API key from the .env
file using the python-dotenv
library.
load_article(filename)
Loads the article from the specified file.
generate_html_content(article_text)
Generates HTML from the article using the GPT-4o model. The HTML is structurally organized according to guidelines.
save_html_content(html_content, filename)
Saves the generated HTML code to the specified file.
Filip Rokita
www.filiprokita.com