Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def main():
|
2 |
st.title("PDF to Excel Converter")
|
3 |
|
4 |
# File uploader
|
5 |
uploaded_pdf = st.file_uploader("Upload a PDF file", type="pdf")
|
6 |
|
|
|
7 |
if uploaded_pdf:
|
8 |
-
# Extract text
|
9 |
text = extract_text_from_pdf(uploaded_pdf)
|
10 |
st.success("Text extracted from PDF!")
|
11 |
|
12 |
-
#
|
13 |
st.text_area("Extracted Text", text, height=300)
|
14 |
|
15 |
-
#
|
16 |
if st.button("Convert to Excel"):
|
17 |
output_file = "converted_file.xlsx"
|
18 |
convert_text_to_excel(text, output_file)
|
@@ -24,6 +46,7 @@ def main():
|
|
24 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
25 |
)
|
26 |
os.remove(output_file)
|
27 |
-
if __name__ == "__main__":
|
28 |
-
main()
|
29 |
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import pdfplumber
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Function to extract text from a PDF file
|
7 |
+
def extract_text_from_pdf(pdf_file):
|
8 |
+
with pdfplumber.open(pdf_file) as pdf:
|
9 |
+
text = ""
|
10 |
+
for page in pdf.pages:
|
11 |
+
text += page.extract_text()
|
12 |
+
return text
|
13 |
+
|
14 |
+
# Function to convert extracted text to Excel
|
15 |
+
def convert_text_to_excel(text, output_file):
|
16 |
+
rows = text.split("\n")
|
17 |
+
data = [row.split() for row in rows]
|
18 |
+
df = pd.DataFrame(data)
|
19 |
+
df.to_excel(output_file, index=False)
|
20 |
+
|
21 |
+
# Main function to build the Streamlit app
|
22 |
def main():
|
23 |
st.title("PDF to Excel Converter")
|
24 |
|
25 |
# File uploader
|
26 |
uploaded_pdf = st.file_uploader("Upload a PDF file", type="pdf")
|
27 |
|
28 |
+
# Check if a file has been uploaded
|
29 |
if uploaded_pdf:
|
30 |
+
# Extract text from the PDF
|
31 |
text = extract_text_from_pdf(uploaded_pdf)
|
32 |
st.success("Text extracted from PDF!")
|
33 |
|
34 |
+
# Display the extracted text
|
35 |
st.text_area("Extracted Text", text, height=300)
|
36 |
|
37 |
+
# Button to convert and download Excel
|
38 |
if st.button("Convert to Excel"):
|
39 |
output_file = "converted_file.xlsx"
|
40 |
convert_text_to_excel(text, output_file)
|
|
|
46 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
47 |
)
|
48 |
os.remove(output_file)
|
|
|
|
|
49 |
|
50 |
+
# Entry point of the script
|
51 |
+
if __name__ == "__main__":
|
52 |
+
main()
|