{"id":174,"date":"2023-09-09T17:04:02","date_gmt":"2023-09-09T09:04:02","guid":{"rendered":"https:\/\/blog.mada.press\/?p=174"},"modified":"2023-09-23T17:48:12","modified_gmt":"2023-09-23T09:48:12","slug":"%e9%a3%9e%e6%a1%a8%e7%8e%af%e5%a2%83%e8%bf%90%e8%a1%8cword%e7%bf%bb%e8%af%91%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/blog.mada.press\/index.php\/2023\/09\/09\/%e9%a3%9e%e6%a1%a8%e7%8e%af%e5%a2%83%e8%bf%90%e8%a1%8cword%e7%bf%bb%e8%af%91%e4%bb%a3%e7%a0%81\/","title":{"rendered":"\u98de\u6868\u73af\u5883\u8fd0\u884cWord\u7ffb\u8bd1\u4ee3\u7801"},"content":{"rendered":"<h1>\u51c6\u5907:<\/h1>\n<ul>\n<li>OpenAI Key<\/li>\n<li>api\u4ee3\u7406\u57df\u540d(cloudflare\u642d\u5efa)<\/li>\n<li>\u767e\u5ea6\u98de\u6868AIStudio\u8d26\u53f7<\/li>\n<li>\u51c6\u5907\u7ffb\u8bd1\u7684Word\u6587\u4ef6<\/li>\n<\/ul>\n<h1>\u548cChatGPT\u79c1\u804a\u83b7\u5f97\u4ee3\u7801<\/h1>\n<pre><code class=\"language-python line-numbers\">import requests\nfrom docx import Document\nfrom docx.shared import Pt\nfrom concurrent.futures import ThreadPoolExecutor\nfrom tqdm import tqdm\n\ndef chatgpt(prompt):\n    h = {\n        'Content-Type': 'application\/json',\n        'Authorization': 'Bearer Openai\u5bc6\u94a5'\n    }\n    d = {\n        \"model\": \"text-davinci-003\",\n        \"prompt\": prompt,\n        \"max_tokens\": 1000,\n        \"temperature\": 0\n    }\n    u = 'https:\/\/\u7f51\u5740\/v1\/completions'\n    r = requests.post(url=u, headers=h, json=d, verify=True, timeout=30).json()\n    if 'choices' in r:\n        return r['choices'][0]['text']\n\n# Read the Word document\ndoc = Document('\/home\/aistudio\/[EN]\u5229\u7528\u6570\u636e\u5171\u4eab\u5de5\u5177\u5b9e\u73b0\u5faa\u73af\u7ecf\u6d4e\uff1a\u4ee5\u4ea7\u54c1\u6570\u5b57\u62a4\u7167\u4e3a\u4f8b.pdf.docx')\n\n# Create a new Word document to save the translated results\nnew_doc = Document()\n\n# Create a ThreadPoolExecutor\nexecutor = ThreadPoolExecutor(max_workers=20)\n\n# Process paragraphs\nprompts = [f'Translate the following English text to Chinese: {paragraph.text}' for paragraph in doc.paragraphs]\nfor prompt, translation in tqdm(zip(prompts, executor.map(chatgpt, prompts)), total=len(prompts)):\n    # Add the original text and translation to a new paragraph in the new document, and set paragraph spacing\n    new_para = new_doc.add_paragraph()\n    new_para.add_run(prompt.replace('Translate the following English text to Chinese: ', ''))\n    new_para.space_after = Pt(12)\n\n    new_para = new_doc.add_paragraph()\n    new_para.add_run(translation)\n    new_para.space_after = Pt(12)\n\n# Process tables\nprompts = [f'Translate the following English text to Chinese: {cell.text}' for table in doc.tables for row in table.rows for cell in row.cells]\nfor prompt, translation in tqdm(zip(prompts, executor.map(chatgpt, prompts)), total=len(prompts)):\n    # Add the original text and translation to a new table in the new document\n    new_cell = new_doc.add_table(1, 1).cell(0, 0)\n    new_cell.text = prompt.replace('Translate the following English text to Chinese: ', '')\n    new_cell = new_doc.add_table(1, 1).cell(0, 0)\n    new_cell.text = translation\n\n# Save the new document\nnew_doc.save('[EN]\u5229\u7528\u6570\u636e\u5171\u4eab\u5de5\u5177\u5b9e\u73b0\u5faa\u73af\u7ecf\u6d4e\uff1a\u4ee5\u4ea7\u54c1\u6570\u5b57\u62a4\u7167\u4e3a\u4f8b.docx')\n<\/code><\/pre>\n<h1>\u5728\u767e\u5ea6\u98de\u6868\u7684notebook\u8fd0\u884c<\/h1>\n<p>\u6548\u679c\u548c\u8fc7\u7a0b\u89c1<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/aistudio.baidu.com\/projectdetail\/6708613\" title=\"\u94fe\u63a5https:\/\/aistudio.baidu.com\/projectdetail\/6708613\">\u94fe\u63a5https:\/\/aistudio.baidu.com\/projectdetail\/6708613<\/a><\/p>\n<p>\u5f97\u5230\u7ed3\u679c\u540e\uff0c\u8fd8\u9700\u8981\u8c03\u6574\u6587\u672c\u683c\u5f0f\uff0c\u5e76\u6e05\u7406\u6389AI\u7684\u80e1\u8a00\u4e71\u8bed\u3002<\/p>\n<h1>PS<\/h1>\n<p>\u65e5\u5e38\u7528\u7684\u8bdd\uff0c\u63a8\u8350\u6548\u679c\u66f4\u597d\u7684<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/immersivetranslate.com\/docs\/\" title=\"&quot;\u6c89\u6d78\u5f0f\u7ffb\u8bd1&quot;\">&#8220;\u6c89\u6d78\u5f0f\u7ffb\u8bd1&#8221;<\/a><\/p>\n<p>\u672c\u6765\u60f3\u7ee7\u7eed\u8c03\u8bd5\uff0c\u8c03\u7528\u767e\u5ea6\u6587\u5fc3\u4e00\u8a00\u7684api(<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/https:\/\/dify.ai\/zh\" title=\"\u6b64\u5904\u4f7f\u7528Dify\u7b80\u5316\u540e\u7684API\">\u6b64\u5904\u4f7f\u7528Dify\u7b80\u5316\u540e\u7684API<\/a>)\uff0c\u4f46\u5343\u5e06\u5e73\u53f0\u76ee\u524d\u7ed9\u4e2a\u4eba\u6ce8\u518c\u7528\u6237\u7684QPS\u53ea\u67095\uff0c\u7ffb\u8bd1\u901f\u5ea6\u5f88\u6162\uff0c\u62a5\u5de5\u5355\u7ed9\u4e86\u5ba2\u670d\uff0c\u8bf4\u4e0d\u901a\u8fc7\u5546\u52a1\u7ecf\u7406\u7684\u8bdd\u76ee\u524d\u53ea\u80fd\u53cd\u9988\u7ed9\u9879\u76ee\u90e8\u3002\u867d\u7136\u767e\u5ea6\u5728\u5f00\u653eapi\u4ed8\u8d39token\u65b9\u9762\u5df2\u7ecf\u505a\u7684\u633a\u597d\u4e86\uff0c\u4f46\u548c\u70e7\u94b1\u6269\u5f20\u7684Openai\u6bd4\u8fd8\u662f\u6709\u5dee\u8ddd\u3002\u98de\u6868Studio\u867d\u7136\u4e0d\u9519\uff0c\u4f46\u548cGoogleColab\u6bd4\u8d77\u6765\uff0c\u5728\u7f51\u7edc\u73af\u5883\u914d\u7f6e\u548c\u5e73\u53f0\u5f00\u653e\u6027\u4e0a\u8fd8\u662f\u6709\u6b20\u7f3a\u3002<\/p>\n<p>\u5410\u69fd\u7ed3\u675f\u3002\u3002\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51c6\u5907: OpenAI Key api\u4ee3\u7406\u57df\u540d(cloudflare\u642d\u5efa) \u767e\u5ea6\u98de &hellip; <a href=\"https:\/\/blog.mada.press\/index.php\/2023\/09\/09\/%e9%a3%9e%e6%a1%a8%e7%8e%af%e5%a2%83%e8%bf%90%e8%a1%8cword%e7%bf%bb%e8%af%91%e4%bb%a3%e7%a0%81\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[45],"tags":[52,53,50,40,48,51,49],"class_list":["post-174","post","type-post","status-publish","format-standard","hentry","category-45","tag-api","tag-openai","tag-paddlepaddle","tag-python","tag-48","tag-51","tag-49"],"_links":{"self":[{"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/posts\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/comments?post=174"}],"version-history":[{"count":5,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/posts\/174\/revisions"}],"predecessor-version":[{"id":180,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/posts\/174\/revisions\/180"}],"wp:attachment":[{"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/media?parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/categories?post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/tags?post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}