March 21

скрипт створює список гіперпосилань, які пов'язані з певними файлами у папці 

import os
import tkinter as tk
from tkinter import filedialog
import pathlib

def create_hyperlinks_html():
# Create a simple GUI to ask for directory path
root = tk.Tk()
root.withdraw() # Hide the main window

print("Please select the folder containing the files:")
folder_path = filedialog.askdirectory(title="Select Folder")

if not folder_path:
print("No folder selected. Exiting.")
return

# Get a list of all files in the directory
files = sorted([f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))])

# Create HTML content
html_content = """<!DOCTYPE html>
<html>
<head>
<title>Files in Selected Folder</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 30px;
}
h1 {
text-align: center;
color: #333;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin: 10px 0;
}
a {
color: #0066cc;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Files in Selected Folder</h1>
<ul>
"""

if not files:
html_content += " <li>No files found in the selected folder.</li>\n"
else:
# Add each file as a hyperlink
for file in files:
# Create full file path
file_path = os.path.join(folder_path, file)

# Convert to URI format for the hyperlink
uri_path = pathlib.Path(file_path).as_uri()

# Add a list item with the hyperlink
html_content += f' <li><a href="{uri_path}">{file}</a></li>\n'

html_content += """ </ul>
</body>
</html>"""

# Save the HTML file
output_path = os.path.join(folder_path, "FileLinks.html")
with open(output_path, 'w', encoding='utf-8') as f:
f.write(html_content)

print(f"HTML document created successfully: {output_path}")

if __name__ == "__main__":
try:
create_hyperlinks_html()
except Exception as e:
print(f"An error occurred: {e}")