-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfile_utils.py
50 lines (39 loc) · 1.73 KB
/
file_utils.py
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
import os
import zipfile
import subprocess
from pdf2image import convert_from_path
def search_and_extract(zip_filepath, target_files, extract_to):
# Ensure the target directory exists
if not os.path.exists(extract_to):
os.makedirs(extract_to)
extracted_files = []
# Open the zip file in read mode
with zipfile.ZipFile(zip_filepath, "r") as zip_ref:
# Loop over the files in the zip file
for filename in zip_ref.namelist():
# Check the filename part after the last slash
if os.path.basename(filename) in target_files:
# Extract the file
zip_ref.extract(filename, extract_to)
print(f"File {filename} extracted to {extract_to}")
extracted_files.append(extract_to + "/" + os.path.basename(filename))
return extracted_files
def ppt_preview(ppt_file_path, preview_file_path):
# Check the file extension
if not ppt_file_path.endswith((".ppt", ".pptx")):
raise ValueError("File must be a .ppt or .pptx file")
# Generate a temporary pdf path
pdf_file_path = os.path.splitext(ppt_file_path)[0] + ".pdf"
print(pdf_file_path)
# Convert PowerPoint to PDF using unoconv
subprocess.run(["unoconv", "-f", "pdf", "-o", pdf_file_path, ppt_file_path])
# Convert PDF to list of images
images = convert_from_path(pdf_file_path)
preview_file_paths = []
for i, image in enumerate(images):
fname = os.path.splitext(preview_file_path)[0] + f"-{i}.jpg"
image.save(fname, "JPEG")
preview_file_paths.append(fname)
# Save the first image (the first slide of the ppt) to the preview_file_path
# images[0].save(preview_file_path, "JPEG")
return preview_file_paths