如何通过网页控制Windows本地Python家谱编辑程序?
实现本地网页与Python程序的交互:点击家谱人物触发编辑
针对你的需求,这里提供几种可行的方案,让本地网页点击人物方块后,Python程序能识别并启动编辑流程:
一、搭建本地HTTP服务器(最推荐)
通过Python启动一个轻量HTTP服务,网页点击人物时向服务发送请求,Python接收后识别目标人物ID,触发编辑逻辑。
步骤1:修改HTML代码
给每个人物的div添加点击事件,发送请求到本地服务器并携带人物ID(从class名person-3-6中提取3-6作为唯一标识):
<style> .person-3-6 { position: absolute; top: 1420px; left: 3755px; width: 150px; height: 70px; cursor: pointer; /* 添加上手型光标,提示可点击 */ } .person-3-7 { position: absolute; top: 1420px; left: 5020px; width: 150px; height: 70px; cursor: pointer; } </style> <body> <div class='person-3-6' onclick="fetch('http://localhost:8000/edit?person_id=3-6')"> John Christy<br><br><br>1845 - 1930 <a href='Root/Mother/Mother/Father/index.html'><span class='link'></span></a> </div> <div class='person-3-7' onclick="fetch('http://localhost:8000/edit?person_id=3-7')"> Ann Smith<br><br><br>1851 - 1924 <a href='Root\Mother\Mother\Mother/index.html'><span class='link'></span></a> </div> </body>
步骤2:编写Python服务器
方案A:用Flask(需要安装pip install flask)
from flask import Flask, request app = Flask(__name__) @app.route('/edit') def handle_edit(): person_id = request.args.get('person_id') # 这里替换成你的编辑逻辑,比如加载人物数据、打开编辑界面 print(f"开始编辑人物:{person_id}") return "编辑请求已接收" if __name__ == '__main__': # 启动本地服务器,仅允许本机访问 app.run(host='localhost', port=8000, debug=False)
方案B:用Python内置http.server(无需额外依赖)
from http.server import BaseHTTPRequestHandler, HTTPServer import urllib.parse class EditRequestHandler(BaseHTTPRequestHandler): def do_GET(self): parsed_path = urllib.parse.urlparse(self.path) if parsed_path.path == '/edit': query_params = urllib.parse.parse_qs(parsed_path.query) person_id = query_params.get('person_id', [''])[0] # 执行编辑逻辑 print(f"开始编辑人物:{person_id}") # 返回响应给网页 self.send_response(200) self.end_headers() self.wfile.write(b"编辑请求已接收") else: self.send_response(404) self.end_headers() def run_server(port=8000): server_address = ('localhost', port) httpd = HTTPServer(server_address, EditRequestHandler) print(f"本地服务器已启动:http://localhost:{port}") httpd.serve_forever() if __name__ == '__main__': run_server()
步骤3:访问网页
运行Python服务器后,不要直接双击打开HTML文件,而是通过浏览器访问http://localhost:8000/你的家谱首页路径,点击人物方块即可触发Python端的编辑流程。
二、自定义Windows URL协议
通过注册自定义协议,让网页点击时直接调用Python程序并传递人物ID,无需搭建服务器。
步骤1:注册自定义协议
创建一个.reg文件(替换Python脚本路径为你自己的):
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\familytree] @="URL:Family Tree Edit Protocol" "URL Protocol"="" [HKEY_CLASSES_ROOT\familytree\shell] [HKEY_CLASSES_ROOT\familytree\shell\open] [HKEY_CLASSES_ROOT\familytree\shell\open\command] @="\"C:\\Path\\To\\Your\\Python\\python.exe\" \"C:\\Path\\To\\Your\\EditScript.py\" \"%1\""
双击该文件导入注册表(需要管理员权限)。
步骤2:修改HTML代码
给人物div添加点击事件,打开自定义协议链接:
<div class='person-3-6' onclick="window.location.href='familytree://edit?person_id=3-6'"> John Christy<br><br><br>1845 - 1930 <a href='Root/Mother/Mother/Father/index.html'><span class='link'></span></a> </div>
步骤3:编写Python处理脚本
import sys import urllib.parse if len(sys.argv) > 1: request_url = sys.argv[1] parsed_url = urllib.parse.urlparse(request_url) if parsed_url.scheme == 'familytree' and parsed_url.path == '/edit': query_params = urllib.parse.parse_qs(parsed_url.query) person_id = query_params.get('person_id', [''])[0] print(f"开始编辑人物:{person_id}") # 执行你的编辑逻辑
注意:第一次点击时Windows会弹出确认框,需要用户同意打开外部程序。
三、WebSocket实时通信(适合双向交互场景)
如果需要实时同步家谱数据(比如编辑后网页自动更新),可以用WebSocket实现双向通信。
步骤1:安装依赖
pip install websockets
步骤2:Python WebSocket服务器
import asyncio import websockets async def handle_client(websocket): async for message in websocket: if message.startswith('edit:'): person_id = message.split(':')[1] print(f"开始编辑人物:{person_id}") # 执行编辑逻辑,还可以向网页发送更新消息 await websocket.send(f"正在编辑人物:{person_id}") async def main(): async with websockets.serve(handle_client, "localhost", 8765): await asyncio.Future() # 保持服务器运行 asyncio.run(main())
步骤3:修改HTML代码
添加WebSocket连接和点击事件:
<script> const ws = new WebSocket('ws://localhost:8765'); ws.onopen = () => console.log('已连接到编辑服务'); ws.onmessage = (event) => console.log(event.data); </script> <div class='person-3-6' onclick="ws.send('edit:3-6')"> John Christy<br><br><br>1845 - 1930 <a href='Root/Mother/Mother/Father/index.html'><span class='link'></span></a> </div>
内容的提问来源于stack exchange,提问作者Trevor Boardman
相关产品推荐
相关产品推荐

