本人只会python和一点前端,借鉴了个前端模板实现了下面功能。
1、读取并展示桌面(可自定义指定文件夹)的所有文件夹下面一层的文件及文件夹已经软件图标并展示
2、点击图标可直接打开对应软件或文件夹
第一次执行可能有点慢,需要转换图标到当前目录下用于读取。可自己修改一些默认文件图标,如文件夹,word,excel等,详细内容见压缩包
缺点:只能用浏览器展示
源码:
[Python] 纯文本查看 复制代码from flask import Flask, render_template_string, request, jsonify, url_for
import subprocess
import win32com.client
import pythoncom
import configparser
import os
app = Flask(__name__)
@app.route('/open-folder', methods=['POST'])
def open_folder():
data = request.get_json()
folder_path = data.get('path')
if not folder_path:
return jsonify({"success": False, "error": "未提供路径"}), 400
try:
# 使用 Windows 资源管理器打开文件夹
subprocess.Popen(f'explorer "{folder_path}"')
return jsonify({"success": True})
except Exception as e:
return jsonify({"success": False, "error": str(e)})
def html(data):
# 包含 HTML、CSS 和 JS 的完整页面字符串
html_content = f"""
图标管理工具
:root {{
--primary-color: #2563eb;
--hover-color: #1d4ed8;
--background: #f8fafc;
}}
body {{
font-family: 'Segoe UI', system-ui, sans-serif;
background-color: var(--background);
margin: 0;
padding: 20px;
color: #334155;
}}
.container {{
max-width: 1200px;
margin: 0 auto;
}}
header {{
text-align: center;
margin-bottom: 40px;
}}
h1 {{
color: var(--primary-color);
font-size: 2.5rem;
margin: 20px 0;
}}
.search-box {{
max-width: 600px;
margin: 20px auto;
position: relative;
}}
.search-input {{
width: 100%;
padding: 12px 20px;
border: 2px solid #e2e8f0;
border-radius: 30px;
font-size: 16px;
transition: all 0.3s;
}}
.search-input:focus {{
outline: none;
border-color: var(--primary-color);
box-shadow: 0 0 10px rgba(37, 99, 235, 0.1);
}}
.tools-grid {{
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 20px;
}}
.tool-card {{
background: white;
border-radius: 12px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
transition: transform 0.2s, box-shadow 0.2s;
display: flex;
align-items: center;
gap: 15px;
text-decoration: none;
color: inherit;
}}
.tool-card:hover {{
transform: translateY(-3px);
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
}}
.tool-icon {{
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
background: #eff6ff;
border-radius: 8px;
color: var(--primary-color);
}}
.tool-text {{
flex: 1;
}}
.tool-title {{
font-weight: 600;
margin-bottom: 5px;
}}
.tool-desc {{
font-size: 0.9em;
color: #64748b;
}}
.category-section {{
margin: 40px 0;
}}
.category-title {{
color: var(--primary-color);
border-bottom: 2px solid #e2e8f0;
padding-bottom: 10px;
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 10px;
}}
.category-icon {{
font-size: 1.2em;
}}
.category-section.hidden {{
display: none;
}}
@media (max-width: 768px) {{
.tools-grid {{
grid-template-columns: 1fr;
}}
}}
图标管理工具
{data}
"""
return html_content
def get_icon():
# 获取当前工作目录下的 icons 目录路径
icons_dir = os.path.join(os.getcwd(), "static/icons")
# 判断目录是否存在且是一个文件夹
if not os.path.exists(icons_dir) or not os.path.isdir(icons_dir):
print(f"目录不存在: {icons_dir}")
return []
# 遍历目录,筛选出文件(排除子目录)
file_list = []
for filename in os.listdir(icons_dir):
file_path = os.path.join(icons_dir, filename)
if os.path.isfile(file_path):
file_list.append(filename.replace(".ico", ""))
return file_list
def create_icon(file_paths, icon_list):
for i in file_paths:
icon_path = i.replace('\\', '\\\\')
icon_name = os.path.basename(i)
if icon_name.replace(".exe", "") in icon_list:
continue
if icon_path.endswith(".exe") and icon_name not in ["pycdas.exe", "pycdc.exe", "upx.exe"]:
command = f'icoextract "{icon_path}" ".\\static\\icons\\{icon_name.replace(".exe", ".ico")}"'
subprocess.Popen(command)
def get_html_content():
config = configparser.ConfigParser()
# 读取配置文件
config.read('setting.ini')
dir_path = config['DESKTOP_PATH']['PATH']
dir_result = get_subdirectories(dir_path)
out_section = ""
icon_list = get_icon()
for i in range(len(dir_result)):
out_app_section = ""
app_result, app_result_ini = get_direct_files_and_dirs(dir_result)
create_icon(app_result, icon_list)
section_name = dir_result.split('\\')[-1]
for j in range(len(app_result)):
app_name_ini = app_result_ini[j].split('\\')[-1]
app_name = app_result[j].split('\\')[-1]
app_path = app_result[j].replace('\\', '\\\\')
if app_name.endswith(".exe"):
ico_path = url_for('static', filename=f'icons/{app_name.replace(".exe", ".ico")}') # 浏览器可访问的 URL 路径
elif app_name.endswith(".xls") or app_name.endswith(".xlsx"):
ico_path = url_for('static', filename='default_icons/excel.ico')
elif app_name.endswith(".doc") or app_name.endswith(".docx"):
ico_path = url_for('static', filename='default_icons/word.ico')
elif app_name.endswith(".ppt") or app_name.endswith(".pptx"):
ico_path = url_for('static', filename='default_icons/ppt.ico')
elif app_name.endswith(".pdf"):
ico_path = url_for('static', filename='default_icons/pdf.ico')
elif app_name.endswith(".txt"):
ico_path = url_for('static', filename='default_icons/txt.ico')
elif '.' not in app_name:
ico_path = url_for('static', filename='default_icons/dir.ico')
else:
ico_path = url_for('static', filename='default_icons/other.ico')
app_section = f"""
[img][/img]
{app_name_ini}
"""
out_app_section += app_section
category_section = f"""
{section_name}
{out_app_section}
\n
"""
out_section = out_section + category_section
html_content = html(out_section)
return html_content
@app.route('/')
def home():
html_content = get_html_content()
return render_template_string(html_content)
def get_subdirectories(directory):
sub_dirs = []
for name in os.listdir(directory):
full_path = os.path.join(directory, name)
if os.path.isdir(full_path):
sub_dirs.append(full_path)
return sub_dirs
def get_direct_files_and_dirs(directory):
result = []
result_ini = []
pythoncom.CoInitialize()
try:
shell = win32com.client.Dispatch("WScript.Shell")
except Exception as e:
print(f"创建 WScript.Shell 失败: {e}")
return result
for name in os.listdir(directory):
full_path = os.path.join(directory, name)
if os.path.isfile(full_path) and name.lower().endswith(".lnk"):
try:
shortcut = shell.CreateShortcut(full_path)
target_path = shortcut.TargetPath
if target_path and os.path.exists(target_path): # 确保路径存在
result.append(target_path)
except Exception as e:
print(f"解析快捷方式失败: {full_path} - {e}")
else:
result.append(full_path)
result_ini.append(full_path)
pythoncom.CoUninitialize()
return result, result_ini
if __name__ == '__main__':
app.run(debug=True)