Skip to content

Commit 6953a86

Browse files
author
Greg Taylor
committed
Add examples of how to store or print labels.
1 parent de6419c commit 6953a86

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

examples/create_shipment.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This example shows how to ship shipments.
44
"""
55
import logging
6+
import binascii
67
from fedex.services.ship_service import FedexProcessShipmentRequest
78
from fedex.config import FedexConfig
89

@@ -98,3 +99,38 @@
9899
# This will show the reply to your shipment being sent. You can access the
99100
# attributes through the response attribute on the request object.
100101
print ship.response
102+
103+
# Get the label image in ASCII format from the reply.
104+
ascii_label_data = ship.response.CompletedShipmentDetail.CompletedPackageDetails[0].Label.Parts[0].Image
105+
# Convert the ASCII data to binary.
106+
label_binary_data = binascii.a2b_base64(ascii_label_data)
107+
# This will be the file we write the label out to.
108+
png_file = open('example_shipment_label.png', 'wb')
109+
110+
"""
111+
This is an example of how to dump a label to a PNG file.
112+
"""
113+
png_file.write(label_binary_data)
114+
png_file.close()
115+
116+
"""
117+
This is an example of how to print the label to a serial printer. This will not
118+
work for all label printers, consult your printer's documentation for more
119+
details on what formats it can accept.
120+
"""
121+
# Pipe the binary directly to the label printer. Works under Linux
122+
# without requiring PySerial. This WILL NOT work on other platforms.
123+
#label_printer = open("/dev/ttyS0", "w")
124+
#label_printer.write(label_binary_data)
125+
#label_printer.close()
126+
127+
"""
128+
This is a potential cross-platform solution using pySerial. This has not been
129+
tested in a long time and may or may not work. For Windows, Mac, and other
130+
platforms, you may want to go this route.
131+
"""
132+
#import serial
133+
#label_printer = serial.Serial(0)
134+
#print "SELECTED SERIAL PORT: "+ label_printer.portstr
135+
#label_printer.write(label_binary_data)
136+
#label_printer.close()

0 commit comments

Comments
 (0)