-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_windows.py
77 lines (65 loc) · 2.42 KB
/
setup_windows.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
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
import os
import subprocess
try:
import requests
except ImportError:
print("Module 'requests' not found. Installing...")
subprocess.run(["pip","install","requests"])
print("Module 'requests' is Installed.")
import requests
import zipfile
import shutil
def download_poppler(url, destination):
print("Downloading Poppler...")
response = requests.get(url, stream=True)
response.raise_for_status()
with open(destination, "wb") as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
def extract_zip(zip_path, extract_to):
print("Extracting Poppler...")
with zipfile.ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(extract_to)
def add_to_path(directory):
print("Adding Poppler to PATH...")
path = os.environ.get("PATH", "")
if directory not in path:
os.system(f'setx PATH "%PATH%;{directory}"')
print(f"Added {directory} to PATH.")
else:
print(f"{directory} is already in PATH.")
def clean_up(file_path):
print("Cleaning up...")
if os.path.exists(file_path):
os.remove(file_path)
def main():
# Define variables
poppler_url = "https://github.com/oschwartz10612/poppler-windows/releases/download/v24.08.0-0/Release-24.08.0-0.zip"
zip_path = os.path.join(os.getenv("TEMP"), "poppler.zip")
poppler_dir = "C:\\poppler"
bin_dir = os.path.join(poppler_dir, "poppler-24.08.0", "Library", "bin")
# Step 1: Download Poppler
download_poppler(poppler_url, zip_path)
# Step 2: Extract the ZIP file
extract_zip(zip_path, poppler_dir)
# Step 3: Add Poppler to the PATH
add_to_path(bin_dir)
# Step 4: Clean up
clean_up(zip_path)
# Verify Installation
try:
print("Verifying Poppler installation...")
result = subprocess.run(["cmd","/c","cmd.exe","/k", "pdftotext", "--version"], capture_output=True, text=True)
if result.returncode == 0:
print("Poppler installed successfully!")
print(result.stdout)
else:
print("Poppler installation verification failed.")
print(result.stderr)
except FileNotFoundError:
print("Poppler is not accessible. Please check the installation.")
if __name__ == "__main__":
try:
subprocess.run(["pdftotext", "-v"],stderr = subprocess.DEVNULL)
except:
main()