{"id":3672,"date":"2022-04-01T09:06:29","date_gmt":"2022-04-01T17:36:29","guid":{"rendered":"http:\/\/10ccc.cc\/?p=3672"},"modified":"2026-01-02T20:33:18","modified_gmt":"2026-01-02T12:33:18","slug":"excel%e5%83%8f%e7%b4%a0%e7%94%bb","status":"publish","type":"post","link":"https:\/\/10ccc.cc\/?p=3672","title":{"rendered":"Excel\u50cf\u7d20\u753b"},"content":{"rendered":"<blockquote><p>\u672c\u6587\u7531\u672c\u4eba\u5728 2022-1-21 11:37 \u53d1\u8868\u4e8e<a href=\"https:\/\/www.52pojie.cn\/forum.php?mod=viewthread&amp;tid=1578885\" target=\"_blank\"  rel=\"nofollow\" >\u543e\u7231\u7834\u89e3<\/a><\/p><\/blockquote>\n<p>\u4e4b\u524d\u770b\u4e86\u300a82\u5c81\u8001\u4eba\u7528Excel\u753b\u753b\uff0c\u83b7\u5956\u65e0\u6570\u8fd8\u5f00\u753b\u5c55\uff01\u800c\u4f60\u53ea\u4f1a\u505a\u8868\u683c\u300b\u8fd9\u4e2a\u6545\u4e8b\u4e4b\u540e\u3002<span style=\"font-family: -apple-system, BlinkMacSystemFont, &amp;quot;\">\u5ffd\u7136\u60f3\u5230\u56fe\u7247\u7531\u50cf\u7d20\u6784\u6210\uff0cexcel\u7684sheet\u7531\u5355\u5143\u683c\u6784\u6210\uff0c\u00a0<\/span><br \/>\n<span style=\"font-family: -apple-system, BlinkMacSystemFont, &amp;quot;\">\u50cf\u7d20\u6709\u989c\u8272\uff0c\u5355\u5143\u683c\u53ef\u4ee5\u586b\u5145\u989c\u8272\u3002<\/span><br \/>\n<span style=\"font-family: -apple-system, BlinkMacSystemFont, &amp;quot;\"><br \/>\n<\/span><br \/>\n<span style=\"font-family: -apple-system, BlinkMacSystemFont, &amp;quot;\">\u4e5f\u5c31\u662f\u8bf4\uff1a\u00a0 \u00a0\u50cf\u7d20&lt;=&gt;\u5355\u5143\u683c\uff1b<\/span><br \/>\n<span style=\"font-family: -apple-system, BlinkMacSystemFont, &amp;quot;\">\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u56fe\u7247\u6bcf\u4e2a\u50cf\u7d20\u7684\u989c\u8272\u8bfb\u53d6\u51fa\u6765\uff0c\u5e76\u6839\u636e\u50cf\u7d20\u5750\u6807\u6620\u5c04\u5230excel\u5bf9\u5e94\u7684\u5355\u5143\u683c\u4e2d\u3002<\/span><br \/>\n<span style=\"font-family: -apple-system, BlinkMacSystemFont, &amp;quot;\">\u800c\u4e14\uff0cpython\u64cd\u4f5c\u56fe\u7247\u4ee5\u53caexcel\u7684\u5e93\u90fd\u6bd4\u8f83\u6210\u719f\uff0c\u5e94\u8be5\u5f88\u597d\u64cd\u4f5c<\/span><br \/>\n\u7406\u8bba\u5b58\u5728\uff0c\u90a3\u4e48\u5c31\u52a8\u624b\u5f00\u5e72\u4e86\u3002<\/p>\n<p>\u5148\u770b\u6548\u679c\uff1a<\/p>\n<p><iframe loading=\"lazy\" src=\"\/\/player.bilibili.com\/player.html?aid=595391722&amp;bvid=BV1Aq4y1a7d7&amp;cid=569083312&amp;page=1\" width=\"512px\" height=\"288px\" frameborder=\"no\" scrolling=\"no\" align=\"center\" allowfullscreen=\"allowfullscreen\"> <\/iframe><\/p>\n<blockquote><p>\u6211\u8fd9\u91cc\u7528\u7684\u662f\u82b1\u74e3\u5927\u795e\u7684\u56fe\uff0c\u5927\u5bb6\u53ef\u4ee5\u6362\u6210\u81ea\u5df1\u559c\u6b22\u7684\u56fe<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_3674\" aria-describedby=\"caption-attachment-3674\" style=\"width: 600px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3674\" src=\"http:\/\/10ccc.cc\/wp-content\/uploads\/2022\/04\/112116myiimyoirr47osi5-1024x515.png\" alt=\"\u539f\u56fe\" width=\"600\" height=\"302\" srcset=\"https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/04\/112116myiimyoirr47osi5-1024x515.png 1024w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/04\/112116myiimyoirr47osi5-300x151.png 300w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/04\/112116myiimyoirr47osi5-768x386.png 768w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/04\/112116myiimyoirr47osi5-1536x772.png 1536w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/04\/112116myiimyoirr47osi5.png 1808w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption id=\"caption-attachment-3674\" class=\"wp-caption-text\">\u539f\u56fe<\/figcaption><\/figure>\n<figure id=\"attachment_3673\" aria-describedby=\"caption-attachment-3673\" style=\"width: 600px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3673\" src=\"http:\/\/10ccc.cc\/wp-content\/uploads\/2022\/04\/112241bmo6qoozab36m3zo.png\" alt=\"\u8f6c\u5316\u540e\u6548\u679c\" width=\"600\" height=\"373\" srcset=\"https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/04\/112241bmo6qoozab36m3zo.png 778w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/04\/112241bmo6qoozab36m3zo-300x187.png 300w, https:\/\/10ccc.cc\/wp-content\/uploads\/2022\/04\/112241bmo6qoozab36m3zo-768x478.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption id=\"caption-attachment-3673\" class=\"wp-caption-text\">\u8f6c\u5316\u540e\u6548\u679c<\/figcaption><\/figure>\n<div class=\"dm-code-snippet dark no-background no-background-mobile dm-normal-version\" style=\"background-color:#abb8c3;\" snippet-height=\"\">\n\t\t\t<div class=\"control-language\">\n                <div class=\"dm-buttons\">\n                    <div class=\"dm-buttons-left\">\n                        <div class=\"dm-button-snippet red-button\"><\/div>\n                        <div class=\"dm-button-snippet orange-button\"><\/div>\n                        <div class=\"dm-button-snippet green-button\"><\/div>\n                    <\/div>\n                    <div class=\"dm-buttons-right\">\n                        <a id=\"dm-copy-raw-code\">\n                        <span class=\"dm-copy-text\">Copy Code<\/span>\n                        <span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span>\n                        <span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a>\n                    <\/div>\n                <\/div>\n                <pre class=\"no-line-numbers\"><code id=\"dm-code-raw\" class=\"no-wrap language-python\"><\/p>\n<pre class=\"dm-pre-admin-side\"># - * - coding:utf-8 - * -\r\n#  \u4f5c\u8005\uff1aElias Cheung\r\n#  \u7f16\u5199\u65f6\u95f4\uff1a2022\/1\/20  14:42\r\nimport math\r\nfrom xlwt import *\r\nimport xlwings as xw\r\nfrom PIL import Image\r\nfrom typing import Tuple\r\n\r\n\r\ndef resize_image(original_pic_path: str, resized_pic_path: str, max_resize_width: int = 256) -&gt; Tuple[str, tuple]:\r\n    \"\"\"\r\n    :param original_pic_path: \u3010\u8f93\u5165\u3011\u9700\u8981\u8f6c\u6362\u6210excel\u5355\u5143\u683c\u586b\u5145\u7684\u539f\u59cb\u56fe\u7247A\r\n    :param resized_pic_path: \u3010\u8f93\u51fa\u3011\u7ecf\u8fc7\u7f29\u653e\u540e\u7684\u56fe\u7247A\r\n    :param max_resize_width: \u7f29\u653e\u540e\u56fe\u7247\u7684\u6700\u5927\u5bbd\u5ea6\uff0c\u7531\u4e8exlwings\u9650\u5236\uff0c\u4e0d\u53ef\u8d85\u8fc7256\r\n    :return:\r\n    \"\"\"\r\n    img = Image.open(original_pic_path)\r\n    img_width, img_height = img.size\r\n\r\n    #  \u4ee5max_resize_width\u4e3a\u57fa\u51c6\u6bd4\u4f8b\uff0c\u8ba1\u7b97\u56fe\u7247\u7b49\u6bd4\u7f29\u653e\u540e\u7684\u5bbd\u9ad8\r\n    resize_width, resize_height = max_resize_width, math.ceil(img_height * max_resize_width \/ img_width)\r\n    # print(resize_width, resize_height)\r\n    resized_image = img.resize((resize_width, resize_height))\r\n    resized_image.save(resized_pic_path)\r\n    return resized_pic_path, (resize_width, resize_height)\r\n\r\n\r\ndef pixel_colour(image_path: str, image_size: tuple) -&gt; list:\r\n    \"\"\"\r\n    :param image_path: \u7ecf\u8fc7\u7f29\u653e\u7684\u56fe\u7247\u7684\u8def\u5f84\uff0c\u53ef\u7531resize_image()\u8fd4\u56de\r\n    :param image_size: \u7ecf\u8fc7\u7f29\u653e\u540e\u7684\u56fe\u7247\u7684\u5bbd\u9ad8\uff0c\u53ef\u7531resize_image()\u8fd4\u56de\r\n    :return: \u6bcf\u4e2a\u50cf\u7d20\u70b9\u7684rgb\u70b9\u53ca\u5bf9\u5e94\u7684\u50cf\u7d20\u5750\u6807\r\n    \"\"\"\r\n    img = Image.open(image_path)\r\n    img = img.convert('RGB')\r\n    colour_list = img.load()\r\n\r\n    img_width, img_height = image_size\r\n    pixel_coordinates = [(i, j) for i in range(img_width) for j in range(img_height)]\r\n\r\n    coordinates_and_colour_to_be_returned = [(colour_list[i, j], (i, j)) for i, j in pixel_coordinates]\r\n\r\n    return coordinates_and_colour_to_be_returned\r\n\r\n\r\ndef create_excel(size: tuple, cell_width: int=2) -&gt; str:\r\n    \"\"\"\r\n    :param size: excel\u753b\u5e03\u7684\u5c3a\u5bf8\r\n    :param cell_width: \u6bcf\u4e2a\u5355\u5143\u683c\u7684\u5bbd\u5ea6\uff0c\u4e0d\u5efa\u8bae\u8c03\u6574\uff0c\u6b64\u9ed8\u8ba4\u53c2\u6570\u53ef\u5c06\u5355\u4e2a\u5355\u5143\u683c\u5927\u81f4\u8c03\u6574\u4e3a\u6b63\u65b9\u5f62\r\n    :return: \u521b\u5efa\u5e76\u7ecf\u8fc7\u5355\u5143\u683c\u8c03\u6574\u7684excel\u8def\u5f84\uff0c\u9ed8\u8ba4\u4e3a\u5f53\u524d\u8def\u5f84\r\n    \"\"\"\r\n    cell_width = 256 * cell_width\r\n    _book = Workbook(encoding=\"utf - 8\")\r\n    sheet = _book.add_sheet(\"picture\", cell_overwrite_ok=True)\r\n    for i in range(size[0]):\r\n        first_col = sheet.col(i)\r\n        first_col.width = cell_width\r\n    book_name = \"picture.xls\"\r\n    _book.save(book_name)\r\n    return book_name\r\n\r\n\r\ndef draw_excel(excel: str, colour_and_cord: list):\r\n    \"\"\"\r\n    :param excel: \u521b\u5efa\u5e76\u7ecf\u8fc7\u5355\u5143\u683c\u8c03\u6574\u7684excel\u8def\u5f84\uff0c\u9ed8\u8ba4\u4e3a\u5f53\u524d\u8def\u5f84\r\n    :param colour_and_cord: \u6bcf\u4e2a\u5355\u5143\u683c\u7684rgb\u70b9\u53ca\u5bf9\u5e94\u7684\u50cf\u7d20\u5750\u6807\r\n    :return:\r\n    \"\"\"\r\n    app = xw.App(visible=False, add_book=False)\r\n    wb = app.books.open(excel)\r\n    sht = wb.sheets[0]\r\n    print(sht)\r\n    for cell_info in colour_and_cord:\r\n        r, g, b = cell_info[0]\r\n        x, y = cell_info[1]\r\n        try:\r\n            sht.range((y + 1, x + 1), (y + 1, x + 1)).color = (r, g, b)\r\n        except ValueError as e:\r\n            # \u5728\u8d85\u8fc7excel\u5141\u8bb8\u7684\u6700\u5927\u5217\u5bbd\u65f6\u51fa\u73b0\u6b64\u9519\u8bef\uff0c\u4e00\u822c\u7531\u4e8eresize_image()\u65b9\u6cd5max_resize_width\u53c2\u6570\u8d85\u8fc7256\u65f6\u5bfc\u81f4\r\n            print(f\"cell index out of range: {e}\")\r\n            continue\r\n    wb.save(\"processed.xlsx\")\r\n\r\n\r\nif __name__ in \"__main__\":\r\n    resized = resize_image(original_pic_path=\"kkk.jpeg\", resized_pic_path=\"kkk.resize.png\")\r\n    print(resized)\r\n    colours = pixel_colour(resized[0], resized[1])\r\n    book = create_excel(resized[1])\r\n    draw_excel(book, colours)<\/pre>\n<p><\/code><\/pre>\n\t\t\t<\/div>\n        <\/div>\n<p>&nbsp;<\/p>\n<p>\u4e0b\u8f7d\uff1a<a href=\"http:\/\/10ccc.cc\/wp-content\/uploads\/2022\/04\/picture2excel.py\" target=\"_blank\"  rel=\"nofollow\" >picture2excel<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u7531\u672c\u4eba\u5728 2022-1-21 11:37 \u53d1\u8868\u4e8e\u543e\u7231\u7834\u89e3 \u4e4b\u524d\u770b\u4e86\u300a82\u5c81\u8001\u4eba\u7528Excel\u753b\u753b\uff0c\u83b7\u5956\u65e0\u6570\u8fd8\u5f00\u753b\u5c55\uff01\u800c\u4f60\u53ea\u4f1a\u505a\u8868 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[123,84,82],"tags":[86,89,87,88],"class_list":["post-3672","post","type-post","status-publish","format-standard","hentry","category-python","category-funny","category-82","tag-86","tag-89","tag-87","tag-88"],"_links":{"self":[{"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/posts\/3672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/10ccc.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3672"}],"version-history":[{"count":4,"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/posts\/3672\/revisions"}],"predecessor-version":[{"id":4517,"href":"https:\/\/10ccc.cc\/index.php?rest_route=\/wp\/v2\/posts\/3672\/revisions\/4517"}],"wp:attachment":[{"href":"https:\/\/10ccc.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/10ccc.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/10ccc.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}