Spaces:
Build error
Build error
update to v1.0.1
Browse files
app.py
CHANGED
|
@@ -1,69 +1,34 @@
|
|
| 1 |
-
from
|
| 2 |
import warnings
|
| 3 |
from typing import List
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
class FileNotConvertedWarning(Warning):
|
| 7 |
"""The file was not in one of the specified formats for conversion to PDF,thus it was not converted"""
|
| 8 |
|
| 9 |
def to_pdf(files: List[str]) -> List[str]:
|
| 10 |
-
"""
|
| 11 |
-
Converts various file formats to PDF.
|
| 12 |
-
|
| 13 |
-
Args:
|
| 14 |
-
files: List of file paths to convert. Supports .docx, .pdf, .html, .pptx,
|
| 15 |
-
.csv, .xml, and .md files.
|
| 16 |
-
|
| 17 |
-
Returns:
|
| 18 |
-
List of paths to converted PDF files. For files already in PDF format,
|
| 19 |
-
returns original path.
|
| 20 |
-
|
| 21 |
-
Raises:
|
| 22 |
-
FileNotConvertedWarning: When file format is not supported.
|
| 23 |
-
"""
|
| 24 |
pdfs = []
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
pdfs.append(f)
|
| 32 |
-
elif f.endswith(".html"):
|
| 33 |
-
newfile = f.replace(".html", ".pdf")
|
| 34 |
-
file_to_add = convert_to_pdf(f, newfile, newfile.split(".")[0])
|
| 35 |
-
pdfs.append(file_to_add)
|
| 36 |
-
elif f.endswith(".pptx"):
|
| 37 |
-
newfile = f.replace(".pptx", ".pdf")
|
| 38 |
-
file_to_add = convert_to_pdf(f, newfile, newfile.split(".")[0])
|
| 39 |
-
pdfs.append(file_to_add)
|
| 40 |
-
elif f.endswith(".csv"):
|
| 41 |
-
newfile = f.replace(".csv", ".pdf")
|
| 42 |
-
file_to_add = convert_to_pdf(f, newfile, newfile.split(".")[0])
|
| 43 |
-
pdfs.append(file_to_add)
|
| 44 |
-
elif f.endswith(".xml"):
|
| 45 |
-
newfile = f.replace(".xml", ".pdf")
|
| 46 |
-
file_to_add = convert_to_pdf(f, newfile, newfile.split(".")[0])
|
| 47 |
-
pdfs.append(file_to_add)
|
| 48 |
-
elif f.endswith(".md"):
|
| 49 |
-
newfile = f.replace(".md", ".pdf")
|
| 50 |
-
file_to_add = convert_markdown_to_pdf(f, newfile, newfile.split(".")[0])
|
| 51 |
-
pdfs.append(file_to_add)
|
| 52 |
else:
|
| 53 |
-
|
| 54 |
-
continue
|
| 55 |
return pdfs
|
| 56 |
|
| 57 |
-
def
|
| 58 |
-
files = [file]
|
| 59 |
pdfs = to_pdf(files)
|
| 60 |
-
return pdfs
|
| 61 |
|
| 62 |
iface = gr.Interface(
|
| 63 |
-
fn=
|
| 64 |
-
inputs=gr.File(label="Upload your file"),
|
| 65 |
-
outputs=gr.File(label="Converted PDF"),
|
| 66 |
title="File to PDF Converter",
|
| 67 |
-
description="Upload a file in .docx, .
|
| 68 |
)
|
|
|
|
| 69 |
iface.launch()
|
|
|
|
| 1 |
+
from pdfconversion import Converter
|
| 2 |
import warnings
|
| 3 |
from typing import List
|
| 4 |
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
class FileNotConvertedWarning(Warning):
|
| 8 |
"""The file was not in one of the specified formats for conversion to PDF,thus it was not converted"""
|
| 9 |
|
| 10 |
def to_pdf(files: List[str]) -> List[str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
pdfs = []
|
| 12 |
+
converter = Converter()
|
| 13 |
+
for fl in files:
|
| 14 |
+
try:
|
| 15 |
+
outf = converter.convert(fl, fl.replace(os.path.splitext(fl)[1], ".pdf"))
|
| 16 |
+
except Exception as e:
|
| 17 |
+
warnings.warn(f"File {fl} not converted because of an error during the conversion: {e}", FileNotConvertedWarning)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
else:
|
| 19 |
+
pdfs.append(outf)
|
|
|
|
| 20 |
return pdfs
|
| 21 |
|
| 22 |
+
def convert_files(files: List[str]) -> List[str]:
|
|
|
|
| 23 |
pdfs = to_pdf(files)
|
| 24 |
+
return pdfs
|
| 25 |
|
| 26 |
iface = gr.Interface(
|
| 27 |
+
fn=convert_files,
|
| 28 |
+
inputs=gr.File(label="Upload your file", file_count="multiple"),
|
| 29 |
+
outputs=gr.File(label="Converted PDF", file_count="multiple"),
|
| 30 |
title="File to PDF Converter",
|
| 31 |
+
description="Upload a file in .docx, .xlsx, .html, .pptx, .json, .csv, .xml, .md, .jpg/.jpeg, .png format, and get it converted to PDF."
|
| 32 |
)
|
| 33 |
+
|
| 34 |
iface.launch()
|