lincolnloop / python-qrcode

Python QR Code image generator
https://pypi.python.org/pypi/qrcode
Other
4.43k stars 676 forks source link

Add option to return string from print_ascii() #162

Closed kayagoban closed 3 years ago

kayagoban commented 5 years ago

My application needs the ability to grab a string of ascii instead of having it printed to the screen. This change allows the print_ascii method to optionally return a string.

fionafibration commented 5 years ago

You can probably just write into a StringIO or BytesIO object to grab the string generated by print_ascii

normanr commented 4 years ago

See also #104

maribedran commented 3 years ago

As @Fiona1729 mentioned, this can be achieved with:

import io
import qrcode
qr = qrcode.QRCode()
qr.add_data("Some text")
f = io.StringIO()
qr.print_ascii(out=f)
f.seek(0)
print(f.read())

Will add this example to the docs.