Skip to content

Instantly share code, notes, and snippets.

@Wytamma
Last active May 28, 2018 07:19
Show Gist options
  • Save Wytamma/ffc7461d51a2604c36a16b33e57a16f0 to your computer and use it in GitHub Desktop.
Save Wytamma/ffc7461d51a2604c36a16b33e57a16f0 to your computer and use it in GitHub Desktop.
Downloading a Google font for offline use.
# https://stackoverflow.com./questions/15930003/downloading-a-google-font-and-setting-up-an-offline-site-that-uses-it
# google font links like... http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600,300
# contains links to urls of other fonts
# this script will download all the linked fonts
# enabling offline use.
import requests
with open("example.css", "r") as f:
text = f.read()
urls = re.findall(r'(https?://[^\)]+)', text)
for url in urls:
filename = url.split("/")[-1]
r = requests.get(url)
with open("./fonts/" + filename, "wb") as f:
f.write(r.content)
text = text.replace(url, "/fonts/" + filename)
with open("example.css", "w") as f:
f.write(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment