{"id":407,"date":"2023-06-18T22:03:01","date_gmt":"2023-06-18T14:03:01","guid":{"rendered":"https:\/\/www.gzlcy.cn\/?p=407"},"modified":"2023-06-18T22:03:01","modified_gmt":"2023-06-18T14:03:01","slug":"excel-%e8%a1%a8%e6%a0%bc%e5%a4%8d%e5%88%b6%e5%88%b0word%e5%90%8e%ef%bc%8c%e5%ae%bd%e8%b6%85%e5%87%baword%e5%a6%82%e4%bd%95%e8%b0%83%e6%95%b4%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/www.gzlcy.cn\/?p=407","title":{"rendered":"excel \u8868\u683c\u590d\u5236\u5230word\u540e\uff0c\u5bbd\u8d85\u51faword\u5982\u4f55\u8c03\u6574\uff1f"},"content":{"rendered":"<p>### \u56de\u7b541\uff1a\u8981\u5c06Excel\u8868\u683c\u590d\u5236\u5230Word\u4e2d\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a1. \u6253\u5f00Excel\u8868\u683c\uff0c\u9009\u62e9\u8981\u590d\u5236\u7684\u8868\u683c\u533a\u57df\u30022. \u53f3\u952e\u70b9\u51fb\u9009\u4e2d\u7684\u8868\u683c\u533a\u57df\uff0c\u9009\u62e9\u201c\u590d\u5236\u201d\u6216\u4f7f\u7528\u5feb\u6377\u952e\u201cCtrl+C\u201d\u30023. \u6253\u5f00Word\u6587\u6863\uff0c\u5c06\u5149\u6807\u79fb\u52a8\u5230\u8981\u63d2\u5165\u8868\u683c\u7684\u4f4d\u7f6e\u30024. \u53f3\u952e\u70b9\u51fb\u5149\u6807\u6240\u5728\u4f4d\u7f6e\uff0c\u9009\u62e9\u201c\u7c98\u8d34\u201d\u6216\u4f7f\u7528\u5feb\u6377\u952e\u201cCtrl+V\u201d\u30025. \u5982\u679c\u9700\u8981\u8c03\u6574\u8868\u683c\u7684\u6837\u5f0f\u548c\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528Word\u4e2d\u7684\u8868\u683c\u5de5\u5177\u8fdb\u884c\u4fee\u6539\u30026. \u6700\u540e\u4fdd\u5b58Word\u6587\u6863\u5373\u53ef\u3002\u5e0c\u671b\u8fd9\u4e9b\u6b65\u9aa4\u80fd\u591f\u5e2e\u52a9\u60a8\u6210\u529f\u5c06Excel\u8868\u683c\u590d\u5236\u5230Word\u4e2d\u3002### \u56de\u7b542\uff1a\u4e00\u3001\u4ec0\u4e48\u662fPython\uff1fPython\u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\uff0c\u5b83\u7b80\u5355\u6613\u5b66\u4e14\u529f\u80fd\u5f3a\u5927\uff0c\u56e0\u6b64\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u6559\u80b2\u548c\u5de5\u4e1a\u754c\u90fd\u6df1\u53d7\u6b22\u8fce\u3002\u76ee\u524d\uff0cPython\u5df2\u7ecf\u6210\u4e3a\u6570\u636e\u79d1\u5b66\u548c\u4eba\u5de5\u667a\u80fd\u9886\u57df\u4e2d\u7684\u91cd\u8981\u5de5\u5177\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u5904\u7406\u6570\u636e\u3001\u6784\u5efa\u6a21\u578b\u3001\u5b9e\u73b0\u7b97\u6cd5\u7b49\u3002\u4e8c\u3001\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528Python\u6765\u590d\u5236Excel\u8868\u683c\u5230Word\u4e2d\uff1fExcel\u548cWord\u662f\u529e\u516c\u8f6f\u4ef6\u4e2d\u7684\u4e24\u4e2a\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u5b83\u4eec\u7684\u6570\u636e\u4ea4\u4e92\u5bf9\u6211\u4eec\u65e5\u5e38\u5de5\u4f5c\u548c\u6570\u636e\u5904\u7406\u90fd\u975e\u5e38\u91cd\u8981\u3002Python\u53ef\u4ee5\u5b9e\u73b0Excel\u6570\u636e\u7684\u8bfb\u53d6\u548c\u5904\u7406\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u64cd\u4f5cWord\u6587\u4ef6\u548c\u5b9e\u73b0\u81ea\u52a8\u5316\u6587\u6863\u751f\u6210\u3002\u5c06Excel\u8868\u683c\u590d\u5236\u5230Word\u6587\u6863\u4e2d\u53ef\u4ee5\u6709\u6548\u5730\u51cf\u5c11\u5de5\u4f5c\u91cf\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<p>\u4e09\u3001\u5982\u4f55\u4f7f\u7528Python\u6765\u5b9e\u73b0\u590d\u5236Excel\u8868\u683c\u5230Word\uff1f1. \u4f7f\u7528Python\u5e93openpyxl\u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff1b2. \u4f7f\u7528Python\u5e93python-docx\u521b\u5efaWord\u6587\u6863\u548c\u8868\u683c\uff1b3. \u5c06\u8bfb\u53d6\u7684Excel\u8868\u683c\u6570\u636e\u590d\u5236\u5230\u65b0\u5efa\u7684Word\u8868\u683c\u4e2d\uff1b4. \u4fdd\u5b58Word\u6587\u6863\u3002\u4ee5\u4e0b\u662f\u4ee3\u7801\u793a\u4f8b\uff1a&#8220;`pythonimport openpyxlfrom docx import Documentfrom docx.shared import Inches # \u6253\u5f00Excel\u6587\u4ef6\u5e76\u8bfb\u53d6\u7b2c1\u4e2a\u5de5\u4f5c\u8868wb = openpyxl.load_workbook(&#039;example.xlsx&#039;)ws = wb.worksheets[0] # \u521b\u5efaWord\u6587\u6863\u5e76\u6dfb\u52a0\u8868\u683cdoc = Document()table = doc.add_table(rows=1, cols=3) # \u590d\u5236Excel\u8868\u683c\u6570\u636e\u5230Word\u8868\u683c\u4e2dfor row in ws:row_cells = table.add_row().cellsfor i, cell in enumerate(row):row_cells[i].text = str(cell.value) # \u4fdd\u5b58Word\u6587\u6863doc.save(&#039;example.docx&#039;)&#8220;`\u56db\u3001\u603b\u7ed3\u901a\u8fc7\u4f7f\u7528Python\u53ef\u4ee5\u7b80\u5355\u5feb\u6377\u5730\u590d\u5236Excel\u8868\u683c\u5230Word\u4e2d\uff0c\u8fd9\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\uff0c\u540c\u65f6\u4e5f\u6709\u5229\u4e8e\u4fdd\u8bc1\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<p>\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5b8c\u5584\u4ee3\u7801\uff0c\u589e\u52a0\u5f02\u5e38\u5904\u7406\u548c\u6570\u636e\u6821\u9a8c\u7b49\u529f\u80fd\uff0c\u4f7f\u5904\u7406\u7ed3\u679c\u66f4\u52a0\u53ef\u9760\u548c\u7a33\u5b9a\u3002### \u56de\u7b543\uff1aPython\u662f\u4e00\u79cd\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e38\u7528\u4e8e\u6570\u636e\u5904\u7406\u3001\u6570\u636e\u5206\u6790\u7b49\u9886\u57df\u3002\u5728\u5904\u7406\u6570\u636e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5c06\u6570\u636e\u4eceExcel\u8868\u683c\u4e2d\u590d\u5236\u5230Word\u6587\u6863\u4e2d\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u6216\u8005\u5c55\u793a\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u4ee5\u4e0b\u662f\u5176\u4e2d\u4e00\u79cd\u8f83\u4e3a\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u6b65\u9aa4\u4e00\uff0c\u5b89\u88c5\u4f9d\u8d56\u5e93\u9996\u5148\u9700\u8981\u5b89\u88c5openpyxl\u548cpython-docx\u8fd9\u4e24\u4e2a\u5e93\uff0c\u7528\u4e8e\u5206\u522b\u5904\u7406Excel\u8868\u683c\u548cWord\u6587\u6863\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\u3002pip install openpyxlpip install python-docx\u6b65\u9aa4\u4e8c\uff0c\u8bfb\u53d6Excel\u8868\u683c\u4f7f\u7528openpyxl\u8bfb\u53d6Excel\u8868\u683c\uff0c\u83b7\u53d6\u9700\u8981\u590d\u5236\u7684\u6570\u636e\u3002import openpyxl# \u6253\u5f00Excel\u6587\u4ef6wb = openpyxl.load_workbook(&#039;example.xlsx&#039;)# \u9009\u62e9Sheetsheet = wb[&#039;Sheet1&#039;]# \u83b7\u53d6\u9700\u8981\u590d\u5236\u7684\u6570\u636edata = []for row in sheet.iter_rows(min_row=2, max_row=6, min_col=1, max_col=4):row_data = []for cell in row:row_data.append(cell.value)data.append(row_data)\u8fd9\u91cc\u9009\u62e9\u4e86Sheet1\u8868\u683c\u7684\u7b2c2-6\u884c\u548c\u7b2c1-4\u5217\u7684\u6570\u636e\u4f5c\u4e3a\u590d\u5236\u7684\u6570\u636e\u8303\u56f4\u3002<\/p>\n<p>\u6b65\u9aa4\u4e09\uff0c\u590d\u5236\u6570\u636e\u5230Word\u6587\u6863\u4f7f\u7528python-docx\u5e93\u521b\u5efaWord\u6587\u6863\uff0c\u5e76\u5c06\u6570\u636e\u590d\u5236\u5230\u6587\u6863\u4e2d\u7684\u8868\u683c\u4e2d\u3002import docx# \u521b\u5efa\u65b0\u6587\u6863doc = docx.Document()# \u6dfb\u52a0\u8868\u683ctable = doc.add_table(rows=6, cols=4)# \u590d\u5236\u6570\u636e\u5230\u8868\u683c\u4e2dfor i, row in enumerate(data):for j, cell in enumerate(row):table.cell(i, j).text = str(cell)# \u4fdd\u5b58\u6587\u6863doc.save(&#039;example.docx&#039;)\u8fd9\u91cc\u521b\u5efa\u4e866\u884c4\u5217\u7684\u8868\u683c\uff0c\u5e76\u5c06\u8bfb\u53d6\u7684\u6570\u636e\u590d\u5236\u5230\u8868\u683c\u4e2d\uff0c\u6700\u7ec8\u4fdd\u5b58\u4e3aexample.docx\u6587\u4ef6\u3002\u5982\u679c\u9700\u8981\u590d\u5236\u683c\u5f0f\u6216\u8005\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u6216\u8005\u81ea\u884c\u7f16\u5199\u4ee3\u7801\u5b9e\u73b0\u3002\u4ee5\u4e0a\u662fpython\u590d\u5236Excel\u8868\u683c\u5230Word\u6587\u6863\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u901a\u8fc7\u5bf9Excel\u8868\u683c\u548cWord\u6587\u6863\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u591a\u9488\u5bf9\u6027\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f51\u4e0a\u5f88\u591a\u65b9\u6cd5\u662f\u7528\u201c\u9009\u62e9\u6027\u7c98\u8d34&#8212;-excel\u8868\u683c\u5bf9\u8c61\u201d\uff0c\u8fd9\u4e2a\u9002\u7528\u8868\u683c\u884c\u6570\u5c11\u7684\uff0c\u4e0d\u8d85\u8fc7\u4e00\u9875word\u7684\u3002\u6b65\u9aa4\u590d\u5236\u5230word\u91cc\u540e\uff0c\u9009\u4e2d\u8868\u683c\uff0c\u53f3\u952e&#8212;\u81ea\u52a8\u8c03\u6574&#8211;\u9009\u62e9\u5177\u4f53\u7684\u8c03\u6574\u65b9\u5f0f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[805,806,804,660],"_links":{"self":[{"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/posts\/407"}],"collection":[{"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=407"}],"version-history":[{"count":0,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=\/wp\/v2\/posts\/407\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=407"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gzlcy.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}