-
Notifications
You must be signed in to change notification settings - Fork 307
3 examples #652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
3 examples #652
Changes from all commits
f63b0a7
f51af27
5f91b48
a831fd3
5894f0a
e19483a
6cd3574
06b4481
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) |
| 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) |
| 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" | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| # 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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". |
||||||||||||||||||||
|
|
||||||||||||||||||||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Again, the software_columns method is your friend here. |
||||||||||||||||||||
|
|
||||||||||||||||||||
| # A creature for good luck | ||||||||||||||||||||
| p.set_with_default() | ||||||||||||||||||||
| p.ln(2) | ||||||||||||||||||||
| p.image("creature5.gif", center=True) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| p.cut(mode="PART", feed=True) | ||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.