0

Currently I am working on an Excel file and taking its path as input as

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

constructor is as

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

    self.datawb = xlrd.open_workbook(filepath)

but now from another interface I want to use the same class but its not sending me filepath its sending me file through io stream as

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

now this file variable when I am passing in my file as

myObj = ProcessExcelFile(file)

It's giving me error

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

I want to make my init so that it can take path as well as an io stream or if its not possible, I need to have file from io stream as priority

1 Answer 1

1

You would need to modify your class to allow streams as well. Pass the stream to open_workbook via file_contents.

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)
Sign up to request clarification or add additional context in comments.

3 Comments

in this case if I pass a stream it will still fall in FileNot found exception right ? do I need to put that in else part
Yep, I just realized the same copy&paste mistake. I'll update the answer.
it worked ! tanks a lot ! I m sorry but this simple thing I was not able to google it correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.