{"id":1247,"date":"2024-06-02T14:30:28","date_gmt":"2024-06-02T05:30:28","guid":{"rendered":"https:\/\/daba-no-heya.com\/?p=1247"},"modified":"2024-06-02T14:30:29","modified_gmt":"2024-06-02T05:30:29","slug":"post-1247","status":"publish","type":"post","link":"https:\/\/daba-no-heya.com\/?p=1247","title":{"rendered":"\u3010Python\u3011Excel\u30a2\u30fc\u30c8(\u30c9\u30c3\u30c8\u7d75)\u3092\u4f5c\u308b"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/daba-no-heya.com\/?p=1247\/#%E6%A6%82%E8%A6%81\" >\u6982\u8981<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/daba-no-heya.com\/?p=1247\/#%E5%85%83%E7%94%BB%E5%83%8F\" >\u5143\u753b\u50cf<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/daba-no-heya.com\/?p=1247\/#%E5%AE%8C%E6%88%90%E5%93%81\" >\u5b8c\u6210\u54c1<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/daba-no-heya.com\/?p=1247\/#%E5%AE%9F%E8%A3%85\" >\u5b9f\u88c5<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/daba-no-heya.com\/?p=1247\/#Python%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88\" >Python\u30b9\u30af\u30ea\u30d7\u30c8<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/daba-no-heya.com\/?p=1247\/#%E5%AE%9F%E8%A1%8C%E6%96%B9%E6%B3%95\" >\u5b9f\u884c\u65b9\u6cd5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/daba-no-heya.com\/?p=1247\/#%E8%A7%A3%E8%AA%AC\" >\u89e3\u8aac<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%A6%82%E8%A6%81\"><\/span>\u6982\u8981<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4e00\u53e3\u306bExcel\u30a2\u30fc\u30c8\u3068\u8a00\u3063\u3066\u3082\u8272\u3005\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u4eca\u56de\u306f\u5143\u3068\u306a\u308b\u753b\u50cf\u304b\u3089\u30c9\u30c3\u30c8\u7d75\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br>\u306a\u304a\u3001Excel\u30a2\u30fc\u30c8\u3068\u306f\u8a00\u3044\u3064\u3064\u3001\u7ba1\u7406\u4eba\u306e\u74b0\u5883\u304cZorin OS\u304b\u3064Excel\u306f\u3082\u3063\u3066\u3044\u306a\u3044\u306e\u3067\u3001\u4eca\u56de\u306e\u5b9f\u884c\u7d50\u679c\u306fLibreOffice Calc\u3067\u78ba\u8a8d\u3057\u305f\u7d50\u679c\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5143\u753b\u50cf\u3068\u5b8c\u6210\u54c1\u306e\u30a4\u30e1\u30fc\u30b8\u3092\u4ee5\u4e0b\u306b\u63b2\u8f09\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%85%83%E7%94%BB%E5%83%8F\"><\/span>\u5143\u753b\u50cf<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-medium\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"300\" src=\"https:\/\/daba-no-heya.com\/wp-content\/uploads\/2024\/06\/image_resized-200x300.png\" alt=\"\" class=\"wp-image-1248\" srcset=\"https:\/\/daba-no-heya.com\/wp-content\/uploads\/2024\/06\/image_resized-200x300.png 200w, https:\/\/daba-no-heya.com\/wp-content\/uploads\/2024\/06\/image_resized.png 500w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AE%8C%E6%88%90%E5%93%81\"><\/span>\u5b8c\u6210\u54c1<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"833\" height=\"987\" src=\"https:\/\/daba-no-heya.com\/wp-content\/uploads\/2024\/06\/Screenshot-from-2024-06-02-13-50-55.png\" alt=\"\" class=\"wp-image-1249\" srcset=\"https:\/\/daba-no-heya.com\/wp-content\/uploads\/2024\/06\/Screenshot-from-2024-06-02-13-50-55.png 833w, https:\/\/daba-no-heya.com\/wp-content\/uploads\/2024\/06\/Screenshot-from-2024-06-02-13-50-55-253x300.png 253w, https:\/\/daba-no-heya.com\/wp-content\/uploads\/2024\/06\/Screenshot-from-2024-06-02-13-50-55-768x910.png 768w\" sizes=\"auto, (max-width: 833px) 100vw, 833px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AE%9F%E8%A3%85\"><\/span>\u5b9f\u88c5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306a\u3069\u306f<a href=\"https:\/\/github.com\/maeda6uiui\/ExcelArtCreator\" data-type=\"link\" data-id=\"https:\/\/github.com\/maeda6uiui\/ExcelArtCreator\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub<\/a>\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88\"><\/span>Python\u30b9\u30af\u30ea\u30d7\u30c8<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-python\"><code class=\"language-python\">import argparse\nimport openpyxl\nfrom openpyxl.worksheet.worksheet import Worksheet\nfrom openpyxl.styles import PatternFill\nfrom PIL import Image\n\ndef main(args):\n    image_filepath:str=args.image_filepath\n    output_filepath:str=args.output_filepath\n    row_height:float=args.row_height\n    column_width:float=args.column_width\n    num_rows:int=args.num_rows\n    num_columns:int=args.num_columns\n\n    #Resize image\n    im=Image.open(image_filepath)\n    im_resized=im.resize((num_columns,num_rows))\n\n    #Create workbook\n    wb=openpyxl.Workbook()\n    ws:Worksheet=wb.active\n\n    #Set row height and column width\n    for i in range(1,num_rows+1):\n        ws.row_dimensions[i].height=row_height\n    for i in range(1,num_columns+1):\n        ws.column_dimensions[ws.cell(row=1,column=i).column_letter].width=column_width\n\n    #Set color of each pixel to cell\n    for i in range(1,num_columns+1):\n        for j in range(1,num_rows+1):\n            pixel=im_resized.getpixel((i-1,j-1))\n            ws.cell(row=j,column=i).fill=PatternFill(\n                patternType=\"solid\",\n                fgColor=f\"{pixel[0]:02x}{pixel[1]:02x}{pixel[2]:02x}\"\n            )\n\n    #Save workbook\n    wb.save(output_filepath)\n\nif __name__==\"__main__\":\n    parser=argparse.ArgumentParser()\n    parser.add_argument(\"-i\",\"--image-filepath\",type=str)\n    parser.add_argument(\"-o\",\"--output-filepath\",type=str,default=\"output.xlsx\")\n    parser.add_argument(\"--row-height\",type=float,default=3)\n    parser.add_argument(\"--column-width\",type=float,default=0.5)\n    parser.add_argument(\"-nr\",\"--num-rows\",type=int,default=256)\n    parser.add_argument(\"-nc\",\"--num-columns\",type=int,default=256)\n    args=parser.parse_args()\n\n    main(args)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AE%9F%E8%A1%8C%E6%96%B9%E6%B3%95\"><\/span>\u5b9f\u884c\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u5165\u529b\u3068\u306a\u308b\u753b\u50cf\u306e\u30d5\u30a1\u30a4\u30eb\u30d1\u30b9\u3068\u51fa\u529b\u3059\u308bExcel\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u5f15\u6570\u3067\u6307\u5b9a\u3057\u3066\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python convert_image.py -i image.png -o output.xlsx<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%A7%A3%E8%AA%AC\"><\/span>\u89e3\u8aac<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Python\u304b\u3089Excel\u30d5\u30a1\u30a4\u30eb\u3092\u64cd\u4f5c\u3059\u308b\u305f\u3081\u306bopenpyxl\u3001\u753b\u50cf\u51e6\u7406\u3092\u884c\u3046\u305f\u3081\u306bPillow\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5f15\u6570<code>--row-height<\/code>\u3068<code>--column-width<\/code>\u3067\u305d\u308c\u305e\u308cExcel\u30b7\u30fc\u30c8\u306e\u884c\u306e\u9ad8\u3055\u3068\u5217\u306e\u5e45\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<br>\u3053\u308c\u3089\u306e\u5f15\u6570\u306b\u3088\u3063\u3066\u51fa\u529b\u7d50\u679c\u306e\u30a2\u30b9\u30da\u30af\u30c8\u6bd4\u3092\u5909\u66f4\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5f15\u6570<code>--num-rows<\/code>\u3068<code>--num-columns<\/code>\u3067\u305d\u308c\u305e\u308c\u753b\u50cf\u3092\u51fa\u529b\u3059\u308b\u884c\u6570\u3068\u5217\u6570\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<br>\u3053\u308c\u3089\u306e\u5f15\u6570\u306b\u3088\u3063\u3066\u51fa\u529b\u7d50\u679c\u306e\u30c9\u30c3\u30c8\u306e\u7c97\u3055\u3092\u5909\u66f4\u3067\u304d\u307e\u3059\u3002<br>\u4ee5\u4e0b\u306f\u884c\u6570\u3068\u5217\u6570\u3092128\u306b\u3057\u3066\u5b9f\u884c\u3057\u305f\u7d50\u679c\u3067\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"833\" height=\"987\" src=\"https:\/\/daba-no-heya.com\/wp-content\/uploads\/2024\/06\/Screenshot-from-2024-06-02-14-13-03.png\" alt=\"\" class=\"wp-image-1250\" srcset=\"https:\/\/daba-no-heya.com\/wp-content\/uploads\/2024\/06\/Screenshot-from-2024-06-02-14-13-03.png 833w, https:\/\/daba-no-heya.com\/wp-content\/uploads\/2024\/06\/Screenshot-from-2024-06-02-14-13-03-253x300.png 253w, https:\/\/daba-no-heya.com\/wp-content\/uploads\/2024\/06\/Screenshot-from-2024-06-02-14-13-03-768x910.png 768w\" sizes=\"auto, (max-width: 833px) 100vw, 833px\" \/><\/figure>\n<\/div>\n\n\n<p>Excel\u306e\u30bb\u30eb\u3067\u753b\u50cf\u3092\u8868\u73fe\u3059\u308b\u969b\u3001\u3053\u3053\u3067\u6307\u5b9a\u3059\u308b\u884c\u6570\u00d7\u5217\u6570\u5206\u306e\u30d4\u30af\u30bb\u30eb\u304c\u3042\u308c\u3070\u3088\u3044\u305f\u3081\u3001\u5165\u529b\u3068\u306a\u308b\u753b\u50cf\u3092\u884c\u6570\u00d7\u5217\u6570\u306e\u30b5\u30a4\u30ba\u306b\u30ea\u30b5\u30a4\u30ba\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 \u4e00\u53e3\u306bExcel\u30a2\u30fc\u30c8\u3068\u8a00\u3063\u3066\u3082\u8272\u3005\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u4eca\u56de\u306f\u5143\u3068\u306a\u308b\u753b\u50cf\u304b\u3089\u30c9\u30c3\u30c8\u7d75\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u306a\u304a\u3001Excel\u30a2\u30fc\u30c8\u3068\u306f\u8a00\u3044\u3064\u3064\u3001\u7ba1\u7406\u4eba\u306e\u74b0\u5883\u304cZorin OS\u304b\u3064Excel\u306f\u3082\u3063\u3066\u3044\u306a\u3044\u306e\u3067\u3001\u4eca\u56de\u306e\u5b9f\u884c\u7d50\u679c\u306f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-1247","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=\/wp\/v2\/posts\/1247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1247"}],"version-history":[{"count":2,"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=\/wp\/v2\/posts\/1247\/revisions"}],"predecessor-version":[{"id":1252,"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=\/wp\/v2\/posts\/1247\/revisions\/1252"}],"wp:attachment":[{"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daba-no-heya.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}