Read particular cells values from an Excel file

Hello , I tried to import openpyxl in order to load an excel file and then read a couple of cell values in but that appears not to work in Streamlit. I am using file = st.file_uploader(“Choose an excel file”, type=“xlsx”) What would be the best way to achieve reading in a couple of stand alone cells that act as variables in the code?

I did find some work arounds with pandas but thats not very efficient.

Thanks
Michiel

Hi @M_LUY -

In general, this is more of a Python question than a Streamlit one. Meaning, it’s most likely not the case that Streamlit doesn’t allow this, but rather a different library doesn’t work the way you would expect.

Can you post a code snippet demonstrating what you have tried?

Best,
Randy

Thanks Randy,
For example a snippet to directly read cell values with openpyxl library:

import openpyxl
wb = openpyxl.load_workbook(‘workbook.xlsx’)
sheet = wb[‘data’]

C1 = sheet[‘C1’] # read direct value in cell C1
C1 = sheet.cell(row=1,column=3) # or this has the same effect

print(C1.value)

Hi Randy,
I dont know what went wrong last time, but its working fine indeed now

import openpyxl
import streamlit as st

wb = openpyxl.load_workbook(‘workbook.xlsx’)
sheet = wb[‘data’]

C1 = sheet[‘C1’] # read direct value in cell C1

st.write (C1.value)

1 Like
from win32com.client.gencache import EnsureDispatch
from win32com.client import constants


#file = "path...+file.xlsm"
xl = EnsureDispatch('Excel.Application')
#wb=xl.Workbooks.Open(file)
wb=xl.ActiveWorkbook
ws=xl.ActiveSheet
cell=xl.ActiveCell
satr=cell.Row

print(wb.Name, " active wb name.")

dot=[]

dot.append(int((ws.Cells(satr,2).Value)))
dot.append(ws.Cells(satr,4).Value)
dot.append(ws.Cells(satr,5).Value)

print(dot) 

this might be helpful

1 Like

Thanks I saw this a bit late

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.