Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python script to open a Google Map location on clipboard?
Automating the process of opening Google Maps locations can save time when you frequently search for places. This Python script reads a location from either command line arguments or the clipboard and automatically opens it in Google Maps using your default browser.
Required Installation
We need to install the pyperclip package to access clipboard content ?
pip install pyperclip
How the Script Works
The script follows these steps ?
Check if a location is provided as command line arguments
If no arguments, read the location from clipboard using
pyperclipUse the
webbrowsermodule to open Google Maps with the locationConstruct the Google Maps URL and launch it in the default browser
Complete Script
# webbrowser module helps open default web browser
import webbrowser
# sys contains command line arguments
import sys
# To access clipboard
import pyperclip
# Check if location provided as command line argument
if len(sys.argv) > 1:
# Join all arguments to form complete address
address = " ".join(sys.argv[1:])
else:
# Get location from clipboard
address = pyperclip.paste()
# Construct Google Maps URL and open in browser
maps_url = "https://www.google.com/maps/place/" + address
webbrowser.open(maps_url)
print(f"Opening Google Maps for: {address}")
Usage Examples
Using Command Line Arguments
python map_script.py New York City python map_script.py "Times Square, NYC"
Using Clipboard
Copy any location to clipboard (Ctrl+C) and run ?
python map_script.py
Enhanced Version with Error Handling
import webbrowser
import sys
import pyperclip
import urllib.parse
def open_google_maps():
try:
# Get location from command line or clipboard
if len(sys.argv) > 1:
address = " ".join(sys.argv[1:])
else:
address = pyperclip.paste().strip()
# Check if address is empty
if not address:
print("Error: No location found in arguments or clipboard")
return
# URL encode the address for safety
encoded_address = urllib.parse.quote_plus(address)
maps_url = f"https://www.google.com/maps/place/{encoded_address}"
# Open in browser
webbrowser.open(maps_url)
print(f"Opening Google Maps for: {address}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
open_google_maps()
Key Features
| Feature | Description |
|---|---|
| Command Line Input | Accept location as script arguments |
| Clipboard Input | Read location from clipboard if no arguments |
| URL Encoding | Safely handle special characters in addresses |
| Error Handling | Handle empty inputs and exceptions gracefully |
Conclusion
This script automates Google Maps location lookup by reading addresses from command line arguments or clipboard. The enhanced version includes proper error handling and URL encoding for reliable operation with any location format.
