Created
June 6, 2025 19:26
-
-
Save bradmartin333/1a8d96d9d7ea62aad4afe5df8fcd395e to your computer and use it in GitHub Desktop.
print zen quote to USB thermal printer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
PRINTER_DEVICE = "/dev/usb/lp0" | |
with open(PRINTER_DEVICE, "wb") as printer: | |
response = requests.get("https://zenquotes.io/api/random") | |
response.raise_for_status() | |
data = response.json() | |
if data and isinstance(data, list) and len(data) > 0: | |
quote_data = data[0] | |
quote = quote_data.get("q") | |
author = quote_data.get("a") | |
if quote and author: | |
formatted_quote = f'"{quote}"\n- {author}\n\n\n\n' | |
printer.write(formatted_quote.encode("utf-8")) | |
printer.write(b"\x0c") | |
print(f"successfully printed to {PRINTER_DEVICE}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment