{"id":212629,"date":"2021-04-13T09:17:13","date_gmt":"2021-04-13T01:17:13","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=212629"},"modified":"2021-03-29T08:18:15","modified_gmt":"2021-03-29T00:18:15","slug":"python-excel-sheet","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-excel-sheet.html","title":{"rendered":"\u7528Python\u6279\u91cf\u5b9e\u73b0\u591aExcel\u591aSheet\u5408\u5e76\u76844\u79cd\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5927\u5bb6\u597d\uff0c\u6211\u662f\u5d14\u8273\u98de\u3002\u524d\u4e24\u5929\u7ed9\u5927\u5bb6\u5206\u4eab\u4e86Python\u81ea\u52a8\u5316\u6587\u7ae0\uff1a\u624b\u628a\u624b\u6559\u4f60\u5229\u7528Python\u8f7b\u677e\u62c6\u5206Excel\u4e3a\u591a\u4e2aCSV\u6587\u4ef6\uff0c\u800c\u540e\u5728Python\u8fdb\u9636\u4ea4\u6d41\u7fa4\u91cc\u8fb9\u6709\u8bfb\u8005\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u4ed6\u6709\u5f88\u591a\u4e2aExcel\u8868\u683c\uff0c\u4ed6\u9700\u8981\u5bf9\u8fd9\u4e9bExcel\u6587\u4ef6\u8fdb\u884c\u5408\u5e76\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4e00\u3001\u524d\u8a00<\/strong><\/div>\n

\u5927\u5bb6\u597d\uff0c\u6211\u662f\u5d14\u8273\u98de\u3002\u524d\u4e24\u5929\u7ed9\u5927\u5bb6\u5206\u4eab\u4e86Python\u81ea\u52a8\u5316\u6587\u7ae0\uff1a\u624b\u628a\u624b\u6559\u4f60\u5229\u7528Python\u8f7b\u677e\u62c6\u5206Excel\u4e3a\u591a\u4e2aCSV\u6587\u4ef6\uff0c\u800c\u540e\u5728Python\u8fdb\u9636\u4ea4\u6d41\u7fa4\u91cc\u8fb9\u6709\u8bfb\u8005\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u4ed6\u6709\u5f88\u591a\u4e2aExcel\u8868\u683c\uff0c\u4ed6\u9700\u8981\u5bf9\u8fd9\u4e9bExcel\u6587\u4ef6\u8fdb\u884c\u5408\u5e76\u3002<\/p>\n

\u8bda\u7136\uff0c\u4e00\u4e2a\u4e00\u4e2a\u6253\u5f00\u590d\u5236\u7c98\u8d34\u56fa\u7136\u53ef\u884c\uff0c\u4f46\u662f\u8be5\u65b9\u6cd5\u8d39\u65f6\u8d39\u529b\uff0c\u8fd8\u5bb9\u6613\u51fa\u9519\uff0c\u51e0\u4e2a\u6587\u4ef6\u8fd8\u53ef\u4ee5\u624b\u52a8\u5904\u7406\uff0c\u8981\u662f\u51e0\u5341\u4e2a\u751a\u81f3\u4e0a\u767e\u4e2a\uff0c\u4f60\u5c31\u6293\u778e\u4e86\uff0c\u4e0d\u8fc7\u8fd9\u95ee\u9898\u5bf9Python\u6765\u8bf4\uff0cso easy\uff0c\u4e00\u8d77\u6765\u770b\u770b\u5427!<\/p>\n

\u4e8c\u3001\u9879\u76ee\u76ee\u6807<\/strong><\/div>\n

\u7528Python\u5b9e\u73b0\u591aExcel\u3001\u591aSheet\u7684\u5408\u5e76\u5904\u7406\u3002<\/p>\n

\u4e09\u3001\u9879\u76ee\u51c6\u5907<\/strong><\/div>\n

\u8f6f\u4ef6\uff1aPyCharm<\/p>\n

\u9700\u8981\u7684\u5e93\uff1apandas\uff0c xlrd\uff0cos<\/p>\n

\u56db\u3001\u9879\u76ee\u5206\u6790<\/strong><\/div>\n
1)\u5982\u4f55\u9009\u62e9\u8981\u5408\u5e76\u7684Excel\u6587\u4ef6?<\/strong><\/span><\/div>\n

\u5229\u7528os\uff0c\u83b7\u53d6\u6240\u6709\u8981\u5408\u5e76\u7684Excel\u6587\u4ef6\u3002<\/p>\n

2)\u5982\u4f55\u9009\u62e9\u8981\u5408\u5e76\u7684Sheet?<\/strong><\/span><\/div>\n

\u5229\u7528xlrd\u5e93\u8fdb\u884cExcel\u8bfb\u53d6\uff0c\u83b7\u53d6\u8981\u5408\u5e76\u7684Sheet\u540d\u3002<\/p>\n

3)\u5982\u4f55\u5408\u5e76?<\/strong><\/span><\/div>\n

\u5229\u7528pandas\u5e93\uff0c\u5bf9\u6240\u6709Sheet\u540d\u9010\u4e00\u5faa\u73af\u6253\u5f00\uff0c\u901a\u8fc7concat()\u8fdb\u884c\u6570\u636e\u8ffd\u52a0\u5408\u5e76\u5373\u53ef\u3002<\/p>\n

4)\u5982\u4f55\u4fdd\u5b58\u6587\u4ef6?<\/strong><\/span><\/div>\n

\u5229\u7528to_excel\u4fdd\u5b58\u6570\u636e\uff0c\u5f97\u5230\u6700\u7ec8\u5408\u5e76\u540e\u7684\u76ee\u6807\u6587\u4ef6\u3002<\/p>\n

\u4e94\u3001\u9879\u76ee\u5b9e\u73b0<\/strong><\/div>\n
1\u3001\u7b2c\u4e00\u6b65\u5bfc\u5165\u9700\u8981\u7684\u5e93<\/strong><\/span><\/div>\n
\r\nimport pandas as pd \r\nimport xlrd \r\nimport os \r\n<\/pre>\n
2\u3001\u7b2c\u4e8c\u6b65\u9009\u62e9\u8981\u5408\u5e76\u7684Excel\u6587\u4ef6<\/strong><\/span><\/div>\n

#\u8981\u5408\u5e76\u6587\u4ef6\u8def\u5f84
\n path=\"D:\/b\/\"
\n #\u83b7\u53d6\u6587\u4ef6\u5939\u4e0b\u6240\u6709EXCEL\u540d
\n xlsx_names = [x for x in os.listdir(path) if x.endswith(\".xlsx\")] <\/p>\n

3\u3001\u7b2c\u4e09\u6b65\u9009\u62e9\u8981\u5408\u5e76\u7684Sheet<\/strong><\/span><\/div>\n
\r\n# \u83b7\u53d6\u7b2c\u4e00\u4e2aEXCEL\u540d \r\n xlsx_names1 = xlsx_names[0] \r\n aa = path + xlsx_names1 \r\n #\u6253\u5f00\u7b2c\u4e00\u4e2aEXCEL \r\n first_file_fh=xlrd.open_workbook(aa) \r\n # \u83b7\u53d6SHEET\u540d \r\n first_file_sheet=first_file_fh.sheets() \r\n<\/pre>\n
4\u3001\u7b2c\u56db\u6b65\u5bf9Sheet\u5185\u5bb9\u8fdb\u884c\u5faa\u73af\u5408\u5e76<\/strong><\/span><\/div>\n
\r\n#\u6309SHEET\u540d\u5faa\u73af \r\n for sheet_name in sheet_names: \r\n     df = None \r\n     # \u6309EXCEL\u540d\u5faa\u73af \r\n     for xlsx_name in xlsx_names: \r\n         sheet_na = pd.ExcelFile(path + xlsx_name).sheet_names \r\n         if sheet_name in sheet_na: \r\n             #print(sheet_name) \r\n             _df = pd.read_excel(path + xlsx_name, sheet_name=sheet_name,header=None) \r\n             if df is None: \r\n                 df = _df \r\n             else: \r\n                 df = pd.concat([df, _df], ignore_index=True) \r\n         else:continue \r\n<\/pre>\n
5\u3001\u7b2c\u4e94\u6b65\u4fdd\u5b58\u5408\u5e76\u540e\u7684\u6587\u4ef6<\/strong><\/span><\/div>\n
\r\n  df.to_excel(excel_writer=writer, sheet_name=sheet_name, encoding=\"utf-8\", index=False) \r\n    print(sheet_name + \"  \u4fdd\u5b58\u6210\u529f\uff01\u5171%d\u4e2a\uff0c\u7b2c%d\u4e2a\u3002\" % (len(sheet_names),num)) \r\n    num += 1 \r\nwriter.save() \r\nwriter.close() \r\n<\/pre>\n
\u516d\u3001\u6548\u679c\u5c55\u793a<\/strong><\/div>\n

1\u3001\u5904\u7406\u524dExcel\u6570\u636e\uff1a
\n\"\"<\/p>\n

2\u3001\u8fd0\u884c\u8fdb\u5ea6\u63d0\u793a\uff1a
\n\"\"<\/p>\n

3\u3001\u5408\u5e76\u540e\u7684\u7ed3\u679c\uff1a
\n\"\"<\/p>\n

\u4e03\u3001\u603b\u7ed3<\/strong><\/div>\n

\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5229\u7528Python\u5bf9\u591a\u4e2aExcel\u6587\u4ef6\u3001\u591aSheet\u8fdb\u884c\u5408\u5e76\u5904\u7406\uff0c\u51cf\u5c11\u4e86\u5f88\u591a\u590d\u5236\u7c98\u8d34\u7684\u9ebb\u70e6\uff0c\u7701\u65f6\u7701\u529b\uff0c\u8fd8\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u4ee3\u7801\u4e0d\u591a\uff0c\u5faa\u73af\u8ffd\u52a0\u6709\u70b9\u7ed5\uff0c\u60f3\u60f3\u4e5f\u5c31\u660e\u767d\u4e86\uff0c\u4e0d\u61c2\u7684\u968f\u65f6\u7559\u8a00\u63d0\u95ee\uff0c\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\u3002<\/p>\n

\u6709\u60f3\u6cd5\u7684\u5c0f\u4f19\u4f34\u8fd8\u53ef\u4ee5\u5c06\u6587\u7ae0\u4e2d\u7684\u4ee3\u7801\u8fdb\u884c\u6253\u5305\uff0c\u505a\u6210\u4e00\u4e2aexe\u53ef\u6267\u884c\u7684\u5c0f\u8f6f\u4ef6\uff0c\u5305\u88c5\u597d\u53d1\u7ed9\u522b\u4eba\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u8d5a\u70b9\u5c0f\u8d39\u5662\uff0c\u5173\u4e8e\u6253\u5305\u7684\u6559\u7a0b\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\uff0c\u6b22\u8fce\u524d\u5f80\uff1a\u4e09\u4e2a\u4f60\u5fc5\u987b\u8981\u8bb0\u4f4f\u7684Pyinstaller\u6253\u5305\u547d\u4ee4\u2014\u2014\u5229\u7528Python\u5b9e\u73b0\u6253\u5305exe\u3002<\/p>\n

\u516b\u3001\u5f69\u86cb<\/strong><\/div>\n

\u4e0b\u9762\u4e24\u4efd\u4ee3\u7801\u662f\u7fa4\u5185\u5c0f\u4f19\u4f34\u63d0\u4f9b\u4e86\uff0c\u5c0f\u7f16\u4e5f\u81ea\u884c\u6d4b\u8bd5\u4e86\u4e0b\uff0c\u4eb2\u6d4b\u6709\u6548\uff0c\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u5c1d\u8bd5\u5662!<\/p>\n

\u6765\u81ea\u7fa4\u53cbJayson\u7684\u4ee3\u7801\uff1a<\/p>\n

\r\n# -*- coding: utf-8 -*- \r\n# @Author: hebe \r\n# @Date:   2020-04-18 18:31:03 \r\n# @Last Modified by:   hebe \r\n# @Last Modified time: 2020-04-18 19:40:48 \r\nimport os  \r\nimport glob \r\nimport openpyxl \r\n \r\ndef merge_xlsx_files(xlsx_files): \r\n    wb = openpyxl.load_workbook(xlsx_files[0]) \r\n    ws = wb.active \r\n    ws.title = \"merged result\" \r\n \r\n    for  filename in xlsx_files[1:]: \r\n        workbook = openpyxl.load_workbook(filename) \r\n        sheet = workbook.active \r\n        for row in sheet.iter_rows(min_row=1): \r\n            values = [cell.value for cell in row] \r\n            ws.append(values) \r\n    return wb \r\n \r\n#path is very important here , must true. \r\ndef get_all_xlsx_files(path): \r\n    xlsx_files = glob.glob(os.path.join(r'C:\\\\Users\\\\pdcfi\\\\Desktop\\\\', '*.xlsx')) \r\n    sorted(xlsx_files, key=str.lower) \r\n    return xlsx_files \r\n \r\ndef main(): \r\n    xlsx_files = get_all_xlsx_files(os.path.expanduser('~lmx')) \r\n    wb = merge_xlsx_files(xlsx_files) \r\n    wb.save('merged_form.xlsx') \r\n \r\nif __name__ == '__main__': \r\n    main() \r\n     \r\nprint(\"all excel append OK!\") \r\n<\/pre>\n

\u6765\u81ea\u597d\u53cb\u5218\u65e9\u8d77\u7684\u4ee3\u7801\uff1a<\/p>\n

\r\n# -*- coding: utf-8 -*- \r\nfrom openpyxl import load_workbook, Workbook \r\nimport glob \r\n \r\npath = \"C:\\\\Users\\\\pdcfi\\\\Desktop\\\\excel\\\\\" \r\nnew_workbook = Workbook() \r\nnew_sheet = new_workbook.active \r\n \r\n# \u7528flag\u53d8\u91cf\u660e\u786e\u65b0\u8868\u662f\u5426\u5df2\u7ecf\u6dfb\u52a0\u4e86\u8868\u5934\uff0c\u53ea\u8981\u6dfb\u52a0\u8fc7\u4e00\u6b21\u5c31\u65e0\u987b\u91cd\u590d\u518d\u6dfb\u52a0 \r\nflag = 0 \r\n \r\nfor file in glob.glob(path + '\/*.xlsx'): \r\n    workbook = load_workbook(file) \r\n    sheet = workbook.active \r\n \r\n    coloum_A = sheet['A'] \r\n    row_lst = [] \r\n    for cell in coloum_A: \r\n        if cell: \r\n            print(cell.row) \r\n            row_lst.append(cell.row) \r\n \r\n    if not flag: \r\n        header = sheet[1] \r\n        header_lst = [] \r\n        for cell in header: \r\n            header_lst.append(cell.value) \r\n        new_sheet.append(header_lst) \r\n        flag = 1 \r\n \r\n    for row in row_lst: \r\n        data_lst = [] \r\n        for cell in sheet[row]: \r\n            data_lst.append(cell.value) \r\n        new_sheet.append(data_lst) \r\n \r\nnew_workbook.save(path + '\/' + '\u7b26\u5408\u7b5b\u9009\u6761\u4ef6\u7684\u65b0\u8868.xlsx') \r\n\u6765\u81ea\u7fa4\u53cbEngineer\u7684\u4ee3\u7801\uff1a\r\n\r\nimport tkinter as tk \r\nfrom tkinter import filedialog \r\nimport os \r\nimport pandas as pd \r\nimport glob \r\n \r\nroot = tk.Tk() \r\nroot.withdraw() \r\n \r\n# \u9009\u62e9\u6587\u4ef6\u5939\u4f4d\u7f6e \r\nfilelocation = os.path.normpath(filedialog.askdirectory(initialdir=os.getcwd())) \r\nlst = [] \r\n \r\n# \u8bfb\u53d6\u6587\u4ef6\u5939\u4e0b\u6240\u6709\u6587\u4ef6\uff08xls\u548cxlsx\u90fd\u8bfb\u53d6\uff09 \r\nfor i in glob.glob(filelocation + \"\\\\\\\\\" + \"*.*\"): \r\n    if os.path.splitext(i)[1] in [\".xls\", \".xlsx\"]: \r\n        lst.append(pd.read_excel(i)) \r\n \r\n# \u4fdd\u5b58\u5408\u5e76\u540e\u7684excel\u6587\u4ef6 \r\nwriter = pd.ExcelWriter(filedialog.asksaveasfilename(title=\"\u4fdd\u5b58\", initialdir=filelocation, defaultextension=\"xlsx\", \r\n                                                     filetypes=[(\"Excel \u5de5\u4f5c\u7c3f\", \"*.xlsx\"), \r\n                                                                (\"Excel 97-2003 \u5de5\u4f5c\u7c3f\", \"*.xls\")])) \r\npd.concat(lst).to_excel(writer, 'all', index=False) \r\nwriter.save() \r\n \r\nprint('\\n%d\u4e2a\u6587\u4ef6\u5df2\u7ecf\u5408\u5e76\u6210\u529f\uff01' % len(lst)) \r\n<\/pre>\n

\u5f53\u7136\u4e86\uff0c\u5b9e\u73b0\u672c\u6587\u529f\u80fd\u4e0d\u4ec5\u4ec5\u9650\u4e8e\u4e0a\u9762\u63d0\u53ca\u76843\u79cd\u65b9\u5f0f\uff0c\u4f7f\u7528pandas\u4e5f\u662f\u53ef\u4ee5\u505a\u5230\u7684\uff0c\u5982\u679c\u4f60\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\uff0c\u6b22\u8fce\u4ea4\u4e2a\u670b\u53cb\u4e00\u8d77\u5b66\u4e60\u4ea4\u6d41!<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5927\u5bb6\u597d\uff0c\u6211\u662f\u5d14\u8273\u98de\u3002\u524d\u4e24\u5929\u7ed9\u5927\u5bb6\u5206\u4eab\u4e86Python\u81ea\u52a8\u5316\u6587\u7ae0\uff1a\u624b\u628a\u624b\u6559\u4f60\u5229\u7528Python\u8f7b\u677e\u62c6\u5206Excel\u4e3a\u591a […]<\/p>\n","protected":false},"author":1898,"featured_media":212637,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[153],"class_list":["post-212629","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/212629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=212629"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/212629\/revisions"}],"predecessor-version":[{"id":215081,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/212629\/revisions\/215081"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/212637"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=212629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=212629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=212629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}