Spaces:
No application file
No application file
File size: 6,150 Bytes
8b9aa2c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# MUSS AUFGERÄUMT WERDEN
import json
import os
import subprocess
import PyPDF2
import csv
import fitz # PyMuPDF
def extract_text_from_pdf(pdf_path):
"""
Extracts all text from a PDF file.
:param pdf_path: Path to the PDF file.
:return: Extracted text as a string.
"""
# Open the PDF file
doc = fitz.open(pdf_path)
# Initialize an empty string to hold the text
text = ''
# Iterate through each page in the PDF
for page_num in range(len(doc)):
# Get a page
page = doc.load_page(page_num)
# Extract text from the page and add it to the result
text += page.get_text()
# Close the document
doc.close()
return text
def read_pdfs_from_folder(folder_path):
"""
Reads all PDF files in the specified folder using PdfReader and extracts their text.
Parameters:
- folder_path: The path to the folder containing PDF files.
Returns:
- A dictionary with file names as keys and their extracted text as values.
"""
pdf_texts = {}
for filename in os.listdir(folder_path):
if filename.endswith('.pdf'):
file_path = os.path.join(folder_path, filename)
with open(file_path, 'rb') as pdf_file:
pdf_reader = PyPDF2.PdfReader(pdf_file)
text = ''
for page in pdf_reader.pages:
try:
text += page.extract_text()
except UnicodeDecodeError as e:
print(e)
for c in text:
if c in ["ä", "Ä"]:
text = text[:text.index(c)] + "ae" + text[text.index(c)+1:]
if c in ["ö", "Ö"]:
text = text[:text.index(c)] + "oe" + text[text.index(c)+1:]
if c in ["ü", "Ü"]:
text = text[:text.index(c)] + "ue" + text[text.index(c)+1:]
if c in [",", ";", "\\", '"']:
text = text[:text.index(c)] + "_" + text[text.index(c)+1:]
if c in ["/n", "\n"]:
text = text[:text.index(c)] + "<newline>" + text[text.index(c) + 1:]
pdf_texts[filename] = text
return pdf_texts
def read_csv_lines_as_strings(filename):
"""
Opens a CSV file and returns each line as a string in a list.
Parameters:
- filename: The path to the CSV file.
Returns:
- A list of strings, each representing a line from the CSV file.
"""
lines_as_strings = []
with open(filename, newline='') as csvfile:
try:
reader = csv.reader(csvfile)
for row in reader:
# Convert the row (a list of values) back into a comma-separated string
line_as_string = ','.join(row)
lines_as_strings.append(line_as_string)
except UnicodeDecodeError as e:
print(e)
return lines_as_strings
# Function to load data from JSON files
def load_data(filename):
with open(filename, 'r') as file:
try:
return json.load(file)
except UnicodeDecodeError as err:
print(err)
return {}
def find_and_open_file(filename, start_directory):
"""
Attempts to open a file with the given filename starting from the specified directory.
If the file is not found, searches recursively in all subfolders. Works across macOS, Linux, and Windows.
"""
for root, dirs, files in os.walk(start_directory):
if filename in files:
filepath = os.path.join(root, filename)
print(f"File found: {filepath}")
return filepath
print(f"File {filename} not found.")
return None
def open_file(filepath):
"""
Opens the file with the default application, based on the operating system.
"""
if os.path.exists(filepath):
if os.name == 'posix': # Linux, macOS, etc.
subprocess.call(('open', filepath))
elif os.name == 'nt': # Windows
os.startfile(filepath)
else:
print(f"Cannot open file on this operating system: {filepath}")
else:
print(f"File does not exist: {filepath}")
def list_folders_files_recursive(path, depth=0):
"""
Recursively lists all folders and files within the specified path, including subfolders.
Parameters:
- path: The directory path to list contents from.
- depth: The current depth of recursion (used for indentation in print statements).
Returns:
- None
"""
# Ensure the provided path is a directory
if not os.path.isdir(path):
print(f"The provided path '{path}' is not a valid directory.")
return
indent = ' ' * depth # Indentation based on recursion depth
folders, files = [], []
# List all entries in the directory
for entry in os.listdir(path):
full_path = os.path.join(path, entry)
if os.path.isdir(full_path):
folders.append(entry)
print(f"{indent}Folder: {entry}")
# Recursively list subfolders and files
list_folders_files_recursive(full_path, depth + 1)
elif os.path.isfile(full_path):
files.append(entry)
for f in files:
print(f"{indent}File: {f}")
def list_folders_files(path):
"""
Lists all folders and files within the specified path.
Parameters:
- path: The directory path to list contents from.
Returns:
- A tuple of two lists: (folders, files).
"""
folders = []
files = []
# Ensure the provided path is a directory
if not os.path.isdir(path):
print(f"The provided path '{path}' is not a valid directory.")
return folders, files
# List all entries in the directory
for entry in os.listdir(path):
full_path = os.path.join(path, entry)
if os.path.isdir(full_path):
folders.append(entry)
elif os.path.isfile(full_path):
files.append(entry)
return folders, files
if __name__ == "__main__":
print("here are all functions that read files")
|