Spaces:
Build error
Build error
Create fileconverter.py
Browse files- fileconverter.py +16 -0
fileconverter.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import docx2txt
|
| 2 |
+
import PyPDF2
|
| 3 |
+
|
| 4 |
+
def convert_docx_file_to_txt_file(docx_path):
|
| 5 |
+
text = docx2txt.process(docx_path)
|
| 6 |
+
return text
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def convert_pdf_file_to_txt_file(pdf_path):
|
| 10 |
+
with open(pdf_path, 'rb') as pdf_file:
|
| 11 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 12 |
+
text = ""
|
| 13 |
+
for page_num in range(len(pdf_reader.pages)):
|
| 14 |
+
page = pdf_reader.pages[page_num]
|
| 15 |
+
text += page.extract_text()
|
| 16 |
+
return text
|