我的目标是从一个文件夹中的多个excel文件中提取信息,然后将信息保存到另一个文件夹中的合并excel文件中。第二个细节是每个新文件的信息都必须粘贴在前一个文件的下面(分隔4行)。
到目前为止,我的代码尝试执行复制和粘贴操作,但我想不出在循环中添加该额外命令的方法,该命令说“在工作表中跳4个新行并在此处添加新信息”
您的时间和帮助将不胜感激。
import os
import xlrd
#-----------------------Files---------------------------
#-----------------------Sheets---------------------------
folder = raw_input("Please specify folder location: ")
month = raw_input("Please specify the month in the format Jan 2018: ")
#Prepare the spreadsheets to copy from and paste too.
def openFile():
sourceFile = xlrd.open_workbook(file)
sourceFileActiveSheet = sourceFile.sheet_by_name("Time Sheet")
for rowNum in range(7, 36):
for colNum in range(1, 6): # copy everything up to and including column 7
values = sourceFileActiveSheet.cell(row=rowNum, column=colNum).value
name = sourceFileActiveSheet.cell(row = 1, column = 2).value
month = sourceFileActiveSheet.cell(row = 3, column = 2).value
return values, name, month
def consolidatedFile(values1, name1, month1):
os.chdir("folder name")
targetFile = xlrd.open_workbook("Consolidated Timesheet.xlsx")
targetFileActiveSheet = targetFile.sheet_by_name(month)
for rowNum in range(1,36):
targetFileActiveSheet.cell(row=rowNum, column=1, value = name1)
targetFileActiveSheet.cell(row=rowNum, column=2, value = month1)
for rowNum in range(7, 36):
for colNum in range(3, 9):
targetFileActiveSheet.cell(row=rowNum, column=colNum, value=values1)
for root, dirs, files in os.walk(folder):
for filename in files:
if filename.endswith(".xlsx"):
v1, v2, v3 = openFile()
consolidatedFile(v1,v2,v3)