{"id":952743,"date":"2024-12-27T01:35:44","date_gmt":"2024-12-26T17:35:44","guid":{"rendered":""},"modified":"2024-12-27T01:35:46","modified_gmt":"2024-12-26T17:35:46","slug":"python%e5%a6%82%e4%bd%95%e7%9b%91%e5%90%acexcel","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/952743.html","title":{"rendered":"python\u5982\u4f55\u76d1\u542cExcel"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090502\/f84e30e0-6c98-4c0a-bb96-47f09a536b64.webp\" alt=\"python\u5982\u4f55\u76d1\u542cExcel\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u76d1\u542cExcel\u6587\u4ef6\u7684\u53d8\u5316\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6bd4\u5982\u4f7f\u7528\u6587\u4ef6\u7cfb\u7edf\u76d1\u63a7\u5e93\u3001\u5b9a\u65f6\u5668\u8f6e\u8be2\u3001VBA\u5b8f\u4e0ePython\u7ed3\u5408\u7b49\u3002\u901a\u8fc7\u4f7f\u7528<code>watchdog<\/code>\u5e93\u6765\u76d1\u63a7\u6587\u4ef6\u53d8\u5316\u662f\u4e00\u79cd\u8f83\u4e3a\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>watchdog<\/code>\u5e93\u76d1\u542cExcel\u6587\u4ef6<\/p>\n<\/p>\n<p><p><code>watchdog<\/code>\u662f\u4e00\u4e2a\u53ef\u4ee5\u76d1\u63a7\u6587\u4ef6\u7cfb\u7edf\u4e8b\u4ef6\u7684Python\u5e93\uff0c\u5b83\u53ef\u4ee5\u76d1\u542c\u6587\u4ef6\u7684\u521b\u5efa\u3001\u4fee\u6539\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u3002\u5728\u76d1\u63a7Excel\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>watchdog<\/code>\u6765\u76d1\u542c\u6587\u4ef6\u7684\u53d8\u5316\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u4f7f\u7528watchdog\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>watchdog<\/code>\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install watchdog<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u76d1\u542cExcel\u6587\u4ef6\u7684\u53d8\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>from watchdog.observers import Observer<\/p>\n<p>from watchdog.events import FileSystemEventHandler<\/p>\n<p>class ExcelEventHandler(FileSystemEventHandler):<\/p>\n<p>    def on_modified(self, event):<\/p>\n<p>        if event.src_path.endswith(&#39;.xlsx&#39;):<\/p>\n<p>            print(f&#39;Excel file {event.src_path} has been modified.&#39;)<\/p>\n<p>event_handler = ExcelEventHandler()<\/p>\n<p>observer = Observer()<\/p>\n<p>observer.schedule(event_handler, path=&#39;path_to_your_excel_directory&#39;, recursive=False)<\/p>\n<p>observer.start()<\/p>\n<p>try:<\/p>\n<p>    while True:<\/p>\n<p>        time.sleep(1)<\/p>\n<p>except KeyboardInterrupt:<\/p>\n<p>    observer.stop()<\/p>\n<p>observer.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>ExcelEventHandler<\/code>\u7c7b\u7ee7\u627f\u81ea<code>FileSystemEventHandler<\/code>\uff0c\u5e76\u91cd\u5199\u4e86<code>on_modified<\/code>\u65b9\u6cd5\uff0c\u5f53\u76d1\u63a7\u7684Excel\u6587\u4ef6\u88ab\u4fee\u6539\u65f6\uff0c\u4f1a\u89e6\u53d1\u8be5\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b9a\u65f6\u5668\u8f6e\u8be2Excel\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5b9a\u65f6\u5668\u8f6e\u8be2\u7684\u65b9\u5f0f\u6765\u68c0\u67e5Excel\u6587\u4ef6\u7684\u53d8\u5316\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u539f\u7406\u662f\u4e0d\u65ad\u8bfb\u53d6Excel\u6587\u4ef6\u7684\u4fee\u6539\u65f6\u95f4\uff0c\u5f53\u68c0\u6d4b\u5230\u4fee\u6539\u65f6\u95f4\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u8ba4\u4e3a\u6587\u4ef6\u5df2\u88ab\u4fee\u6539\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b9e\u73b0\u5b9a\u65f6\u5668\u8f6e\u8be2<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u5b9a\u65f6\u5668\u8f6e\u8be2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import time<\/p>\n<p>def monitor_excel_file(file_path):<\/p>\n<p>    last_modified_time = os.path.getmtime(file_path)<\/p>\n<p>    while True:<\/p>\n<p>        current_modified_time = os.path.getmtime(file_path)<\/p>\n<p>        if current_modified_time != last_modified_time:<\/p>\n<p>            print(f&#39;Excel file {file_path} has been modified.&#39;)<\/p>\n<p>            last_modified_time = current_modified_time<\/p>\n<p>        time.sleep(1)<\/p>\n<p>monitor_excel_file(&#39;path_to_your_excel_file.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7<code>os.path.getmtime<\/code>\u65b9\u6cd5\u83b7\u53d6Excel\u6587\u4ef6\u7684\u4fee\u6539\u65f6\u95f4\uff0c\u5e76\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\u4e0e\u4e0a\u6b21\u7684\u4fee\u6539\u65f6\u95f4\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u7ed3\u5408VBA\u5b8f\u548cPython\u5b9e\u73b0\u76d1\u542c<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528VBA\u5b8f\u6765\u76d1\u63a7Excel\u6587\u4ef6\u7684\u53d8\u5316\uff0c\u5e76\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\u901a\u77e5Python\u811a\u672c\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728Excel\u4e2d\u5b9e\u73b0\u590d\u6742\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528VBA\u5b8f<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Excel\u4e2d\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684VBA\u5b8f\u6765\u68c0\u6d4b\u5de5\u4f5c\u7c3f\u7684\u53d8\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vba\">Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)<\/p>\n<p>    Call Shell(&quot;python path_to_your_python_script.py&quot;, vbNormalFocus)<\/p>\n<p>End Sub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b8f\u4e2d\uff0c\u5f53\u5de5\u4f5c\u7c3f\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u5de5\u4f5c\u8868\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u4f1a\u8c03\u7528Python\u811a\u672c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>Python\u811a\u672c<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9Excel\u6587\u4ef6\u8fdb\u884c\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import openpyxl<\/p>\n<p>def process_excel_file(file_path):<\/p>\n<p>    wb = openpyxl.load_workbook(file_path)<\/p>\n<p>    # \u5728\u8fd9\u91cc\u5904\u7406Excel\u6587\u4ef6<\/p>\n<p>    wb.close()<\/p>\n<p>process_excel_file(&#39;path_to_your_excel_file.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93\u8bfb\u53d6Excel\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u76d1\u542c\u6587\u4ef6\u53d8\u5316\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u8bfb\u53d6\u548c\u5904\u7406Excel\u6587\u4ef6\u3002<code>pandas<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u3001\u5199\u5165\u548c\u5904\u7406Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>pandas.read_excel<\/code>\u51fd\u6570\u8bfb\u53d6Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_excel(&#39;path_to_your_excel_file.xlsx&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5904\u7406Excel\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7<code>pandas<\/code>\uff0c\u53ef\u4ee5\u5bf9Excel\u6570\u636e\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u8fc7\u6ee4\u3001\u6392\u5e8f\u3001\u5206\u7ec4\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8fc7\u6ee4\u6570\u636e<\/p>\n<p>filtered_df = df[df[&#39;Column&#39;] &gt; 10]<\/p>\n<h2><strong>\u6392\u5e8f\u6570\u636e<\/strong><\/h2>\n<p>sorted_df = df.sort_values(by=&#39;Column&#39;)<\/p>\n<h2><strong>\u5206\u7ec4\u6570\u636e<\/strong><\/h2>\n<p>grouped_df = df.groupby(&#39;AnotherColumn&#39;).sum()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5199\u5165Excel\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u5904\u7406\u5b8c\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas.to_excel<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u56de\u5230Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">filtered_df.to_excel(&#39;path_to_output_excel_file.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u5bf9Excel\u6587\u4ef6\u7684\u76d1\u542c\u548c\u5904\u7406\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0Excel\u6587\u4ef6\u7684\u76d1\u542c\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u76d1\u542cExcel\u6587\u4ef6\u7684\u53d8\u5316\uff1f<\/strong><br \/>\u4f7f\u7528Python\u76d1\u542cExcel\u6587\u4ef6\u7684\u53d8\u5316\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u3002\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>watchdog<\/code>\u5e93\uff0c\u8be5\u5e93\u80fd\u591f\u76d1\u63a7\u6587\u4ef6\u7cfb\u7edf\u7684\u53d8\u5316\u3002\u5f53Excel\u6587\u4ef6\u88ab\u4fee\u6539\u3001\u521b\u5efa\u6216\u5220\u9664\u65f6\uff0c\u7a0b\u5e8f\u53ef\u4ee5\u89e6\u53d1\u76f8\u5e94\u7684\u4e8b\u4ef6\u3002\u540c\u65f6\uff0c\u7ed3\u5408<code>openpyxl<\/code>\u6216<code>pandas<\/code>\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u5904\u7406Excel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u4ece\u800c\u5b9e\u73b0\u52a8\u6001\u76d1\u63a7\u548c\u54cd\u5e94\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Python\u76d1\u542cExcel\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u76d1\u542cExcel\u6587\u4ef6\u65f6\uff0c\u91cd\u8981\u7684\u662f\u786e\u4fdd\u6587\u4ef6\u5728\u76d1\u542c\u65f6\u5904\u4e8e\u53ef\u8bfb\u72b6\u6001\u3002\u6587\u4ef6\u88ab\u5176\u4ed6\u7a0b\u5e8f\u6253\u5f00\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bfb\u53d6\u9519\u8bef\u3002\u6b64\u5916\uff0c\u76d1\u542c\u9891\u7387\u548c\u5904\u7406\u903b\u8f91\u4e5f\u9700\u8981\u5408\u7406\u8bbe\u7f6e\uff0c\u4ee5\u907f\u514d\u8fc7\u4e8e\u9891\u7e41\u7684\u68c0\u67e5\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002\u5efa\u8bae\u4f7f\u7528\u4e8b\u4ef6\u9a71\u52a8\u7684\u65b9\u5f0f\u5904\u7406\u53d8\u5316\uff0c\u786e\u4fdd\u53ca\u65f6\u54cd\u5e94\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7Python\u5728Excel\u4e2d\u81ea\u52a8\u66f4\u65b0\u6570\u636e\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u5229\u7528Python\u7684<code>openpyxl<\/code>\u6216<code>pandas<\/code>\u5e93\uff0c\u53ef\u4ee5\u5b9a\u671f\u8bfb\u53d6\u6570\u636e\u6e90\u5e76\u66f4\u65b0\u5230Excel\u4e2d\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408\u76d1\u542c\u673a\u5236\uff0c\u5f53\u6570\u636e\u6e90\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u81ea\u52a8\u89e6\u53d1\u66f4\u65b0\u64cd\u4f5c\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u5de5\u4f5c\u6548\u7387\uff0c\u4e5f\u51cf\u5c11\u4e86\u624b\u52a8\u64cd\u4f5c\u7684\u9519\u8bef\u51e0\u7387\uff0c\u9002\u5408\u9700\u8981\u9891\u7e41\u66f4\u65b0\u7684\u6570\u636e\u5904\u7406\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u76d1\u542cExcel\u6587\u4ef6\u7684\u53d8\u5316\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6bd4\u5982\u4f7f\u7528\u6587\u4ef6\u7cfb\u7edf\u76d1\u63a7\u5e93\u3001\u5b9a\u65f6\u5668\u8f6e\u8be2\u3001VBA\u5b8f\u4e0eP [&hellip;]","protected":false},"author":3,"featured_media":952749,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/952743"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=952743"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/952743\/revisions"}],"predecessor-version":[{"id":952750,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/952743\/revisions\/952750"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/952749"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=952743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=952743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=952743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}