将多个工作簿组合成一个使用Python的xlsx工作簿

我有4个.csv文件,我每天都在进行争吵。 我有parsing的csv文件的输出保存为4个单独的.xlsx工作簿。 我的目标是将所有4个工作簿组合到一个单独的xlsx工作簿中,每个工作簿都包含在其自己的选项卡/工作表中。

我已经阅读了许多使用openpyxl,xlwt和xlwriter以及其他人的版本的方法,我只是困惑并寻求专家的正确方法的指导,以使这项工作适合我的具体应用…

我创build了4个工作表/选项卡的工作簿,但是当我尝试使用工作表名称写入选项卡时,它似乎覆盖了我以前所做的所有工作,而我无法弄清楚如何解决这个问题? 任何帮助或指导,非常感谢!

import pandas as pd import openpyxl import csv from openpyxl import Workbook # this creates an xlsx workbook with 4 worksheets wb = Workbook() dest_filename = 'Drop Offenderssssssss.xlsx' ws = wb.active ws.title = "DropCount_Offenders" ws = wb.create_sheet() ws.title = 'Dropstat_Offenders' ws = wb.create_sheet() ws.title = 'DropCountPerSec_Offenders' ws = wb.create_sheet() ws.title = 'numPktDrops_Offenders' wb.save(filename = dest_filename) # there are 2 possible filenames. this takes user input and stores it as a global date variable to call proper filename date = str(raw_input("Enter yyyymmdd: ")) date_var = date # function 1 for tab1 contents: def dropcount_offenders(): global date_var filename1 = 'PROBE_HEALTH_GRAPH_Drop_Count_%s-01.01.00.AM.csv' %(date_var) filename2 = 'PROBE_HEALTH_GRAPH_Drop_Count_%s-01.01.01.AM.csv' %(date_var) # this trys to open the first possible filename try: file_handler = open(filename1) except: print"trying the next one" # if first filename was not found then it locates and opens the 2nd possible filename try: file_handler = open(filename2) except: print"invalid input" # this uses pandas library to read the csv contents into memory data = pd.read_csv(file_handler) # this renames the columns (takes out spaces) data.columns = ["Probe_Name", "Recording_Time", "Drop_Count"] # this defines a filter threshold which clears all rows who's Drop_Count column data = 0 counts = data[data.Drop_Count >= 1].sort_index(by="Probe_Name", ascending=True) # now I want to append/write my filtered data to a specific tab within the xlsx file counts.to_excel("Drop Offenderssssssss.xlsx", "DropCount_Offenders") # function 2 for tab2 contents (overwrites all tabs I previously created and overwrites function 1 as well?): def dropstat_offenders(): global date_var filename1 = 'DropStats_%s-01.01.00.AM.csv' %(date_var) filename2 = 'DropStats_%s-01.01.01.AM.csv' %(date_var) try: file_handler = open(filename1) except: print"trying the next one" try: file_handler = open(filename2) except: print"invalid input" data = pd.read_csv(file_handler) data.columns = ["Probe_Name", "RecordingTime", "RecordingPeriod", "PrimaryDimension", "BladeId", "dropCount"] # this removes the columns i dont need to see del data["RecordingPeriod"] del data["BladeId"] drops = data[data.dropCount >= 1].sort_index(by="Probe_Name", ascending=True) drops.to_excel("Drop Offenderssssssss.xlsx", 'Dropstat_Offenders') # this runs the above 2 functions in sequence dropcount_offenders() dropstat_offenders() 

我想要的是dropcount_offenders()是工作簿中的一个选项卡/工作表,dropstat_offenders()是另一个选项卡/工作表等…?

我认为一个snipit可能工作,但没有成功: 在Python中使用Openpyxl修改现有的Excel文件

另一个snipit我不明白如何在我的应用程序中使用: 如何连接三个Excel文件xlsx使用python?

我没有pandas的经验,但我可能不需要这里。 目前还不清楚您是修改现有的Excel文件,还是只需要使用csv文件创build一个。 如果是前者,则只能使用openpyxl,如果是后者,则可以使用openpyxl或xlsxwriter。 python-excel(xlrd和xlwt)不支持编辑现有或写入Excel 2010文件。

假设你想把csv文件转换成Excel工作表,你的代码看起来就像这个伪代码。

 from csv import DictReader from openpyxl import Workbook wb = Workbook() del wb["Sheet"] for title in ("DropCount_Offenders", "Dropstat_Offenders", "DropCountPerSec_Offenders", "numPktDrops_Offenders"): wb.create_sheet(title) for f in filenames: src = DictReader(f) ws = wb[f] ws.append(["Probe_Name", "Recording_Time", "Drop_Count"]) for row in src: ws.append(row["Probe_Name"], ["Recording_Time"], ["Drop_Count"]) wb.save("Drop Offenders.xlsx") 

您需要查看标准库中的csv模块以获取更多信息。

如果您确实需要Pandas进行sorting等,那么您需要查看编辑现有Excel文件的文档。

NB。 没有必要在你的函数中使用global 。 在Python中,读取对更高范围variables的访问总是可用的。 global用于使局部variables成为全局variables,这是几乎永远不需要的东西。