I'm creating an application using Streamlit where I need to extract a table from PDF file.
simplifyng the code, it looks like:
def extract_table(file,page,table_areas):
table = camelot.read_pdf(file, pages=page,
table_areas=table_areas,
flavor='stream')
return table
if st.session_state.button:
with st.form("form to upload file"):
uploaded_file = st.file_uploader("Upload file", type="pdf", key="uploaded_file")
submitted = st.form_submit_button("Confirm and upload file")
if submitted:
with NamedTemporaryFile(dir='.', suffix='.pdf') as f:
f.write(st.session_state['uploaded_file'].getbuffer())
df = extract_table(f.name, page_number, ['107', '420.1', '286', '262.1'])
But when I try to run the code I get the following error:
PermissionError: [Errno 13] Permission denied: 'C:\ect_streamlit\tmpbv44t05n.pdf'
How could I solve this issue?
Sorry about possible mistakes in the question, it is my first time asking on StackOverFlow.
My enviroment:
OS: Windows 10 x64 Editor: VSCode
How I'm running file: streamlit run efm.py --server.port 8080
Error in detail:
Traceback (most recent call last):
File "C:\ect_streamlit\venv\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 552, in _run_script
exec(code, module.__dict__)
File "C:\ect_streamlit\efm.py", line 73, in <module>
df_raw = extract_table(f.name, efm_page_number, ['107', '420.1', '286', '262.1']) # area x1,y1,x2,y2
File "C:\ect_streamlit\functions.py", line 12, in extract_table
table = camelot.read_pdf(file, pages=page,
File "C:\ect_streamlit\venv\lib\site-packages\camelot\io.py", line 113, in read_pdf
tables = p.parse(
File "C:\ect_streamlit\venv\lib\site-packages\camelot\handlers.py", line 169, in parse
self._save_page(self.filepath, p, tempdir)
File "C:\ect_streamlit\venv\lib\site-packages\camelot\handlers.py", line 107, in _save_page
with open(filepath, "rb") as fileobj:
PermissionError: [Errno 13] Permission denied: 'C:\\ect_streamlit\\tmpbv44t05n.pdf'
Solution:
following this answer I solved this issue.
Basically added "delete=False" as one of the arguments and removed the file created with "os.remove"
The code, now working:
def extract_table(file,page,table_areas):
table = camelot.read_pdf(file, pages=page,
table_areas=table_areas, #x1,y1,x2,y2
flavor='stream')
return table[0].df
if st.session_state.button:
with st.form("form to upload file"):
uploaded_file = st.file_uploader("Upload file", type="pdf", key="uploaded_file")
submitted = st.form_submit_button("Confirm and upload file")
if submitted:
with NamedTemporaryFile(dir='.', suffix='.pdf',delete=False) as tmp:
tmp.write(st.session_state['uploaded_file'].getbuffer())
df_raw = extract_table(tmp.name, efm_page_number, ['107,420.1,286,262.1']) # area x1,y1,x2,y2
st.table(df_raw)
os.remove(tmp.name)