Wednesday, July 13, 2011

Generate BarCode in Python

You can Generate Barcode (image, Pdf Etc...) in Python
Example is for JPG file
from reportlab.graphics.barcode import createBarcodeDrawing, \
getCodes
def get_image(self, value, width, hight, hr, code='QR'):
""" genrating image for barcode """
options = {}

if width:options['width'] = width
if hight:options['hight'] = hight
if hr:options['humanReadable'] = hr
try:
ret_val = createBarcodeDrawing(code, value=value,
**options)
except Exception, e:
raise osv.except_osv('Error', e)
return ret_val.asString('jpg')


To get Supported barcode in your system

$ print getCodes()

reference : report lab

Tuesday, February 8, 2011

Convert PDF in Text format

import popen2
from StringIO import StringIO
class InputStreamReader(object):

def __init__(self, inputStream, encoding):

super(InputStreamReader, self).__init__()
self.inputStream = inputStream
self.encoding = encoding or 'utf-8'

def _read(self, length):

return self.inputStream.read(length)

def read(self, length=-1):

text = self._read(length)
text = unicode(text, self.encoding)
return text

def close(self):

self.inputStream.close()

process = popen2.Popen4(["pdftotext", "-enc", "UTF-8", 'Full_Path', "-"])
data=InputStreamReader(process.fromchild, 'utf-8')._read(-1)
print data