Windows 默认编码为 GBK,Python stdout 输出含中文的 JSON 时 Node.js 端无法正确解析,导致批量生成操作静默失败。 - python/main.py:启动时强制 stdin/stdout 使用 UTF-8 - templateIpc.js:spawn 传入 PYTHONUTF8=1,改用 Buffer 拼接后 UTF-8 解码 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
31 lines
970 B
Python
31 lines
970 B
Python
import sys
|
||
import json
|
||
import io
|
||
|
||
# Windows 默认编码不是 UTF-8,强制设置
|
||
sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding="utf-8")
|
||
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", line_buffering=True)
|
||
|
||
def main():
|
||
for line in sys.stdin:
|
||
line = line.strip()
|
||
if not line:
|
||
continue
|
||
try:
|
||
req = json.loads(line)
|
||
action = req.get("action")
|
||
if action == "parse_template":
|
||
from xl_parser import parse_template
|
||
result = parse_template(req["file_path"])
|
||
elif action == "generate":
|
||
from generator import generate
|
||
result = generate(req)
|
||
else:
|
||
result = {"error": f"unknown action: {action}"}
|
||
except Exception as e:
|
||
result = {"error": str(e)}
|
||
print(json.dumps(result, ensure_ascii=False), flush=True)
|
||
|
||
if __name__ == "__main__":
|
||
main()
|