{"id":172,"date":"2022-09-06T14:41:38","date_gmt":"2022-09-06T06:41:38","guid":{"rendered":"http:\/\/www.hufe.org.cn\/?p=172"},"modified":"2022-09-06T14:42:19","modified_gmt":"2022-09-06T06:42:19","slug":"5%e4%b8%aa%e6%96%b9%e4%be%bf%e5%a5%bd%e7%94%a8%e7%9a%84python%e8%87%aa%e5%8a%a8%e5%8c%96%e8%84%9a%e6%9c%ac%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.hufe.org.cn\/?p=172","title":{"rendered":"5\u4e2a\u65b9\u4fbf\u597d\u7528\u7684Python\u81ea\u52a8\u5316\u811a\u672c\uff08\u4e00\uff09"},"content":{"rendered":"\n<p>\u4ee5\u4e0b\u6587\u7ae0\u6765\u6e90\u4e8ePython\u5927\u6570\u636e\u5206\u6790&nbsp;\uff0c\u4f5c\u8005\uff1a\u6731\u536b\u519b<\/p>\n\n\n\n<p>\u76f8\u6bd4\u5927\u5bb6\u90fd\u542c\u8fc7\u81ea\u52a8\u5316\u751f\u4ea7\u7ebf\u3001\u81ea\u52a8\u5316\u529e\u516c\u7b49\u8bcd\u6c47\uff0c\u5728\u6ca1\u6709\u4eba\u5de5\u5e72\u9884\u7684\u60c5\u51b5\u4e0b\uff0c\u673a\u5668\u53ef\u4ee5\u81ea\u5df1\u5b8c\u6210\u5404\u9879\u4efb\u52a1\uff0c\u8fd9\u5927\u5927\u63d0\u5347\u4e86\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n\n\n\n<p>\u7f16\u7a0b\u4e16\u754c\u91cc\u6709\u5404\u79cd\u5404\u6837\u7684\u81ea\u52a8\u5316\u811a\u672c\uff0c\u6765\u5b8c\u6210\u4e0d\u540c\u7684\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>\u5c24\u5176Python\u975e\u5e38\u9002\u5408\u7f16\u5199\u81ea\u52a8\u5316\u811a\u672c\uff0c\u56e0\u4e3a\u5b83\u8bed\u6cd5\u7b80\u6d01\u6613\u61c2\uff0c\u800c\u4e14\u6709\u4e30\u5bcc\u7684\u7b2c\u4e09\u65b9\u5de5\u5177\u5e93\u3002<\/p>\n\n\n\n<p>\u8fd9\u6b21\u6211\u4eec\u4f7f\u7528Python\u6765\u5b9e\u73b0\u51e0\u4e2a\u81ea\u52a8\u5316\u573a\u666f\uff0c\u6216\u8bb8\u53ef\u4ee5\u7528\u5230\u4f60\u7684\u5de5\u4f5c\u4e2d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1\u3001\u81ea\u52a8\u5316\u9605\u8bfb\u7f51\u9875\u65b0\u95fb<\/h2>\n\n\n\n<p>\u8fd9\u4e2a\u811a\u672c\u80fd\u591f\u5b9e\u73b0\u4ece\u7f51\u9875\u4e2d\u6293\u53d6\u6587\u672c\uff0c\u7136\u540e\u81ea\u52a8\u5316\u8bed\u97f3\u6717\u8bfb\uff0c\u5f53\u4f60\u60f3\u542c\u65b0\u95fb\u7684\u65f6\u5019\uff0c\u8fd9\u662f\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u5206\u4e3a\u4e24\u5927\u90e8\u5206\uff0c\u7b2c\u4e00\u901a\u8fc7\u722c\u866b\u6293\u53d6\u7f51\u9875\u6587\u672c\u5462\uff0c\u7b2c\u4e8c\u901a\u8fc7\u9605\u8bfb\u5de5\u5177\u6765\u6717\u8bfb\u6587\u672c\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u7684\u7b2c\u4e09\u65b9\u5e93\uff1a<\/p>\n\n\n\n<p>Beautiful Soup &#8211; \u7ecf\u5178\u7684HTML\/XML\u6587\u672c\u89e3\u6790\u5668\uff0c\u7528\u6765\u63d0\u53d6\u722c\u4e0b\u6765\u7684\u7f51\u9875\u4fe1\u606f<\/p>\n\n\n\n<p>requests &#8211; \u597d\u7528\u5230\u9006\u5929\u7684HTTP\u5de5\u5177\uff0c\u7528\u6765\u5411\u7f51\u9875\u53d1\u9001\u8bf7\u6c42\u83b7\u53d6\u6570\u636e<\/p>\n\n\n\n<p>Pyttsx3 &#8211; \u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u8bed\u97f3\uff0c\u5e76\u63a7\u5236\u901f\u7387\u3001\u9891\u7387\u548c\u8bed\u97f3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pyttsx3\nimport requests\nfrom bs4 import BeautifulSoup\nengine = pyttsx3.init('sapi5')\nvoices = engine.getProperty('voices')\nnewVoiceRate = 130                       ## Reduce The Speech Rate\nengine.setProperty('rate',newVoiceRate)\nengine.setProperty('voice', voices&#91;1].id)\ndef speak(audio):\n  engine.say(audio)\n  engine.runAndWait()\ntext = str(input(\"Paste article\\n\"))\nres = requests.get(text)\nsoup = BeautifulSoup(res.text,'html.parser')\n\narticles = &#91;]\nfor i in range(len(soup.select('.p'))):\n    article = soup.select('.p')&#91;i].getText().strip()\n    articles.append(article)\ntext = \" \".join(articles)\nspeak(text)\n# engine.save_to_file(text, 'test.mp3') ## If you want to save the speech as a audio file\nengine.runAndWait()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2\u3001\u81ea\u52a8\u751f\u6210\u7d20\u63cf\u8349\u56fe<\/h2>\n\n\n\n<p>\u8fd9\u4e2a\u811a\u672c\u53ef\u4ee5\u628a\u5f69\u8272\u56fe\u7247\u8f6c\u5316\u4e3a\u94c5\u7b14\u7d20\u63cf\u8349\u56fe\uff0c\u5bf9\u4eba\u50cf\u3001\u666f\u8272\u90fd\u6709\u5f88\u597d\u7684\u6548\u679c\u3002<\/p>\n\n\n\n<p>\u800c\u4e14\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u5c31\u53ef\u4ee5\u4e00\u952e\u751f\u6210\uff0c\u9002\u5408\u6279\u91cf\u64cd\u4f5c\uff0c\u975e\u5e38\u7684\u5feb\u6377\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u7684\u7b2c\u4e09\u65b9\u5e93\uff1a<\/p>\n\n\n\n<p>Opencv &#8211; \u8ba1\u7b97\u673a\u89c6\u89c9\u5de5\u5177\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u5143\u5316\u7684\u56fe\u50cf\u89c6\u9891\u5904\u7406\uff0c\u6709Python\u63a5\u53e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\"\" Photo Sketching Using Python \"\"\"\n  import cv2\n  img = cv2.imread(\"elon.jpg\")\n\n  ## Image to Gray Image\n  gray_image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n\n  ## Gray Image to Inverted Gray Image\n  inverted_gray_image = 255-gray_image\n\n  ## Blurring The Inverted Gray Image\n  blurred_inverted_gray_image = cv2.GaussianBlur(inverted_gray_image, (19,19),0)\n\n  ## Inverting the blurred image\n  inverted_blurred_image = 255-blurred_inverted_gray_image\n\n  ### Preparing Photo sketching\n  sketck = cv2.divide(gray_image, inverted_blurred_image,scale= 256.0)\n\n  cv2.imshow(\"Original Image\",img)\n  cv2.imshow(\"Pencil Sketch\", sketck)\n  cv2.waitKey(0)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"875\" height=\"528\" src=\"http:\/\/www.hufe.org.cn\/wp-content\/uploads\/2022\/09\/\u5fae\u4fe1\u56fe\u7247_20220905090541.png\" alt=\"\" class=\"wp-image-173\" srcset=\"https:\/\/www.hufe.org.cn\/wp-content\/uploads\/2022\/09\/\u5fae\u4fe1\u56fe\u7247_20220905090541.png 875w, https:\/\/www.hufe.org.cn\/wp-content\/uploads\/2022\/09\/\u5fae\u4fe1\u56fe\u7247_20220905090541-300x181.png 300w, https:\/\/www.hufe.org.cn\/wp-content\/uploads\/2022\/09\/\u5fae\u4fe1\u56fe\u7247_20220905090541-768x463.png 768w\" sizes=\"(max-width: 875px) 100vw, 875px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3\u3001\u81ea\u52a8\u53d1\u9001\u591a\u5c01\u90ae\u4ef6<\/h2>\n\n\n\n<p>\u8fd9\u4e2a\u811a\u672c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6279\u91cf\u5b9a\u65f6\u53d1\u9001\u90ae\u4ef6\uff0c\u90ae\u4ef6\u5185\u5bb9\u3001\u9644\u4ef6\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u8c03\u6574\uff0c\u975e\u5e38\u7684\u5b9e\u7528\u3002<\/p>\n\n\n\n<p>\u76f8\u6bd4\u8f83\u90ae\u4ef6\u5ba2\u6237\u7aef\uff0cPython\u811a\u672c\u7684\u4f18\u70b9\u5728\u4e8e\u53ef\u4ee5\u667a\u80fd\u3001\u6279\u91cf\u3001\u9ad8\u5b9a\u5236\u5316\u5730\u90e8\u7f72\u90ae\u4ef6\u670d\u52a1\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u7684\u7b2c\u4e09\u65b9\u5e93\uff1a<\/p>\n\n\n\n<p>Email &#8211; \u7528\u4e8e\u7ba1\u7406\u7535\u5b50\u90ae\u4ef6\u6d88\u606f<\/p>\n\n\n\n<p>Smtlib &#8211; \u5411SMTP\u670d\u52a1\u5668\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\uff0c\u5b83\u5b9a\u4e49\u4e86\u4e00\u4e2a SMTP \u5ba2\u6237\u7aef\u4f1a\u8bdd\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u53ef\u5c06\u90ae\u4ef6\u53d1\u9001\u5230\u4e92\u8054\u7f51\u4e0a\u4efb\u4f55\u5e26\u6709 SMTP \u6216 ESMTP \u76d1\u542c\u7a0b\u5e8f\u7684\u8ba1\u7b97\u673a<\/p>\n\n\n\n<p>Pandas &#8211; \u7528\u4e8e\u6570\u636e\u5206\u6790\u6e05\u6d17\u5730\u5de5\u5177<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;smtplib&nbsp;<br>from&nbsp;email.message&nbsp;import&nbsp;EmailMessage<br>import&nbsp;pandas&nbsp;as&nbsp;pd<br><br>def&nbsp;send_email(remail,&nbsp;rsubject,&nbsp;rcontent):<br>&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;=&nbsp;EmailMessage()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>##&nbsp;Creating&nbsp;a&nbsp;object&nbsp;for&nbsp;EmailMessage<\/em><br>&nbsp;&nbsp;&nbsp;&nbsp;email&#91;'from']&nbsp;=&nbsp;'The&nbsp;Pythoneer&nbsp;Here'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>##&nbsp;Person&nbsp;who&nbsp;is&nbsp;sending<\/em><br>&nbsp;&nbsp;&nbsp;&nbsp;email&#91;'to']&nbsp;=&nbsp;remail&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>##&nbsp;Whom&nbsp;we&nbsp;are&nbsp;sending<\/em><br>&nbsp;&nbsp;&nbsp;&nbsp;email&#91;'subject']&nbsp;=&nbsp;rsubject&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>##&nbsp;Subject&nbsp;of&nbsp;email<\/em><br>&nbsp;&nbsp;&nbsp;&nbsp;email.set_content(rcontent)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>##&nbsp;content&nbsp;of&nbsp;email<\/em><br>&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;smtplib.SMTP(host='smtp.gmail.com',port=587)as&nbsp;smtp:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;smtp.ehlo()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>##&nbsp;server&nbsp;object<\/em><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;smtp.starttls()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>##&nbsp;used&nbsp;to&nbsp;send&nbsp;data&nbsp;between&nbsp;server&nbsp;and&nbsp;client<\/em><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;smtp.login(\"deltadelta371@gmail.com\",\"delta@371\")&nbsp;<em>##&nbsp;login&nbsp;id&nbsp;and&nbsp;password&nbsp;of&nbsp;gmail<\/em><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;smtp.send_message(email)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>##&nbsp;Sending&nbsp;email<\/em><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(\"email&nbsp;send&nbsp;to&nbsp;\",remail)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em>##&nbsp;Printing&nbsp;success&nbsp;message<\/em><br><br>if&nbsp;__name__&nbsp;==&nbsp;'__main__':<br>&nbsp;&nbsp;&nbsp;&nbsp;df&nbsp;=&nbsp;pd.read_excel('list.xlsx')<br>&nbsp;&nbsp;&nbsp;&nbsp;length&nbsp;=&nbsp;len(df)+1<br><br>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;index,&nbsp;item&nbsp;in&nbsp;df.iterrows():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email&nbsp;=&nbsp;item&#91;0]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subject&nbsp;=&nbsp;item&#91;1]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content&nbsp;=&nbsp;item&#91;2]<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;send_email(email,subject,content)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u6587\u7ae0\u6765\u6e90\u4e8ePython\u5927\u6570\u636e\u5206\u6790&nbsp;\uff0c\u4f5c\u8005\uff1a\u6731\u536b\u519b \u76f8\u6bd4\u5927\u5bb6\u90fd\u542c\u8fc7\u81ea\u52a8\u5316\u751f\u4ea7\u7ebf\u3001\u81ea\u52a8\u5316\u529e\u516c\u7b49\u8bcd\u6c47\uff0c&hellip;&nbsp;<a href=\"https:\/\/www.hufe.org.cn\/?p=172\" class=\"\" rel=\"bookmark\">\u9605\u8bfb\u66f4\u591a &raquo;<span class=\"screen-reader-text\">5\u4e2a\u65b9\u4fbf\u597d\u7528\u7684Python\u81ea\u52a8\u5316\u811a\u672c\uff08\u4e00\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"[\"title\",\"meta\",\"thumbnail\",\"content\",\"tags\",\"comments\"]","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/172"}],"collection":[{"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=172"}],"version-history":[{"count":4,"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":179,"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/172\/revisions\/179"}],"wp:attachment":[{"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hufe.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}