Accepting an Excel file with io stream
<p>Currently I am working on an Excel file and taking its path as input as</p>

<pre><code>myObj = ProcessExcelFile("C:\SampleExcel.xlsx")
</code></pre>

<p>constructor is as</p>

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

    self.datawb = xlrd.open_workbook(filepath)
</code></pre>

<p>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 </p>

<pre><code>data = req.stream.read(req.content_length)
file = io.BytesIO(data)
</code></pre>

<p>now this file variable when I am passing in my file as</p>

<pre><code>myObj = ProcessExcelFile(file)
</code></pre>

<p>It's giving me error </p>

<pre><code>TypeError: argument should be string, bytes or integer, not _io.BytesIO
</code></pre>

<p>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</p>

<p>You would need to modify your class to allow streams as well. Pass the stream to <code>open_workbook</code> via <code>file_contents</code>.</p>

<pre><code>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)
</code></pre>

python excel io xlrd