实现了读取文件夹中多个csv文件并将其逐个转换为excel文件,最后将excel文件压缩成.zip压缩包
import zipfile
import pandas as pd
import os
class Csv2Excel2Zip:
"""
读取一个文件夹内的所有csv文件,并将其转化为excel,最后将所有的excel表格压缩成压缩包
"""
def __init__(self):
self.path = r'E:\Files' # csv文件路径自行修改
self.out_name = r'E:\excels.zip' # 压缩包路径或名称自行修改
self.compress(out_name=self.out_name)
def get_file(self): # 创建一个空列表
files = os.listdir(self.path)
files.sort() # 排序
list_path = [] # 空list存放csv文件路径
for file in files:
if not os.path.isdir(self.path + file): # 判断该文件是否是一个文件夹
f_name = str(file)
tr = '\\' # 多增加一个斜杠
filename = self.path + tr + f_name
list_path.append(filename)
return list_path
def print_csv(self): # 读取csv文件并打印
list_path = self.get_file()
for i in range(len(list_path)):
print(list_path[i])
data = pd.read_csv(list_path[i])
print(data.head())
return list_path
def csv_to_xlsx_pd(self): # csv转换为excel文件
list_path = self.print_csv()
filename_excel_s = [] # 创建一个空list,存放excel文件路径
for i in range(len(list_path)):
filename = list_path[i]
csv = pd.read_csv(filename, encoding='utf-8')
filename_excel = filename.strip('.csv') + '.xlsx' # 更改后缀名
filename_excel_s.append(filename_excel)
csv.to_excel(filename_excel, sheet_name='data')
return filename_excel_s
def compress(self, out_name): # 文件压缩
files = self.csv_to_xlsx_pd() # excel文件的路径合集
f = zipfile.ZipFile(out_name, 'w', zipfile.ZIP_DEFLATED)
for file in files:
f.write(file)
f.close()
if __name__ == '__main__':
Csv2Excel2Zip()
有对代码好的改进请留言