python获取excel数据进行判断输出结果到excel,如何使用python xlrd/xlwt从一个excel工作簿提取数据并输出到另一个excel工作簿?...

我正在为xlutils、xlrd和xlwt创建一个名为excel functions的类,最终我可能会创建一个库。如果你有兴趣帮我做一个删除工作表的功能。

您可能希望转向openpyxl和/或pyexcel,因为它们更容易实现,并且有相应的功能。

如果要从一个excel工作簿提取数据并输出到另一个excel工作簿,则需要使用createCopy(原始工作簿、其他工作簿、原始文件名、新文件名)import xlwt

import xlrd

import xlutils.copy

import xlutils class excelFunctions():

def getSheetNumber(self, fileName, sheetName):

# opens existing workbook

workbook = xlrd.open_workbook(fileName, on_demand=True)

#turns sheet name into sheet number

for index, sheet in enumerate(workbook.sheet_names()):

if sheet == sheetName:

return index

def createSheet(self, fileName, sheetName):

# open existing workbook

rb = xlrd.open_workbook(fileName, formatting_info=True, on_demand=True)

# make a copy of it

wb = xl_copy(rb)

# creates a variable called sheets which stores all the sheet names

sheets = rb.sheet_names()

# creates a string which is equal to the sheetName user input

str1 = sheetName

# checks to see if the given sheetName is a current sheet

if (str1 not in sheets):

# add sheet to workbook with existing sheets

Sheet = wb.add_sheet(sheetName)

# save the sheet with the same file name as before

wb.save(fileName)

else:

# this declares the sheet variable to be equal to the sheet name the user gives

sheet = wb.get_sheet(self.getSheetNumber(fileName, sheetName))

# save the sheet with the same file name as before

wb.save(fileName)

def createCopy(self, fileName, fileName2, sheetName, sheetName2):

# open existing workbook

rb = xlrd.open_workbook(fileName, formatting_info=True)

# defines sheet as the name of the sheet given

sheet = rb.sheet_by_name(sheetName)

# makes a copy of the original sheet

wb = xl_copy(rb)

# creates an int called column_count which is equal to the sheets maximum columns

column_count = sheet.ncols - 1

# creates a blank array called stuff

Stuff = []

# this loops through adding columns from the given sheet name

for i in range (0, column_count):

Stuff.append([sheet.cell_value(row, i) for row in range(sheet.nrows)])

# create a sheet if there is not already a sheet

self.createSheet(fileName, sheetName2)

# defines sheet as the new sheet

sheet = wb.get_sheet(self.getSheetNumber(fileName, sheetName2))

# this writes to the sheet

for colidx, col in enumerate(Stuff):

for rowidx, row in enumerate(col):

sheet.write(rowidx, colidx, row)

# this saves the file

wb.save(fileName2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值