Spaces:
Runtime error
Runtime error
| # Importing the required libraries | |
| import streamlit as st | |
| import pandas as pd | |
| import PyPDF2 | |
| import docx2pdf | |
| from io import BytesIO | |
| # Creating a title and a sidebar | |
| st.title("All files to single pdf") | |
| st.sidebar.header("Upload your files") | |
| # Creating a function to convert docx files to pdf | |
| def docx_to_pdf(docx_file): | |
| pdf_file = BytesIO() | |
| docx2pdf.convert(docx_file.name, pdf_file) | |
| pdf_file.seek(0) | |
| return pdf_file | |
| # Creating a function to merge multiple pdf files | |
| def merge_pdfs(pdf_files): | |
| merger = PyPDF2.PdfMerger() | |
| for pdf_file in pdf_files: | |
| merger.append(PyPDF2.PdfReader(pdf_file)) | |
| merged_pdf = BytesIO() | |
| merger.write(merged_pdf) | |
| merged_pdf.seek(0) | |
| return merged_pdf | |
| # Creating a list to store the uploaded files | |
| uploaded_files = [] | |
| # Allowing the user to upload multiple files of different formats | |
| uploaded_files = st.sidebar.file_uploader("Choose your files", type=["pdf", "docx", "txt", "xlsx", "csv"], accept_multiple_files=True) | |
| # Checking if any files are uploaded | |
| if uploaded_files: | |
| st.write("You have uploaded {} files.".format(len(uploaded_files))) | |
| # Creating a list to store the pdf files | |
| pdf_files = [] | |
| # Looping through the uploaded files and converting them to pdf if needed | |
| for uploaded_file in uploaded_files: | |
| # Getting the file name and extension | |
| file_name = uploaded_file.name | |
| file_ext = file_name.split(".")[-1] | |
| # Checking the file format and converting it to pdf if needed | |
| if file_ext == "pdf": | |
| # No conversion needed, just append to the pdf list | |
| pdf_files.append(uploaded_file) | |
| elif file_ext == "docx": | |
| # Converting docx to pdf using the function defined earlier | |
| pdf_file = docx_to_pdf(uploaded_file) | |
| pdf_files.append(pdf_file) | |
| elif file_ext == "txt": | |
| # Reading the text file and creating a pandas dataframe | |
| df = pd.read_csv(uploaded_file, sep="\n", header=None, names=["Text"]) | |
| # Converting the dataframe to a pdf file using pandas | |
| pdf_file = BytesIO() | |
| df.to_pdf(pdf_file) | |
| pdf_file.seek(0) | |
| pdf_files.append(pdf_file) | |
| elif file_ext == "xlsx" or file_ext == "csv": | |
| # Reading the excel or csv file and creating a pandas dataframe | |
| df = pd.read_excel(uploaded_file) if file_ext == "xlsx" else pd.read_csv(uploaded_file) | |
| # Converting the dataframe to a pdf file using pandas | |
| pdf_file = BytesIO() | |
| df.to_pdf(pdf_file) | |
| pdf_file.seek(0) | |
| pdf_files.append(pdf_file) | |
| # Merging the pdf files using the function defined earlier | |
| merged_pdf = merge_pdfs(pdf_files) | |
| # Displaying the merged pdf file using streamlit | |
| st.write("Here is your merged PDF file:") | |
| st.write("") | |
| #st.pdf(merged_pdf) | |
| # Allowing the user to download the merged pdf file using streamlit | |
| st.write("You can download your merged PDF file here:") | |
| st.download_button(label="Download PDF", data=merged_pdf, mime="application/pdf", file_name="merged.pdf") | |
| else: | |
| st.write("No files uploaded yet. Please upload your files in the sidebar.") | |