[python][pandas] excel 批量转 csv 格式

import pandas as pd

import os

# 读取一个目录里面的所有文件:

def read_path(path):
    dirs = os.listdir(path)
    return dirs

def getpath(dir):
    # pwd = os.getcwd()
    # father_path=os.path.abspath(os.path.dirname(pwd)+os.path.sep+".")

    file_path = 'C:/meng_F/python_study'
    full_path=file_path+'/'+dir+'/'

    return full_path

#数据源Excel如果有多个sheet,那么则需要一一遍历输出

def xlsdata_to_csv(xlsx_data,tag_path):
    if not xlsx_data.empty:
        xlsx_data.to_csv('test.csv', encoding='utf_8_sig')

def get_excel_file(dir_path):
    file_list = []
    for root, dirs, files in os.walk(dir_path):
        for file in files:
            if file.endswith("xlsx") or file.endswith("xls"):
                file_list.append(os.path.join(root, file))
    return file_list

def main():
    source_path = getpath('excel_to_csv')
    tag_path = getpath('excel_to_csv')
    excel_files_list = get_excel_file(source_path)

    for file in  excel_files_list:
        print(file)
        xlsx_data = pd.read_excel(file, sheet_name='info')
        print(xlsx_data)
        xlsdata_to_csv(xlsx_data,tag_path)

if __name__ == '__main__':
    main()

你可能感兴趣的:(Python,python,pandas,excel)