{"id":16217,"date":"2025-05-15T23:35:57","date_gmt":"2025-05-15T21:35:57","guid":{"rendered":"https:\/\/www.beseit.net\/?p=16217"},"modified":"2025-05-15T23:53:45","modified_gmt":"2025-05-15T21:53:45","slug":"executar-el-script-python-des-de-php","status":"publish","type":"post","link":"http:\/\/www.beseit.net\/?p=16217","title":{"rendered":"Col\u00b7laborar PHP &#038; Python (Separant totalment la responsabilitat"},"content":{"rendered":"\n<ul class=\"wp-block-list\">\n<li><strong>PHP<\/strong> fa la seva feina (interf\u00edcie, configuraci\u00f3, etc.).<\/li>\n\n\n\n<li><strong>Python<\/strong> est\u00e0 sempre en segon pla, monitorant el porta-retalls i parlant.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Exemple de control via fitxer:<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nfile_put_contents(\"config.txt\", \"start\"); \/\/ O \"stop\"\n?&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ul class=\"wp-block-list\">\n<li><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Python (modificat):<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import pyperclip\nimport pyttsx3\nimport time\nimport os\n\nengine = pyttsx3.init()\nlast_text = \"\"\n\nwhile True:\n    if os.path.exists(\"config.txt\"):\n        with open(\"config.txt\") as f:\n            state = f.read().strip()\n        if state != \"start\":\n            time.sleep(1)\n            continue\n    text = pyperclip.paste()\n    if text != last_text and text.strip() != \"\":\n        print(\"Nou text copiat:\", text)\n        engine.say(text)\n        engine.runAndWait()\n        last_text = text\n    time.sleep(0.5)\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>?\ufe0f <strong>Etapa 1: Crear el lector en Python<\/strong><\/li>\n\n\n\n<li>\u2705 Script Python que detecta el text copiat.<\/li>\n\n\n\n<li>\u2705 Converteix el text en veu (TTS).<\/li>\n\n\n\n<li>\u2705 Opci\u00f3 per controlar l\u2019activaci\u00f3 (fitxer <code>config.txt<\/code>).<\/li>\n\n\n\n<li>?\ufe0f <strong>Etapa 2: Crear una interf\u00edcie b\u00e0sica en PHP<\/strong><\/li>\n\n\n\n<li>\u2705 Botons per activar o desactivar la lectura.<\/li>\n\n\n\n<li>\u2705 Escriure a <code>config.txt<\/code> segons l&#8217;acci\u00f3.<\/li>\n\n\n\n<li>?\ufe0f <strong>Etapa 3: Integraci\u00f3<\/strong><\/li>\n\n\n\n<li>\u2705 Gestionar l\u2019estat des de la interf\u00edcie.<\/li>\n\n\n\n<li>\u2705 Executar el script Python des de PHP si cal.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>'Etapa 1: lector en Python\n\nimport pyperclip\nimport pyttsx3\nimport time\nimport os\n\nengine = pyttsx3.init()\nlast_text = \"\"\n\ndef llegir_text(text):\n    engine.say(text)\n    engine.runAndWait()\n\nwhile True:\n    # Llegim l'estat del fitxer de configuraci\u00f3\n    if os.path.exists(\"config.txt\"):\n        with open(\"config.txt\") as f:\n            estat = f.read().strip()\n        if estat != \"start\":\n            time.sleep(1)\n            continue\n    else:\n        time.sleep(1)\n        continue\n\n    # Llegim el text del porta-retalls\n    text = pyperclip.paste()\n    if text != last_text and text.strip() != \"\":\n        print(\"Llegint:\", text)\n        llegir_text(text)\n        last_text = text\n    time.sleep(0.5)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Exemple de control via fitxer: Python (modificat):<\/p>\n","protected":false},"author":2,"featured_media":3170,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[],"class_list":["post-16217","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts\/16217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16217"}],"version-history":[{"count":8,"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts\/16217\/revisions"}],"predecessor-version":[{"id":16228,"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts\/16217\/revisions\/16228"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/media\/3170"}],"wp:attachment":[{"href":"http:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16217"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}