{"id":1536,"date":"2023-03-20T15:13:45","date_gmt":"2023-03-20T07:13:45","guid":{"rendered":"http:\/\/175.24.184.226\/?p=1536"},"modified":"2023-03-20T15:13:45","modified_gmt":"2023-03-20T07:13:45","slug":"%e5%88%a9%e7%94%a8-python-%e5%b0%86-xls-%e8%bd%ac%e4%b8%ba-xlsx","status":"publish","type":"post","link":"\/?p=1536","title":{"rendered":"\u5229\u7528 Python \u5c06 xls \u8f6c\u4e3a xlsx"},"content":{"rendered":"<p>\u4e3a\u4e86\u8bfb\u53d6 excel \u4e2d\u8868\u683c\u7684\u5168\u90e8\u4fe1\u606f\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a html \u683c\u5f0f\uff0c\u590d\u7528\u4e86\u4ee5\u4e0b\u5b9e\u73b0\uff1a<\/p>\n<p><a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/blog.csdn.net\/weixin_50702169\/article\/details\/125127536\">python\u5c06excel\u8f6chtml\u7684table\u6807\u7b7e\uff08\u542b\u5408\u5e76\u5355\u5143\u683c\uff09<\/a><\/p>\n<p>\u4f46\u5176\u4e2d\u7528\u5230\u7684 openpyxl \u4ec5\u652f\u6301 xlsx \u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5982\u679c\u7ed9\u5b9a xls \u4f1a \u62a5\u9519 Not Zip File.<\/p>\n<p>\u6240\u4ee5\u9700\u8981\u5c06 xls \u6279\u91cf\u8f6c\u6362\u4e3a xlsx<\/p>\n<p>\u5982\u679c\u662f Windows \u73af\u5883\uff0c\u90a3\u4e48\u5b9e\u73b0\u8d77\u6765\u6bd4\u8f83\u7b80\u5355\uff0c\u7f51\u4e0a\u6709\u5927\u91cf\u4f7f\u7528 pywin32 \u7684\u53c2\u8003\u793a\u4f8b\u5982\uff1a<\/p>\n<pre><code>import win32com.client as win32\nfname = &quot;full+path+to+xls_file&quot;\nexcel = win32.gencache.EnsureDispatch(&#039;Excel.Application&#039;)\nwb = excel.Workbooks.Open(fname)\n\nwb.SaveAs(fname+&quot;x&quot;, FileFormat = 51)    #FileFormat = 51 is for .xlsx extension\nwb.Close()                               #FileFormat = 56 is for .xls extension\nexcel.Application.Quit()<\/code><\/pre>\n<p>\u4f46\u5728 linux \u73af\u5883\u4e0b\u5c06\u4e0d\u80fd\u5b89\u88c5 pywin32\uff0c \u6240\u4ee5\u9700\u8981\u7528\u5230\u53e6\u4e00\u4e2a\u5305<\/p>\n<pre><code>pip install pyexcel pyexcel-xls pyexcel-xlsx<\/code><\/pre>\n<p>\u7136\u540e<\/p>\n<pre><code>import pyexcel as p\n\np.save_book_as(file_name=&#039;your-file-in.xls&#039;,\n               dest_file_name=&#039;your-new-file-out.xlsx&#039;)<\/code><\/pre>\n<p>\u672c\u6587\u8f6c\u81ea <a href=\"https:\/\/zhuanlan.zhihu.com\/p\/564399585\">https:\/\/zhuanlan.zhihu.com\/p\/564399585<\/a>\uff0c\u5982\u6709\u4fb5\u6743\uff0c\u8bf7\u8054\u7cfb\u5220\u9664\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4e86\u8bfb\u53d6 excel \u4e2d\u8868\u683c\u7684\u5168\u90e8\u4fe1\u606f\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a html \u683c\u5f0f\uff0c\u590d\u7528\u4e86\u4ee5\u4e0b\u5b9e\u73b0\uff1a python\u5c06excel\u8f6chtml\u7684table\u6807   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[48],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1536"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1536"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1536\/revisions"}],"predecessor-version":[{"id":1537,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1536\/revisions\/1537"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1536"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}