将所有.xls转换为.xlsx

时间:2020-06-22 07:32:25

标签: python pandas

您好,我在将所有.xls文件转换为.xlsx时遇到问题。另一个挑战是每个.xls文件都有多个工作表,而我要转换的文件很多。有人可以帮我个解决办法吗

1 个答案:

答案 0 :(得分:1)

import glob
import pandas as pd
import os
from pandas import ExcelWriter

_list_of_xls_files = glob.glob(r'C:\Users\enter_your_pc_username_here\Documents\*xls')

for _xls_file in _list_of_xls_files:
    df = pd.read_excel(_xls_file,sheet_name = None)
    _list_of_tabs_inside_xls_file = df.keys()

    with ExcelWriter(str(_xls_file).replace('.xls','.xlsx')) as writer:

        for n, _sheet_name in enumerate(list_of_tabs_inside_xls_file):

            df[_sheet_name].to_excel(writer,'sheet%s' % n)

来源:

1 Using Pandas to pd.read_excel() for multiple worksheets of the same workbook