{"id":189,"date":"2019-07-07T10:19:44","date_gmt":"2019-07-07T02:19:44","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=189"},"modified":"2019-07-07T10:19:44","modified_gmt":"2019-07-07T02:19:44","slug":"%e5%b0%86mysql%e8%a1%a8%e5%af%bc%e5%85%a5xls%e6%96%87%e4%bb%b6%ef%bc%8c%e5%b0%86word%e6%96%87%e6%a1%a3%e5%af%bc%e5%85%a5mysql","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=189","title":{"rendered":"\u5c06MySQL\u8868\u5bfc\u5165xls\u6587\u4ef6\uff0c\u5c06word\u6587\u6863\u5bfc\u5165MySQL"},"content":{"rendered":"<h3>\u5f15\u8a00<\/h3>\n<p>\u6700\u8fd1\u63a5\u4e86\u4e00\u5355\uff0c\u8981\u6c42\u662f\u7528Python\u505a\u4e00\u4e2a\u5c0f\u7a0b\u5e8f<br \/>\n1. \u53ef\u4ee5\u5c06MySQL\u4e2d\u4efb\u610f\u7684\u8868\u5bfc\u51fa\u81f3Excel\u6587\u4ef6<br \/>\n2. \u53ef\u4ee5\u5c06\u4efb\u610fWord\u6587\u6863\u7684\u5185\u5bb9\u4fdd\u5b58\u81f3MySQL\u4e2d<\/p>\n<h3>\u914d\u7f6e\u5f00\u53d1\u73af\u5883<\/h3>\n<p>\u6211\u4f7f\u7528\u7684Python\u7248\u672c\u662f3.7\uff0c\u4e0b\u9762\u7684\u6559\u7a0b\u5e94\u8be5\u4e0d\u9002\u7528\u4e8ePython2\u3002<\/p>\n<h4>\u5b89\u88c5pymysql\u5e93<\/h4>\n<p>\u8fd9\u4e2a\u5e93\u53ef\u4ee5\u5bf9MySQL\u8fdb\u884c\u5904\u7406<br \/>\n\u6253\u5f00cmd\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5e76\u56de\u8f66<br \/>\n<code>pip3 install pymysql<\/code><\/p>\n<h4>\u5b89\u88c5xlwt\u5e93<\/h4>\n<p>\u8fd9\u4e2a\u5e93\u662f\u7528\u6765\u64cd\u4f5cxls\u6587\u4ef6\u7684<br \/>\n<code>pip3 install xlwt<\/code><\/p>\n<h4>\u5b89\u88c5python-docx\u5e93<\/h4>\n<p>\u8fd9\u4e2a\u5e93\u7528\u6765\u8bfb\u5199docx\u6587\u4ef6\uff0c\u4e0d\u652f\u6301doc\u6587\u4ef6<br \/>\n<code>pip3 install python-docx<\/code><\/p>\n<h4>\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898<\/h4>\n<ol>\n<li>\u8f93\u5165\u4e0a\u8ff0\u547d\u4ee4\u51fa\u73b0cmd\u7684\u9519\u8bef\u63d0\u793a\uff0c\u8bf4\u660e\u6ca1\u6709\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3002\u81ea\u884c\u767e\u5ea6Python\u73af\u5883\u53d8\u91cf\u914d\u7f6e\u3002<\/li>\n<li>\u8fde\u63a5\u5f88\u957f\u65f6\u95f4\u540e\u81ea\u5df1\u9000\u51fa\u6216\u8005\u4e0b\u8f7d\u901f\u5ea6\u8d85\u7ea7\u6162\u3002\u5c06\u4e0b\u8f7d\u6e90\u66f4\u6362\u4e3a\u56fd\u5185\u6e90\u5373\u53ef\u3002\u5728\u4e0a\u9762\u7684\u547d\u4ee4\u540e\u52a0\u4e0a<code>-i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<\/code>\u5373\u53ef\u3002<\/li>\n<\/ol>\n<h3>\u4ee3\u7801<\/h3>\n<pre><code class=\"language-Python line-numbers\"># -*- coding: utf-8 -*-\n# www.guanhaobo.cn\n\nimport xlwt\nimport docx\nimport pymysql\n\n\ndef MysqlToExcel(UserName, Password, Database, TableName, SaveName):\n    # UserName\u662fMySQL\u8d26\u53f7,Password\u4e3aMySQL\u5bc6\u7801\uff0cDatabase\u662f\u6570\u636e\u5e93\u7684\u540d\u79f0\uff0cTableName\u662f\u8868\u7684\u540d\u79f0\uff0cSaveName\u662f\u8981\u4fdd\u5b58\u7684excel\u7684\u540d\u79f0\n\n    # \u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\n    db = pymysql.connect(\"localhost\", UserName, Password, Database)\n\n    # \u4f7f\u7528cursor()\u65b9\u6cd5\u83b7\u53d6\u64cd\u4f5c\u6e38\u6807\n    cursor = db.cursor()\n\n    count = cursor.execute('select * from '+TableName)\n    # print(cursor.lastrowid)\n    print(\"\u53d1\u73b0\", count, \"\u6761\u8bb0\u5f55\")\n    # \u91cd\u7f6e\u6e38\u6807\u7684\u4f4d\u7f6e\n    cursor.scroll(0, mode='absolute')\n    # \u641c\u53d6\u6240\u6709\u7ed3\u679c\n    results = cursor.fetchall()\n\n    # \u83b7\u53d6MYSQL\u91cc\u9762\u7684\u6570\u636e\u5b57\u6bb5\u540d\u79f0\n    fields = cursor.description\n    workbook = xlwt.Workbook()\n\n    # \u6ce8\u610f: \u5728add_sheet, \u7f6e\u53c2\u6570cell_overwrite_ok=True, \u53ef\u4ee5\u8986\u76d6\u539f\u5355\u5143\u683c\u4e2d\u7684\u6570\u636e\n    # cell_overwrite_ok\u9ed8\u8ba4\u4e3aFalse, \u8986\u76d6\u7684\u8bdd, \u4f1a\u629b\u51fa\u5f02\u5e38\n    sheet = workbook.add_sheet('table_'+TableName, cell_overwrite_ok=True)\n\n    # \u5199\u4e0a\u5b57\u6bb5\u4fe1\u606f\n    for field in range(0, len(fields)):\n        sheet.write(0, field, fields[field][0])\n        print(fields[field][0], end='  ')\n    print()\n\n    # \u83b7\u53d6\u5e76\u5199\u5165\u6570\u636e\u6bb5\u4fe1\u606f\n    row = 1\n    col = 0\n    for row in range(1, len(results)+1):\n        for col in range(0, len(fields)):\n            sheet.write(row, col, u'%s' % results[row-1][col])\n            print(results[row-1][col], end='  ')\n        print()\n\n    workbook.save(SaveName+\".xls\")\n    print(\"\u6210\u529f\u5199\u5165\", count, \"\u6761\u8bb0\u5f55\u5230\"+SaveName+\".xls\")\n\n\ndef WordToMysql(UserName, Password, Database, TableName, FileName):\n    # \u83b7\u53d6\u6587\u6863\u5bf9\u8c61\n    file = docx.Document(FileName)\n\n    # \u8f93\u51fa\u6bb5\u843d\u7f16\u53f7\u53ca\u6bb5\u843d\u5185\u5bb9\n    for i in range(len(file.paragraphs)):\n        print(\"\u7b2c\"+str(i+1)+\"\u6bb5\u7684\u5185\u5bb9\u662f\uff1a\"+file.paragraphs[i].text)\n\n    # \u6253\u5f00\u6570\u636e\u5e93\u8fde\u63a5\n    db = pymysql.connect(\"localhost\", UserName, Password, Database)\n\n    # \u4f7f\u7528cursor()\u65b9\u6cd5\u83b7\u53d6\u64cd\u4f5c\u6e38\u6807\n    cursor = db.cursor()\n    try:\n        # \u521b\u5efa\u6570\u636e\u8868SQL\u8bed\u53e5\n        sql = \"CREATE TABLE \"+TableName + \" (Paragraph varchar(500))\"\n\n        # \u6267\u884csql\u8bed\u53e5\n        cursor.execute(sql)\n        # \u63d0\u4ea4\u5230\u6570\u636e\u5e93\u6267\u884c\n        db.commit()\n    except:\n        print(\"\u51fa\u9519\u4e86......\u6216\u8bb8\"+TableName+\"\u5df2\u7ecf\u5b58\u5728\u4e86\uff01\")\n        return\n    for i in range(len(file.paragraphs)):\n        print(\"\u6210\u529f\u63d2\u5165\u7b2c\"+str(i+1)+\"\u6bb5......\")\n        text = '\"'+file.paragraphs[i].text+'\"'\n        # SQL \u63d2\u5165\u8bed\u53e5\n        sql = \"INSERT INTO \"+TableName+\"(Paragraph) VALUES (\" + text + \")\"\n        try:\n            # \u6267\u884csql\u8bed\u53e5\n            cursor.execute(sql)\n            # \u63d0\u4ea4\u5230\u6570\u636e\u5e93\u6267\u884c\n            db.commit()\n        except:\n            print(\"\u51fa\u9519\u4e86......\")\n\n    # \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\n    db.close()\n    print(\"\u6210\u529f\u5c06\"+FileName+\"\u4e2d\u7684\u6570\u636e\u5199\u5165\u8868\"+TableName+\"\u4e2d\uff01\")\n\n\nif __name__ == '__main__':\n    while True:\n        print(\"    **************************************************************\")\n        print(\"    *                 1. \u5c06MySQL\u7684\u8868\u4e2d\u6570\u636e\u5bfc\u51fa\u81f3Excel            *\")\n        print(\"    *                 2. \u5c06Word\u6587\u4ef6\u5bfc\u5165MySQL                     *\")\n        print(\"    *                 0. \u9000\u51fa\u7a0b\u5e8f                                *\")\n        print(\"    **************************************************************\")\n        a = int(input(\"\u8bf7\u9009\u62e9\uff1a\"))\n        if a == 1:\n            print(\"    **************************************************************\")\n            print(\"    *                  \u5c06MySQL\u7684\u8868\u4e2d\u6570\u636e\u5bfc\u51fa\u81f3Excel              *\")\n            print(\"    **************************************************************\")\n            UserName = input(\"\u8bf7\u8f93\u5165MySQL\u8d26\u53f7\uff1a\")\n            Password = input(\"\u8bf7\u8f93\u5165MySQL\u5bc6\u7801\uff1a\")\n            Database = input(\"\u8bf7\u8f93\u5165\u6570\u636e\u5e93\u540d\u79f0\uff1a\")\n            TableName = input(\"\u8bf7\u8f93\u5165\u8868\u7684\u540d\u79f0\uff1a\")\n            SaveName = input(\"\u8bf7\u8f93\u5165\u4fdd\u5b58\u7684excel\u6587\u4ef6\u540d\uff08\u65e0\u9700\u6269\u5c55\u540d\uff09\uff1a\")\n            MysqlToExcel(UserName, Password, Database, TableName, SaveName)\n        elif a == 2:\n            print(\"    **************************************************************\")\n            print(\"    *                   \u5c06Word\u6587\u4ef6\u5bfc\u5165MySQL                      *\")\n            print(\"    **************************************************************\")\n            UserName = input(\"\u8bf7\u8f93\u5165MySQL\u8d26\u53f7\uff1a\")\n            Password = input(\"\u8bf7\u8f93\u5165MySQL\u5bc6\u7801\uff1a\")\n            Database = input(\"\u8bf7\u8f93\u5165\u6570\u636e\u5e93\u540d\u79f0\uff1a\")\n            TableName = input(\"\u8bf7\u8f93\u5165\u60f3\u8981\u65b0\u5efa\u7684\u8868\u7684\u540d\u79f0\uff1a\")\n            FileName = input(\"\u8bf7\u8f93\u5165word\u6587\u4ef6\u7684\u8def\u5f84\uff08\u53ea\u652f\u6301docx\u6587\u4ef6\uff0c\u5982\uff1aD:\\\\test\\\\\u4f60\u597d.docx\uff09\uff1a\")\n            WordToMysql(UserName, Password, Database, TableName, FileName)\n        elif a == 0:\n            input(\"\u6309\u56de\u8f66\u9000\u51fa\u7a0b\u5e8f......\")\n            exit()\n<\/code><\/pre>\n<h3>\u8fd0\u884c\u7ed3\u679c<\/h3>\n<h4>\u5c06MySQL\u4e2d\u7684\u8868\u5bfc\u51fa\u81f3xls<\/h4>\n<p><img decoding=\"async\" src=\"http:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2019\/07\/1001.jpg\" alt=\"\u5c06MySQL\u4e2d\u7684\u8868\u5bfc\u51fa\u81f3xls\" title=\"\u5c06MySQL\u4e2d\u7684\u8868\u5bfc\u51fa\u81f3xls\" \/><\/p>\n<h4>\u751f\u6210\u7684xls\u6587\u4ef6<\/h4>\n<p><img decoding=\"async\" src=\"http:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2019\/07\/1005.jpg\" alt=\"\u751f\u6210\u7684xls\u6587\u4ef6\" title=\"\u751f\u6210\u7684xls\u6587\u4ef6\" \/><\/p>\n<h4>\u793a\u4f8b\u4e2dword\u6587\u6863\u7684\u5185\u5bb9<\/h4>\n<p><img decoding=\"async\" src=\"http:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2019\/07\/1002.jpg\" alt=\"\u793a\u4f8b\u4e2dword\u6587\u6863\u7684\u5185\u5bb9\" title=\"\u793a\u4f8b\u4e2dword\u6587\u6863\u7684\u5185\u5bb9\" \/><\/p>\n<h4>\u5c06docx\u6587\u4ef6\u4fdd\u5b58\u81f3MySQL<\/h4>\n<p><img decoding=\"async\" src=\"http:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2019\/07\/1003.jpg\" alt=\"\u5c06docx\u6587\u4ef6\u4fdd\u5b58\u81f3MySQL\" title=\"\u5c06docx\u6587\u4ef6\u4fdd\u5b58\u81f3MySQL\" \/><\/p>\n<h4>MySQL\u4e2d\u7684\u7ed3\u679c<\/h4>\n<p><img decoding=\"async\" src=\"http:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2019\/07\/1004.jpg\" alt=\"MySQL\u4e2d\u7684\u7ed3\u679c\" title=\"MySQL\u4e2d\u7684\u7ed3\u679c\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \u6700\u8fd1\u63a5\u4e86\u4e00\u5355\uff0c\u8981\u6c42\u662f\u7528Python\u505a\u4e00\u4e2a\u5c0f\u7a0b\u5e8f 1. \u53ef\u4ee5\u5c06MySQL\u4e2d\u4efb\u610f\u7684\u8868\u5bfc\u51fa\u81f3Excel\u6587\u4ef6 2 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[26],"class_list":["post-189","post","type-post","status-publish","format-standard","hentry","category-life","tag-python"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=189"}],"version-history":[{"count":0,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/189\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}