FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

3 examples by agordon · Pull Request #652 · python-escpos/python-escpos · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .gif  (2) .jpg  (3) .py  (3) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
22 changes: 22 additions & 0 deletions examples/block_text.py
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3

from escpos import printer

txt = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla pellentesque augue libero. Integer non erat in velit venenatis tristique. Phasellus id ultrices orci. Quisque est ligula, varius vel justo sit amet, laoreet porttitor orci. Nulla commodo porta augue id molestie. Duis tempor eget tellus vel posuere."""

p = printer.Usb(0x04B8, 0x0E20)


for i in [20, 30, 40, 60]:
p.set_with_default(custom_size=True, width=3, height=3, underline=True)
p.textln(f"Blk-txt({i} col)")

p.set_with_default()
p.block_text(txt, columns=i)
p.ln(2)


p.ln(4)


p.cut(mode="PART", feed=True)
Binary file added examples/block_text_results.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Viewer requires iframe.
Binary file added examples/creature5.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Viewer requires iframe.
67 changes: 67 additions & 0 deletions examples/font_variations.py
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3

# This demo allows testing multiple combination of specific font variations.
# Two lines will be printed for every entry in "variations[]",
# First line showing the entry index and parametes,
# Second line being "Hello World".
#
# Example:
# If a printed line shows:
# variations[2] =
# Hello World
# F=b B U AL=R
# It is the 3rd entry (zero-based index 2),
# with Font="B", Bold, Underline, Align=right.

from escpos import printer

p = printer.Usb(0x04B8, 0x0E20, profile="TM-P80")

p.set_with_default()
p.textln("Default Text")

# Use any combination of parameters allowed in set_with_default()
# See: https://python-escpos.readthedocs.io/en/latest/user/methods.html
variations = [
{"font": "a", "custom_size": True, "width": 3, "height": 1},
{"font": "a", "custom_size": True, "width": 1, "height": 2},
{"font": "a", "bold": True},
{"font": "a", "custom_size": True, "width": 2, "height": 2},
{"font": "b", "underline": True},
{"font": "b", "align": "right", "double_width": True, "double_height": True},
]

for idx, v in enumerate(variations):
p.set_with_default(**v)

txt = "variations[%d] =" % idx
p.textln(txt)

p.textln("Hello World")

tags = []
if v.get("font"):
tags.append("F=" + v["font"])
if v.get("align"):
tags.append("AL=" + v.get("align")[:1])
if v.get("custom_size"):
tags.append("CS")
if "width" in v:
tags.append("W=" + str(v["width"]))
if "height" in v:
tags.append("H=" + str(v["height"]))
if v.get("bold"):
tags.append("B")
if v.get("underline"):
tags.append("U")
if v.get("smooth"):
tags.append("S")
if v.get("flip"):
tags.append("F")

txt = " ".join(tags)
p.textln(txt)

p.ln(1)

p.cut(mode="PART", feed=True)
Binary file added examples/font_variations_result.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Viewer requires iframe.
Binary file added examples/hocus-pocus.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Viewer requires iframe.
145 changes: 145 additions & 0 deletions examples/receipt.py
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/env python3

# Print a fake receipt

import re
import textwrap
from datetime import datetime

from escpos import printer

items = [
{"sku": 666, "desc": "Dragon Tears", "price": 5.6},
{"sku": 661, "desc": "Lizard Tongue", "price": 7.77},
{"sku": 911, "desc": "Bat Wing", "price": 13.7},
{"sku": 42, "desc": "Towel", "price": 19.99},
]
tax_percent = 0.05
header = {"sku": "SKU", "desc": "Product Description", "price": "Price"}
header_format = "{sku:<6} {desc:<33} {price:<6}"
item_format = "{sku:>06} {desc:<33} {price:>6.2f}"
subtotals_format = "{dummy:6} {desc:>33} {price:>6.2f}"

# As good as time as any
timestamp = datetime(2012, 12, 21, 11, 59, 59)
served_by = "Imruryg the Brave"

address = """57 Dandelion Tower Drive
Glimmerhollow, TQ, 981-PPU
Phone: +1-403-555-2106"""

disclaimer = """For entertainment purposes only.
Do not use for summoning demons and/or conjuration of spirits.
Magick should only be perfomed by trained professionals.
All sales are final. No refunds or exchanges for enchanted items.
Hocus Pocus will not be responsible for any damages, injuries, or
losses that occur while using or misusing these items.
Always check local bylaws and regulations before invoking any spells.
"""

# Font "b" on my TM-P80-clone printer can squeeze 64 characters per line
disclaimer_width = 64

recipt_barcode = "1234567890"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Not used.
Forgot to add the barcode to the receipt?
It would be great to see a barcode or a QR printed on the receipt to showcase the capabilities of python-escpos.



# Justify-text on left AND right sides by padding spaces,
# code by: Georgina Skibinski https://stackoverflow.com/a/66087666
def justify(txt: str, width: int) -> str:
prev_txt = txt
while (l := width - len(txt)) > 0:
txt = re.sub(r"(\s+)", r"\1 ", txt, count=l)
if txt == prev_txt:
break
return txt.rjust(width)
Comment on lines +46 to +54

belono Aug 6, 2024
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I really like this function.
I think we must add it to python-escpos as a private method so that other methods like block_text, software_columns and the like can make optional use of it.

What do you think @patkan @agordon ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Sounds good, feel free to create a change request :-)



p = printer.Usb(0x04B8, 0x0E20, profile="TM-P80")

# Store Logo at the Top
p.set_with_default()
p.image("hocus-pocus.gif", center=True)

# Print Address, centered
p.ln(1)
p.set_with_default(align="center")
for l in address.split("\n"):
p.textln(l)
p.ln(1)


# Print date and time
p.set_with_default(align="left")
p.textln(timestamp.strftime("%A, %B %d, %Y %I:%M%P"))

# Print cashier's name
p.set_with_default(align="right")
p.textln("Served by: " + served_by)

# Add some empty space before itemized list
p.ln(2)


## Add a bit of line spacing for itemized list for easier reading
p.set_with_default()
p.line_spacing(80, 180)

## Itemized list header (bold with underline)
p.set_with_default(bold=True, underline=True)
p.textln(header_format.format(**header))

## Itemized List
p.set_with_default()
for idx, item in enumerate(items):
txt = item_format.format(**item)
if idx == len(items) - 1:
# If this is the last item, add underline
# to visually "close" the list.
p.set_with_default(underline=True)
p.textln(txt)
Comment on lines +87 to +99

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We recently implemented the new software_columns method which simplifies formatting and printing lines of text as columns from a list of text items, so now users have an easy way to print something like "tables".
It would be great if this example file could include that functionality.


p.set_with_default()

## Subtotal
subtotal = sum([x["price"] for x in items])
p.textln(subtotals_format.format(dummy="", desc="subtotal", price=subtotal))

## Tax
tax_amount = subtotal * tax_percent
tax_desc = "Guild Tax (%d%%)" % (int(tax_percent * 100.0))
p.textln(subtotals_format.format(dummy="", desc=tax_desc, price=tax_amount))

## Total
## NOTE: because we use double-sized font, alignment won't match
## the previous lines. Instead, with trim leading whitespace,
## and use the printer's built-in right-alignment feature.
p.set_with_default(align="right", custom_size=True, width=2, height=2)
total_amount = subtotal + tax_amount
total_desc = "Total"
total_text_line = subtotals_format.format(dummy="", desc=total_desc, price=total_amount)
total_text_line = total_text_line.strip()
p.textln(total_text_line)

# Add some empty space before disclaimer
p.ln(4)

# preprocess disclaimer text:
# In python it's easy to use multilined strings,
# but to print the disclaimer we want to merge lines
# and condense whitespaces.
txt = disclaimer.replace("\n", " ")
txt = re.sub(" +", " ", txt)
# textwrap.wrap() ensures words are not broken (unlike "escpos.block_text()").
txt = textwrap.wrap(txt, width=disclaimer_width)
# Justify each line
txt = [justify(x, disclaimer_width) for x in txt]
p.set_with_default(font="b")
for l in txt:
p.textln(l)
Comment on lines +131 to +138

belono Aug 7, 2024
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
txt = re.sub(" +", " ", txt)
# textwrap.wrap() ensures words are not broken (unlike "escpos.block_text()").
txt = textwrap.wrap(txt, width=disclaimer_width)
# Justify each line
txt = [justify(x, disclaimer_width) for x in txt]
p.set_with_default(font="b")
for l in txt:
p.textln(l)
p.software_columns(text_list=[txt], widths=disclaimer_width, align='justify')

Again, the software_columns method is your friend here.
This will print the text in disclaimer as a single column of left aligned text wrapped to fit within 64 characters max width per line.
The only lack would be the justification of the text that I would like to see be part of the python-escpos tools in the near future.
EDIT 2026: Justification is already a feature


# A creature for good luck
p.set_with_default()
p.ln(2)
p.image("creature5.gif", center=True)

p.cut(mode="PART", feed=True)
Binary file added examples/receipt_results.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Viewer requires iframe.

Back | FazBrowse Home | New Git URL