Here's a simple implementation of a Txt & Html File Generator tool using Python:
import tkinter as tk
from tkinter import filedialog, messagebox
class FileGenerator:
def __init__(self, root):
self.root = root
self.root.title("Txt & Html File Generator")
# Filename
tk.Label(self.root, text="Filename:").grid(row=0, column=0, padx=5, pady=5)
self.entry_filename = tk.Entry(self.root, width=50)
self.entry_filename.grid(row=0, column=1, padx=5, pady=5)
# Title
tk.Label(self.root, text="Title (for HTML file):").grid(row=1, column=0, padx=5, pady=5)
self.entry_title = tk.Entry(self.root, width=50)
self.entry_title.grid(row=1, column=1, padx=5, pady=5)
# Content
tk.Label(self.root, text="Content:").grid(row=2, column=0, padx=5, pady=5)
self.text_content = tk.Text(self.root, width=50, height=10)
self.text_content.grid(row=2, column=1, padx=5, pady=5)
# Directory
tk.Label(self.root, text="Directory:").grid(row=3, column=0, padx=5, pady=5)
self.entry_directory = tk.Entry(self.root, width=50)
self.entry_directory.grid(row=3, column=1, padx=5, pady=5)
tk.Button(self.root, text="Browse", command=self.browse_directory).grid(row=3, column=2, padx=5, pady=5)
# Generate Files
tk.Button(self.root, text="Generate Files", command=self.generate_files).grid(row=4, column=1, padx=5, pady=5)
def browse_directory(self):
directory = filedialog.askdirectory()
if directory:
self.entry_directory.delete(0, tk.END)
self.entry_directory.insert(0, directory)
def generate_files(self):
filename = self.entry_filename.get()
title = self.entry_title.get()
content = self.text_content.get("1.0", tk.END)
directory = self.entry_directory.get()
if not filename or not title or not content or not directory:
messagebox.showerror("Error", "Please fill in all fields.")
return
try:
with open(f"{directory}/{filename}.txt", "w") as file:
file.write(content)
with open(f"{directory}/{filename}.html", "w") as file:
file.write(f"""
{title}
{title}
{content}
""")
messagebox.showinfo("Success", "Files generated successfully.")
except Exception as e:
messagebox.showerror("Error", str(e))
if __name__ == "__main__":
root = tk.Tk()
file_generator = FileGenerator(root)
root.mainloop()
This tool allows you to:
1. Enter a filename
2. Enter a title for the HTML file
3. Enter content for both files
4. Choose a directory to save the files
5. Generate both a text file and an HTML file with the given filename, title, and content.
To run this tool, save the code to a file (e.g., `file_generator.py`) and run it using Python:
python file_generator.py
No comments:
Post a Comment