接受带有iostream的Excel文件

目前我正在处理Excel文件,并将其path作为input

myObj = ProcessExcelFile("C:\SampleExcel.xlsx") 

构造函数是as

 def __init__(self, filepath): ''' Constructor ''' if not os.path.isfile(filepath): raise FileNotFoundError(filepath) self.datawb = xlrd.open_workbook(filepath) 

但现在从另一个接口,我想使用相同的类,但它不发送给我的文件path它通过iostream发送我的文件

 data = req.stream.read(req.content_length) file = io.BytesIO(data) 

现在这个文件variables,当我通过我的文件作为

 myObj = ProcessExcelFile(file) 

这是给我的错误

 TypeError: argument should be string, bytes or integer, not _io.BytesIO 

我想让我的init,以便它可以采取path以及iostream或如果它不可能,我需要从iostream的文件作为优先

你需要修改你的类以允许stream。 通过file_contents将stream传open_workbook

 def __init__(self, filepath, stream=False): ''' Constructor ''' if stream: self.datawb = xlrd.open_workbook(file_contents=filepath.read()) else: if not os.path.isfile(filepath): raise FileNotFoundError(filepath) self.datawb = xlrd.open_workbook(filepath)