HTMLDocumentGenerator
The HTMLDocumentGenerator is a Python class that allows you to easily generate HTML documents with customizable tags and content. It provides a simple and intuitive way to create HTML documents for websites or other applications.
Features
- Add HTML tags with custom tag names, attributes, and content
- Set attributes for the
<head>tag, such as title, style, etc. - Generate a complete HTML document with the
<!DOCTYPE>,<html>,<head>, and<body>tags - Generate a string representation of the HTML document
- NEW FEATURE: Write the generated HTML document to an HTML file using the
openfunction
Example Usage
from html_document_generator import HTMLDocumentGenerator, HTMLTagGenerator
# Create an instance of HTMLDocumentGenerator
doc_generator = HTMLDocumentGenerator()
doc_generator.set_attribute('head', 'title', 'My Website')
# Generate the header tag
header_tag = HTMLTagGenerator(tag_name='header', tag_content='<h1>Welcome to My Website</h1>')
doc_generator.add_tag(header_tag)
# Generate the nav tag
nav_tag = HTMLTagGenerator(tag_name='nav', tag_content='<ul><li>Home</li><li>Contact</li></ul>')
doc_generator.add_tag(nav_tag)
# Generate the main tag
main_tag = HTMLTagGenerator(tag_name='main', tag_content='<p>Explore the content of my website!</p>')
doc_generator.add_tag(main_tag)
# Generate the footer tag
footer_tag = HTMLTagGenerator(tag_name='footer', tag_content='<p>Contact information: email@example.com, Phone: 123-456-7890</p>')
doc_generator.add_tag(footer_tag)
# Generate the complete HTML document
html_doc = doc_generator.generate_document()
# Write the generated HTML document to an HTML file
with open('index.html', 'w') as file:
file.write(html_doc)
print('HTML file created successfully!')
Exemple 2
from html_document_generator import HTMLDocumentGenerator, HTMLTagGenerator
# Define the content for the home page
header_content = '<h1>Welcome to Farmer\'s Farm</h1>'
nav_content = '<ul><li>Home</li><li>Contact</li></ul>'
main_content = '<p>Explore the freshest produce from our farm!</p>'
footer_content = '<p>Contact information: Email: info@farmersfarm.com, Phone: 123-456-7890</p>'
# Create an instance of HTMLDocumentGenerator for home page
home_doc_generator = HTMLDocumentGenerator()
home_doc_generator.set_attribute('head', 'title', 'Home | Farmer\'s Farm')
home_doc_generator.set_style('body { background-color: lightgreen; }')
# Generate the header tag for home page
header_tag = HTMLTagGenerator(tag_name='header', tag_content=header_content)
home_doc_generator.add_tag(header_tag)
# Generate the nav tag for home page
nav_tag = HTMLTagGenerator(tag_name='nav', tag_content=nav_content)
home_doc_generator.add_tag(nav_tag)
# Generate the main tag for home page
main_tag = HTMLTagGenerator(tag_name='main', tag_content=main_content)
home_doc_generator.add_tag(main_tag)
# Generate the footer tag for home page
footer_tag = HTMLTagGenerator(tag_name='footer', tag_content=footer_content)
home_doc_generator.add_tag(footer_tag)
# Define the content for the contact page
contact_header_content = '<h1>Contact Us</h1>'
contact_form_content = '<form action="/submit" method="post">' \
'<label for="name">Name:</label>' \
'<input type="text" id="name" name="name" required>' \
'<label for="email">Email:</label>' \
'<input type="email" id="email" name="email" required>' \
'<label for="message">Message:</label>' \
'<textarea id="message" name="message" required></textarea>' \
'<input type="submit" value="Submit">' \
'</form>'
contact_footer_content = '<p>Contact information: Email: info@farmersfarm.com, Phone: 123-456-7890</p>'
# Create an instance of HTMLDocumentGenerator for contact page
contact_doc_generator = HTMLDocumentGenerator()
contact_doc_generator.set_attribute('head', 'title', 'Contact | Farmer\'s Farm')
contact_doc_generator.set_style('body { background-color: lightblue; }')
# Generate the header tag for contact page
contact_header_tag = HTMLTagGenerator(tag_name='header', tag_content=contact_header_content)
contact_doc_generator.add_tag(contact_header_tag)
# Generate the nav tag for contact page
contact_nav_tag = HTMLTagGenerator(tag_name='nav', tag_content=nav_content)
contact_doc_generator.add_tag(contact_nav_tag)
# Generate the main tag for contact page
contact_main_tag = HTMLTagGenerator(tag_name='main', tag_content=contact_form_content)
contact_doc_generator.add_tag(contact_main_tag)
# Generate the footer tag for contact page
contact_footer_tag = HTMLTagGenerator(tag_name='footer', tag_content=contact_footer_content)
contact_doc_generator.add_tag(contact_footer_tag)
# Write the generated HTML document to an HTML file
with open('home.html', 'w') as file:
file.write(home_doc_generator.generate_document())
print('HTML file created successfully')
file.close()
with open('contact.html', 'w') as file:
file.write(contact_doc_generator.generate_document())
print('HTML file created successfully')
file.close()
Requirements
Python 3.6+ No external dependencies License This class is released under the MIT License.
Contributions
Contributions are welcome! Please feel free to open issues or submit pull requests to improve this class.
Metadata
Release files for HTMLDocumentGenerator 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| HTMLDocumentGenerator-1.0.3.tar.gz | 5.3 kB | Without provenance | Details for HTMLDocumentGenerator-1.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| HTMLDocumentGenerator-1.0.3-py3-none-any.whl | Python 3 | none | any | Details for HTMLDocumentGenerator-1.0.3-py3-none-any.whl |
Total release size: 11.8 kB
Release files / HTMLDocumentGenerator-1.0.3.tar.gz
| Download URL | HTMLDocumentGenerator-1.0.3.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e0470a104d84db1343e1ad79a313cf323f3a165de2813e21648c82b977369d2a
|
|
BLAKE2b-256 checksum How to use checksums |
0b2578baffb217a1bab1d1582a3eea73584f1d9ec22bbe2ba060c75c057dd363
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.11
|
Release files / HTMLDocumentGenerator-1.0.3-py3-none-any.whl
| Download URL | HTMLDocumentGenerator-1.0.3-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9dc3480871562ab3cfda5852eace71df57d06fe46dab7a46743831d5b456ad66
|
|
BLAKE2b-256 checksum How to use checksums |
c0e1a88b15afd739f37d8ed7c608ef7f36a0780436b041e82c4114fbd08b1a90
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.11
|
Security for HTMLDocumentGenerator 1.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.