

def get_excels(self): """ 读取所有excel文件数据,应用 if 排除干扰网站数据 :return: excel_files """ excel_files=[] excels=os.listdir(self.excel_path) print(len(excels)) for excel in excels: if "zhihu" not in excel: if "baidu" not in excel: if "bilibili" not in excel: excel=os.path.join(self.excel_path,excel) print(excel) excel_files.append(excel) print(len(excel_files)) return excel_file
def get_first_exceldata(self): """ xlrd和lsxWriter打开文档读取数据重新写入到一个新excel文档 读取所有excel表格数据,写入新的excel表格 :return: """ #读取数据 data=[] excel_files=self.get_excels() for excel_file in excel_files: wb=xlrd.open_workbook(excel_file) for sheet in wb.sheets(): for rownum in range(sheet.nrows): data.append(sheet.row_values(rownum)) print(data) print(len(data)) #写入数据 workbook = xlsxwriter.Workbook(self.first_target_xls) worksheet = workbook.add_worksheet() #font = workbook.add_format({"font_size": 14}) for i in range(len(data)): for j in range(len(data[i])): worksheet.write(i, j, data[i][j]) #worksheet.write(i, j, data[i][j], font) workbook.close() #关闭文件
def get_second_exceldata(self): """ pandas 读取所有文档的数据,重新写入到一个新的excel文档 :return: """ data = [] excel_files = self.get_excels() for excel_file in excel_files: df = pd.read_excel(excel_file) # excel转换成DataFrame data.append(df) result = pd.concat(data) result.to_csv(self.second_target_xls,encoding='utf-8-sig',sep=',', index=False) # 保存合并的数据,并把合并后的文件命名
#合并多个excel 20201015 #author/微信:huguo00289 # -*- coding: utf-8 -*- import os import xlrd import xlsxwriter import pandas as pd class Hb(): def __init__(self): self.excel_path=r'E:/Python/mryq' self.first_target_xls = "E:/python/first_mryq.xlsx" self.second_target_xls = "E:/python/second_mryq.csv" def get_excels(self): """ 读取所有excel文件数据,应用 if 排除干扰网站数据 :return: excel_files """ excel_files=[] excels=os.listdir(self.excel_path) print(len(excels)) for excel in excels: if "zhihu" not in excel: if "baidu" not in excel: if "bilibili" not in excel: excel=os.path.join(self.excel_path,excel) print(excel) excel_files.append(excel) print(len(excel_files)) return excel_files def get_first_exceldata(self): """ xlrd和lsxWriter打开文档读取数据重新写入到一个新excel文档 读取所有excel表格数据,写入新的excel表格 :return: """ #读取数据 data=[] excel_files=self.get_excels() for excel_file in excel_files: wb=xlrd.open_workbook(excel_file) for sheet in wb.sheets(): for rownum in range(sheet.nrows): data.append(sheet.row_values(rownum)) print(data) print(len(data)) #写入数据 workbook = xlsxwriter.Workbook(self.first_target_xls) worksheet = workbook.add_worksheet() #font = workbook.add_format({"font_size": 14}) for i in range(len(data)): for j in range(len(data[i])): worksheet.write(i, j, data[i][j]) #worksheet.write(i, j, data[i][j], font) workbook.close() #关闭文件流 def get_second_exceldata(self): """ pandas 读取所有文档的数据,重新写入到一个新的excel文档 :return: """ data = [] excel_files = self.get_excels() for excel_file in excel_files: df = pd.read_excel(excel_file) # excel转换成DataFrame data.append(df) result = pd.concat(data) result.to_csv(self.second_target_xls,encoding='utf-8-sig',sep=',', index=False) # 保存合并的数据,并把合并后的文件命名 if __name__=='__main__': spider=Hb() spider.get_first_exceldata() spider.get_second
效果展示
https://www.cnblogs.com/cjsblog/p/9314166.html
https://blog.csdn.net/shouji111111/article/details/86062328
转载请注明:二爷记 » excel办公小能手,python合并多个EXCEL表的两种方法