JamanKH commited on
Commit
cfed571
·
1 Parent(s): c7b1452

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importing the required libraries
2
+ import streamlit as st
3
+ import pandas as pd
4
+ import PyPDF2
5
+ import docx2pdf
6
+ from io import BytesIO
7
+
8
+ # Creating a title and a sidebar
9
+ st.title("All files to single pdf")
10
+ st.sidebar.header("Upload your files")
11
+
12
+ # Creating a function to convert docx files to pdf
13
+ def docx_to_pdf(docx_file):
14
+ pdf_file = BytesIO()
15
+ docx2pdf.convert(docx_file.name, pdf_file)
16
+ pdf_file.seek(0)
17
+ return pdf_file
18
+
19
+ # Creating a function to merge multiple pdf files
20
+ def merge_pdfs(pdf_files):
21
+ merger = PyPDF2.PdfMerger()
22
+ for pdf_file in pdf_files:
23
+ merger.append(PyPDF2.PdfReader(pdf_file))
24
+ merged_pdf = BytesIO()
25
+ merger.write(merged_pdf)
26
+ merged_pdf.seek(0)
27
+ return merged_pdf
28
+
29
+ # Creating a list to store the uploaded files
30
+ uploaded_files = []
31
+
32
+ # Allowing the user to upload multiple files of different formats
33
+ uploaded_files = st.sidebar.file_uploader("Choose your files", type=["pdf", "docx", "txt", "xlsx", "csv"], accept_multiple_files=True)
34
+
35
+ # Checking if any files are uploaded
36
+ if uploaded_files:
37
+ st.write("You have uploaded {} files.".format(len(uploaded_files)))
38
+
39
+ # Creating a list to store the pdf files
40
+ pdf_files = []
41
+
42
+ # Looping through the uploaded files and converting them to pdf if needed
43
+ for uploaded_file in uploaded_files:
44
+ # Getting the file name and extension
45
+ file_name = uploaded_file.name
46
+ file_ext = file_name.split(".")[-1]
47
+
48
+ # Checking the file format and converting it to pdf if needed
49
+ if file_ext == "pdf":
50
+ # No conversion needed, just append to the pdf list
51
+ pdf_files.append(uploaded_file)
52
+ elif file_ext == "docx":
53
+ # Converting docx to pdf using the function defined earlier
54
+ pdf_file = docx_to_pdf(uploaded_file)
55
+ pdf_files.append(pdf_file)
56
+ elif file_ext == "txt":
57
+ # Reading the text file and creating a pandas dataframe
58
+ df = pd.read_csv(uploaded_file, sep="\n", header=None, names=["Text"])
59
+ # Converting the dataframe to a pdf file using pandas
60
+ pdf_file = BytesIO()
61
+ df.to_pdf(pdf_file)
62
+ pdf_file.seek(0)
63
+ pdf_files.append(pdf_file)
64
+ elif file_ext == "xlsx" or file_ext == "csv":
65
+ # Reading the excel or csv file and creating a pandas dataframe
66
+ df = pd.read_excel(uploaded_file) if file_ext == "xlsx" else pd.read_csv(uploaded_file)
67
+ # Converting the dataframe to a pdf file using pandas
68
+ pdf_file = BytesIO()
69
+ df.to_pdf(pdf_file)
70
+ pdf_file.seek(0)
71
+ pdf_files.append(pdf_file)
72
+
73
+ # Merging the pdf files using the function defined earlier
74
+ merged_pdf = merge_pdfs(pdf_files)
75
+
76
+ # Displaying the merged pdf file using streamlit
77
+ st.write("Here is your merged PDF file:")
78
+ st.write("")
79
+ #st.pdf(merged_pdf)
80
+
81
+ # Allowing the user to download the merged pdf file using streamlit
82
+ st.write("You can download your merged PDF file here:")
83
+ st.download_button(label="Download PDF", data=merged_pdf, mime="application/pdf", file_name="merged.pdf")
84
+ else:
85
+ st.write("No files uploaded yet. Please upload your files in the sidebar.")