print(f"Downloading driver from {DRIVER_URL}...") try: urllib.request.urlretrieve(DRIVER_URL, DRIVER_FILENAME) print("Download complete.") return True except Exception as e: print(f"Download failed: {e}") print("Please download manually from Canon support and place it in the same folder as this script.") return False def extract_driver_if_needed(): """Some Canon .exe files are self-extracting ZIPs. Extract if needed.""" # For simplicity, if it's an exe, we'll run it silently later. # But we check if it's a known self-extractor by magic bytes. with open(DRIVER_FILENAME, "rb") as f: header = f.read(4) if header == b'PK\x03\x04': # ZIP file print("Detected ZIP archive. Extracting...") with zipfile.ZipFile(DRIVER_FILENAME, 'r') as zip_ref: extract_path = tempfile.mkdtemp() zip_ref.extractall(extract_path) # Look for an installer (.exe or .msi) for root, dirs, files in os.walk(extract_path): for file in files: if file.lower().endswith(('.exe', '.msi')): new_path = os.path.join(os.getcwd(), "extracted_driver") shutil.copytree(extract_path, new_path, dirs_exist_ok=True) print(f"Extracted to {new_path}") return new_path return None
""" Canon LBP 3010 Printer Installer (Windows) Automates driver download, silent installation, and printer addition. """ import os import sys import subprocess import platform import urllib.request import zipfile import tempfile import shutil DRIVER_URL = "https://gdlp01.c-wss.com/gds/8/0100005918/01/LBP3010_LBP3018_LBP3050_Driver_V200_W32_en.exe" DRIVER_FILENAME = "LBP3010_Driver.exe" PRINTER_NAME = "Canon LBP3010" PORT_NAME = "USB001" # Most common USB port for this printer installer imprimante canon lbp 3010
def add_printer(): """Add printer using Windows printui command.""" print(f"Adding printer '{PRINTER_NAME}'...") try: # Find driver name from inf # Simpler: use printui.dll with /if (install using inf) # We need the driver's INF file. Without INF path, we rely on already installed driver. # Alternative: use PowerShell to add printer using existing driver. ps_command = f''' $driverName = "Canon LBP3010" $printerName = "{PRINTER_NAME}" $portName = "{PORT_NAME}" print(f"Downloading driver from {DRIVER_URL}
def install_driver(): """Silently install the Canon driver.""" print("Installing driver (this may take a few minutes)...") try: # Silent install: /S for NSIS installers, /quiet for MSI # Common Canon driver installer flags: /S /v/qn result = subprocess.run( [DRIVER_FILENAME, "/S", "/v/qn"], capture_output=True, text=True, timeout=120 ) if result.returncode != 0: print(f"Installation returned code {result.returncode}") print("Stdout:", result.stdout) print("Stderr:", result.stderr) return False print("Driver installed successfully.") return True except subprocess.TimeoutExpired: print("Installation timed out but may still succeed. Proceeding...") return True except Exception as e: print(f"Installation failed: {e}") return False with open(DRIVER_FILENAME, "rb") as f: header = f
# Step 3: Install driver if not install_driver(): print("Driver installation failed. Try installing manually.") sys.exit(1)
def is_admin(): """Check if script runs with admin rights (required for printer installation).""" try: return os.getuid() == 0 # Linux/Mac (not used here but safe) except AttributeError: import ctypes return ctypes.windll.shell32.IsUserAnAdmin() != 0
# Step 2: Extract if needed (optional) extracted_path = extract_driver_if_needed() if extracted_path: print(f"Driver extracted to {extracted_path}")