{"id":352,"date":"2018-11-20T13:22:12","date_gmt":"2018-11-20T05:22:12","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=352"},"modified":"2022-05-17T09:44:45","modified_gmt":"2022-05-17T01:44:45","slug":"python-docx","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=352","title":{"rendered":"\u901a\u8fc7python-docx\u7ed9word\u6587\u6863\u4e2d\u7684\u6307\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u8868\u683c"},"content":{"rendered":"<h1><strong>\u9700\u6c42<\/strong><\/h1>\n<h6>1.\u8bfb\u53d6\u4e00\u4e2a\u5df2\u6709\u7684word\u6587\u6863\u3002docx\u683c\u5f0f\u3002<\/h6>\n<h6>2.\u5728\u8be5word\u6587\u6863\u4e2d\uff0c\u901a\u8fc7\u4e00\u4e2a\u7ed9\u5b9a\u7684\u6587\u5b57\u3002\u627e\u5230\u8be5\u4f4d\u7f6e\u3002\u5728\u8be5\u4f4d\u7f6e\u7684\u4e0b\u65b9\u6dfb\u52a0\u4e00\u4e2a\u8868\u683c\u3002\u4f8b\u5982\u5728\u56fe\u4e2d\u201cBUG\u60c5\u51b5\u8868\u201d\u7684\u4e0b\u65b9\u63d2\u5165\u4e00\u4e2a\u8868\u683c<\/h6>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-353 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/word\u6587\u6863\u622a\u56fe.png\" alt=\"\" width=\"869\" height=\"738\" \/><\/p>\n<h6>3.\u8868\u683c\u5185\u5bb9\u5982\u4e0b\u3002\u8981\u6c42\u6dfb\u52a0\u5b8c\u8be5\u8868\u683c\u540e\uff0c\u5982\u679c\u8868\u683c\u5185\u5bb9\u53d1\u751f\u53d8\u66f4\u3002\u8fd8\u80fd\u518d\u6b21\u901a\u8fc7\u8be5\u7a0b\u5e8f\uff0c\u4fee\u6539\u8868\u683c\u91cc\u7684\u6570\u636e\u3002<\/h6>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-354 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/excel\u6587\u6863\u622a\u56fe.png\" alt=\"\" width=\"882\" height=\"288\" \/><\/p>\n<h1>\u8bbe\u8ba1<\/h1>\n<h6>\u901a\u8fc7python-docx\u8bfb\u53d6word\u6587\u6863\u3002\u901a\u8fc7document.paragraphs\u5b9a\u4f4d\u6307\u5b9a\u6587\u5b57\u7684\u4f4d\u7f6e\u3002<\/h6>\n<h6>\u901a\u8fc7xlwings\u8bfb\u53d6excel\u7684\u5185\u5bb9\uff0c\u5b58\u6210list[list[]]\u3002<\/h6>\n<h6>\u901a\u8fc7docx\u7684add_table\u589e\u52a0\u4e00\u4e2a\u8868\u683c\uff0c\u5e76\u4e14\u66f4\u6539\u8868\u5934\u989c\u8272\uff0c\u5408\u5e76\u8868\u683c\u7b49\u64cd\u4f5c<\/h6>\n<h6>\u901a\u8fc7\u8bc6\u522b\u8868\u5934\u7684\u7b2c\u4e00\u884c\uff0c\u5224\u65ad\u662f\u5426\u662f\u5df2\u7ecf\u5b58\u5728\u8fd9\u4e2a\u8868\u683c\uff0c\u6765\u51b3\u5b9a\u662f\u5426\u8981\u5220\u9664\u539f\u8868\u683c<\/h6>\n<h1>\u4ee3\u7801<\/h1>\n<pre class=\"lang:python decode:true  EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\"># -*- coding: UTF-8 -*-\nimport sys\nfrom copy import deepcopy\nimport xlwings\nfrom docx import Document\nfrom docx.oxml.ns import nsdecls\nfrom docx.oxml import parse_xml\ndef copy_table_after(table, paragraph):\n    tbl, p = table._tbl, paragraph._p\n    new_tbl = deepcopy(tbl)\n    p.addnext(new_tbl)\ndef move_table_after(table, paragraph):\n    tbl, p = table._tbl, paragraph._p\n    p.addnext(tbl)\ndef get_excel_date(filename):\n    '''\n    \u83b7\u5f97excel\u91cc\u7684\u6240\u6709\u5185\u5bb9\uff0c\u8fd4\u56delist\n    :param filename:  excel\u8def\u5f84\n    :return: list[list[]]\n    '''\n    app = xlwings.App(visible=False, add_book=True)\n    app.display_alerts = False\n    app.screen_updating = False\n    wb = app.books.open(filename)\n    sht = wb.sheets[0]\n    rng = sht.range('A1')\n    # \u628aexcel\u91cc\u7684\u6570\u636e\u8bfb\u53d6\u6210 \u5e74-\u6708-\u65e5 \u65f6:\u5206:\u79d2\u7684\u683c\u5f0f\n    my_date_handler = lambda year, month, day, hour, minute, second, **kwargs: \"%04i-%02i-%02i %02i:%02i:%02i\" % (\n        year, month, day, hour, minute, second)\n    # \u53d6\u51fa\u6240\u6709\u5185\u5bb9,\u8fd9\u91cc\u7528ig\u8fd9\u4e2a\u53d8\u91cf\uff0c\u662f\u4e3a\u4e86\u5e86\u795dI.G\u83b7\u5f97LOL S8\u8d5b\u5b63\u603b\u51a0\u519b\n    ig = rng.current_region.options(index=False, numbers=int, empty='N\/A', dates=my_date_handler)\n    result = ig.value\n    wb.close()\n    app.quit()\n    return result\ndef delete_table_with_title(document,expect_text):\n    allTables = document.tables\n    for activeTable in allTables:\n        if activeTable.cell(0, 0).paragraphs[0].text == expect_text:\n            print('\u5220\u9664\u6210\u529f')\n            activeTable._element.getparent().remove(activeTable._element)\ndef insert_table_after_text(file_name,excel_name,expect_text):\n    document = Document(file_name)\n    # \u56e0\u4e3adocx\u8bfb\u51fa\u6765\u7684\u90fd\u662funicode\u7c7b\u578b\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u8981\u7528unicode\u7c7b\u578b\u7684\u8fdb\u884c\u67e5\u627e\n    expect_text=expect_text.decode('utf-8')\n    delete_table_with_title(document,expect_text)\n    target = None\n    for paragraph in document.paragraphs:\n        paragraph_text = paragraph.text\n        if paragraph_text.endswith(expect_text):\n            target = paragraph\n            break\n    if target is not None:\n        records = get_excel_date(excel_name)\n        # \u83b7\u5f97excel\u6570\u636e\u7684\u680f\u6570\uff0c\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u7684table\n        col = len(records[0])\n        table = document.add_table(rows=1, cols=col)\n        table.style = 'Table Grid'\n        # \u7ed9table\u52a0\u4e00\u4e2a\u8868\u5934\uff0c\u5e76\u4e14\u5408\u5e76\u7b2c\u4e00\u680f\n        shading_elm_1 = parse_xml(r'&lt;w:shd {} w:fill=\"D9E2F3\"\/&gt;'.format(nsdecls('w')))\n        table.rows[0].cells[0]._tc.get_or_add_tcPr().append(shading_elm_1)\n        table.rows[0].cells[0].text=expect_text\n        table_row=table.rows[0]\n        first=table_row.cells[0]\n        end=table_row.cells[-1]\n        first.merge(end)\n        # \u5408\u5e76\u7ed3\u675f\uff0c\u5f00\u59cb\u628aexcel\u91cc\u7684\u5185\u5bb9\u6dfb\u52a0\u5230table\u91cc\n        for tr_list in records:\n            row_cells = table.add_row().cells\n            index = 0\n            for td_list in tr_list:\n                row_cells[index].text = td_list\n                index = index + 1\n        # \u628a\u6dfb\u52a0\u7684table\u79fb\u52a8\u5230\u6307\u5b9a\u7684\u4f4d\u7f6e\n        move_table_after(table, target)\n        # \u4fdd\u5b58\n    document.save(file_name)\nif __name__ == '__main__':\n    insert_table_after_text('demo2.docx', 'demo.xlsx',\"BUG\u60c5\u51b5\u8868\")<\/code><\/pre>\n<h1>\u6700\u7ec8\u6548\u679c<\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-355 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/\u6210\u529f\u6dfb\u52a0\u8868\u683c.png\" alt=\"\" width=\"772\" height=\"990\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42 1.\u8bfb\u53d6\u4e00\u4e2a\u5df2\u6709\u7684word\u6587\u6863\u3002docx\u683c\u5f0f\u3002 2.\u5728\u8be5word\u6587\u6863\u4e2d\uff0c\u901a\u8fc7\u4e00\u4e2a\u7ed9\u5b9a\u7684\u6587\u5b57\u3002\u627e\u5230\u8be5\u4f4d\u7f6e\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-352","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=352"}],"version-history":[{"count":2,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/352\/revisions"}],"predecessor-version":[{"id":1076,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/352\/revisions\/1076"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}