{"id":12439,"date":"2024-11-15T07:31:34","date_gmt":"2024-11-14T22:31:34","guid":{"rendered":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/?p=12439"},"modified":"2024-11-15T07:31:34","modified_gmt":"2024-11-14T22:31:34","slug":"py-docx","status":"publish","type":"post","link":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/2024\/11\/15\/py-docx\/","title":{"rendered":"\u3010Python\u3011Microsoft Word\u306e\u8868\u306e\u6307\u5b9a\u5834\u6240\u306b\u6587\u5b57\u3092\u5165\u529b\u3059\u308b"},"content":{"rendered":"<p>Python\u3067Word\u6587\u66f8\u306e\u8868\u306b\u6587\u5b57\u3092\u5165\u529b\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u3001python-docx\u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\u306e\u304c\u4fbf\u5229\u3067\u3059\u3002\u3053\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001Word\u6587\u66f8\uff08.docx\u5f62\u5f0f\uff09\u306e\u8aad\u307f\u66f8\u304d\u304c\u7c21\u5358\u306b\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u3067\u306f\u3001\u65e2\u5b58\u306eWord\u6587\u66f8\u306b\u542b\u307e\u308c\u308b\u8868\u306b\u6587\u5b57\u3092\u5165\u529b\u3059\u308b\u624b\u9806\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<p>\u307e\u305a\u3001python-docx\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install python-docx\r\n \r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>Word\u6587\u66f8\u306e\u8868\u306b\u6587\u5b57\u3092\u5165\u529b\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/h2>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom docx import Document\r\n\r\n# \u65e2\u5b58\u306eWord\u6587\u66f8\u3092\u8aad\u307f\u8fbc\u3080\r\ndoc = Document(r&quot;C:\\Users\\satos\\Dropbox\\_Source\\Python\\Word\\\u6587\u66f81.docx&quot;)\r\n\r\n# \u6587\u66f8\u5185\u306e\u8868\u306b\u30a2\u30af\u30bb\u30b9\r\ntables = doc.tables\r\n\r\n# 1\u3064\u76ee\u306e\u8868\u304c\u3042\u308b\u5834\u5408\u3001\u305d\u306e\u8868\u306b\u30a2\u30af\u30bb\u30b9\r\nif tables:\r\n    table = tables&#x5B;0]  # \u6700\u521d\u306e\u8868\u3092\u9078\u629e\r\n    \r\n    # \u8868\u306e\u884c\u3068\u5217\u3092\u6307\u5b9a\u3057\u3066\u6587\u5b57\u3092\u5165\u529b\r\n    table.cell(0, 0).text = 'Hello, World!'  # 1\u884c\u76ee1\u5217\u76ee\u306e\u30bb\u30eb\r\n    table.cell(1, 1).text = 'Python-docx'    # 2\u884c\u76ee2\u5217\u76ee\u306e\u30bb\u30eb\r\n\r\n# \u5909\u66f4\u3092\u4fdd\u5b58\r\ndoc.save('output.docx')\r\nprint(&quot;\u8868\u306b\u6587\u5b57\u3092\u5165\u529b\u3057\u307e\u3057\u305f\uff01&quot;)\r\n \r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u30b3\u30fc\u30c9\u306e\u8aac\u660e<\/h2>\n<ul>\n<li><strong>Document(&#8216;sample.docx&#8217;)<\/strong>: \u65e2\u5b58\u306eWord\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002<\/li>\n<li><strong>tables = doc.tables<\/strong>: \u6587\u66f8\u5185\u306e\u3059\u3079\u3066\u306e\u8868\u3092\u30ea\u30b9\u30c8\u3068\u3057\u3066\u53d6\u5f97\u3057\u307e\u3059\u3002<\/li>\n<li>table.cell(row, col).text: \u6307\u5b9a\u3057\u305f\u30bb\u30eb\uff08\u884c\u3068\u5217\uff09\u306b\u6587\u5b57\u5217\u3092\u5165\u529b\u3057\u307e\u3059\u3002<\/li>\n<li>\u884c\u3068\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f0\u304b\u3089\u59cb\u307e\u308b\u306e\u3067\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/li>\n<li><strong>doc.save(&#8216;output.docx&#8217;)<\/strong>: \u5909\u66f4\u5185\u5bb9\u3092\u65b0\u3057\u3044Word\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u4fdd\u5b58\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<h2>\u5b9f\u884c\u7d50\u679c<\/h2>\n<p>\u5b9f\u884c\u7d50\u679c\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002\u3061\u3083\u3093\u3068\u6307\u5b9a\u3057\u305f\u8868\u306b\u6587\u5b57\u3092\u5165\u529b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-content\/uploads\/2024\/11\/image2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-12441\" src=\"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-content\/uploads\/2024\/11\/image2-700x199.png\" alt=\"\" width=\"872\" height=\"248\" srcset=\"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-content\/uploads\/2024\/11\/image2-700x199.png 700w, http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-content\/uploads\/2024\/11\/image2-900x255.png 900w, http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-content\/uploads\/2024\/11\/image2.png 1407w\" sizes=\"auto, (max-width: 872px) 100vw, 872px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>4. \u6ce8\u610f\u70b9<\/h2>\n<p>python-docx\u306f<strong>.docx\u5f62\u5f0f\u306e\u30d5\u30a1\u30a4\u30eb\u3057\u304b\u6271\u3048\u307e\u305b\u3093<\/strong>\u3002.doc\u5f62\u5f0f\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u4e8b\u524d\u306b.docx\u5f62\u5f0f\u306b\u5909\u63db\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u8868\u304c\u8907\u6570\u3042\u308b\u5834\u5408\u306f\u3001tables[1]\u306e\u3088\u3046\u306b\u3057\u30662\u3064\u76ee\u306e\u8868\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u3067Word\u6587\u66f8\u306e\u8868\u306b\u6587\u5b57\u3092\u5165\u529b\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u3001python-doc &#8230; <\/p>\n","protected":false},"author":1,"featured_media":12443,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-12439","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts\/12439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/comments?post=12439"}],"version-history":[{"count":3,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts\/12439\/revisions"}],"predecessor-version":[{"id":12444,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts\/12439\/revisions\/12444"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/media\/12443"}],"wp:attachment":[{"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/media?parent=12439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/categories?post=12439"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/tags?post=12439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}