def generate( name: str, code: str, writer=None, output: Union[str, os.PathLike, BinaryIO] = None, writer_options: Dict = None, text: str = None, ): """Shortcut to generate a barcode in one line. :param name: Name of the type of barcode to use. :param code: Data to encode into the barcode. :param writer: A writer to use (e.g.: ImageWriter or SVGWriter). :param output: Destination file-like or path-like where to save the generated barcode. :param writer_options: Options to pass on to the writer instance. :param text: Text to render under the barcode. """ from barcode.base import Barcode writer = writer or Barcode.default_writer() writer.set_options(writer_options or {}) barcode = get(name, code, writer) if isinstance(output, str): fullname = barcode.save(output, writer_options, text) return fullname elif output: barcode.write(output, writer_options, text) else: raise TypeError("'output' cannot be None")
def __init__(self, i2of5, writer=None): if not i2of5.isdigit(): raise IllegalCharacterError('Code can only contain numbers.') if not len(i2of5)%2 == 0: raise NumberOfDigitsError('Code must have an even number of digits') self.i2of5 = i2of5 self.writer = writer or Barcode.default_writer()
def __init__(self, ean, writer=None): ean = ean[:self.digits] if not ean.isdigit(): raise IllegalCharacterError('Code can only contain numbers.') self.ean = ean self.ean = '{0}{1}'.format(ean, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()
def __init__(self, ean, writer=None): ean = ean[:self.digits] if not ean.isdigit(): raise IllegalCharacterError('Code can only contain numbers.') self.ean = ean self.ean = '{0}{1}'.format(ean, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()
def __init__(self, i2of5, writer=None): if not i2of5.isdigit(): raise IllegalCharacterError('Code can only contain numbers.') if not len(i2of5) % 2 == 0: raise NumberOfDigitsError( 'Code must have an even number of digits') self.i2of5 = i2of5 self.writer = writer or Barcode.default_writer()
def __init__(self, code, writer=None, narrow=2, wide=5): if not code.isdigit(): raise IllegalCharacterError('ITF code can only contain numbers.') #Length must be even, prepend 0 if necessary if len(code) % 2 != 0: code = '0' + code self.code = code self.writer = writer or Barcode.default_writer() self.narrow = narrow self.wide = wide
def __init__(self, code, writer=None, add_checksum=True): code = code.upper() for char in code: if char not in REF: raise IllegalCharacterError('Character {0!r} not valid for ' 'Code 39.'.format(char)) self.code = code if add_checksum: self.code += self.calculate_checksum() self.writer = writer or Barcode.default_writer()
def __init__(self, code, writer=None, add_checksum=True): code = code.upper() for char in code: if char not in REF: raise IllegalCharacterError('Character {0!r} not valid for ' 'Code 39.'.format(char)) self.code = code if add_checksum: self.code += self.calculate_checksum() self.writer = writer or Barcode.default_writer()
def __init__(self, ean, writer=None): ean = ean[:self.digits] if not ean.isdigit(): raise IllegalCharacterError('EAN code can only contain numbers.') if len(ean) != self.digits: raise NumberOfDigitsError('EAN must have {0} digits, not ' '{1}.'.format(self.digits, len(ean))) self.ean = ean self.ean = '{0}{1}'.format(ean, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()
def __init__(self, code, writer=None, narrow=2, wide=5): if not code.isdigit(): raise IllegalCharacterError('ITF code can only contain numbers.') #Length must be even, prepend 0 if necessary if len(code) % 2 != 0: code = '0' + code self.code = code self.writer = writer or Barcode.default_writer() self.narrow = narrow self.wide = wide
def __init__(self, ean, writer=None): ean = ean[:self.digits] if not ean.isdigit(): raise IllegalCharacterError('EAN code can only contain numbers.') if len(ean) != self.digits: raise NumberOfDigitsError('EAN must have {0} digits, not ' '{1}.'.format(self.digits, len(ean))) self.ean = ean self.ean = '{0}{1}'.format(ean, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()
def __init__(self, upc, writer=None, make_ean=False): self.ean = make_ean upc = upc[:self.digits] if not upc.isdigit(): raise IllegalCharacterError('UPC code can only contain numbers.') if len(upc) != self.digits: raise NumberOfDigitsError('UPC must have {0} digits, not ' '{1}.'.format(self.digits, len(upc))) self.upc = upc self.upc = '{}{}'.format(upc, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()
def __init__(self, upc, writer=None, make_ean=False): self.ean = make_ean upc = upc[:self.digits] if not upc.isdigit(): raise IllegalCharacterError('UPC code can only contain numbers.') if len(upc) != self.digits: raise NumberOfDigitsError('UPC must have {0} digits, not ' '{1}.'.format(self.digits, len(upc))) self.upc = upc self.upc = '{}{}'.format(upc, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()
def __init__(self, code: str, writer=None, add_checksum: bool = True): r""" :param code: Code 39 string without \* and without checksum. :param writer: A ``barcode.writer`` instance used to render the barcode (default: SVGWriter). :param add_checksum: Add the checksum to code or not """ self.code = code.upper() if add_checksum: self.code += self.calculate_checksum() self.writer = writer or Barcode.default_writer() check_code(self.code, self.name, code39.REF)
def __init__(self, ean, writer=None): """Initializes EAN13 object. :parameters: ean : String The ean number as string. writer : barcode.writer Instance The writer to render the barcode (default: SVGWriter). """ ean = ean[:self.digits] if not ean.isdigit(): raise IllegalCharacterError('Code can only contain numbers.') self.ean = ean self.ean = '{0}{1}'.format(ean, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()
def __init__(self, ean, writer=None, **kwargs): no_checksum = False if "no_checksum" in kwargs.keys(): no_checksum = kwargs["no_checksum"] ean = ean[:self.digits] if not ean.isdigit(): raise IllegalCharacterError('EAN code can only contain numbers.') if len(ean) != self.digits: raise NumberOfDigitsError('EAN must have {0} digits, not ' '{1}.'.format(self.digits, len(ean))) self.ean = ean # If no checksum if no_checksum: # Add a thirteen char if given in parameter, otherwise pad with zero self.ean = '{0}{1}'.format(ean, ean[self.digits] if len(ean) > self.digits else 0) else: self.ean = '{0}{1}'.format(ean, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()
def __init__(self, ean, writer=None, no_checksum=False): ean = ean[:self.digits] if not ean.isdigit(): raise IllegalCharacterError("EAN code can only contain numbers.") if len(ean) != self.digits: raise NumberOfDigitsError( "EAN must have {} digits, not {}.".format( self.digits, len(ean), )) self.ean = ean # If no checksum if no_checksum: # Add a thirteen char if given in parameter, # otherwise pad with zero self.ean = "{}{}".format( ean, ean[self.digits] if len(ean) > self.digits else 0) else: self.ean = "{}{}".format(ean, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()
def __init__(self, code, writer=None, add_checksum=True): """Initializes a new Code39 instance. :parameters: code : String Code39 string without \* and checksum (added automatically if `add_checksum` is True). writer : barcode.writer Instance The writer to render the barcode (default: SVGWriter). add_checksum : Boolean Add the checksum to code or not. """ code = code.upper() for char in code: if char not in REF: raise IllegalCharacterError('Character {0!r} not valid for ' 'Code 39.'.format(char)) self.code = code if add_checksum: self.code += self.calculate_checksum() self.writer = writer or Barcode.default_writer()
def __init__(self, upc, writer=None, make_ean=False): """Initializes new UPC-A barcode. :param str upc: The upc number as string. :param writer: barcode.writer instance. The writer to render the barcode (default: SVGWriter). :param bool make_ean: Indicates if a leading zero should be added to the barcode. This converts the UPC into a valid European Article Number (EAN). """ self.ean = make_ean upc = upc[:self.digits] if not upc.isdigit(): raise IllegalCharacterError("UPC code can only contain numbers.") if len(upc) != self.digits: raise NumberOfDigitsError( "UPC must have {} digits, not {}.".format( self.digits, len(upc))) self.upc = upc self.upc = "{}{}".format(upc, self.calculate_checksum()) self.writer = writer or Barcode.default_writer()
def __init__(self, code, writer=None): self.code = code self.writer = writer or Barcode.default_writer() self._charset = 'B' self._buffer = '' check_code(self.code, self.name, code128.ALL)
def __init__(self, code, writer=None): self.add_checksum_default = True self.checksum_added = False self.code = code.upper() self.writer = writer or Barcode.default_writer() check_code(self.code, self.name, code39.REF)
def __init__(self, code, writer=None, add_checksum=True): self.code = code.upper() if add_checksum: self.code += self.calculate_checksum() self.writer = writer or Barcode.default_writer() check_code(self.code, self.name, REF)
def __init__(self, code, writer=None): self.code = code self.writer = writer or Barcode.default_writer() self._charset = 'B' self._buffer = '' check_code(self.code, self.name, code128.ALL)
def __init__(self, code, writer=None, add_checksum=True): self.code = code.upper() if add_checksum: self.code += self.calculate_checksum() self.writer = writer or Barcode.default_writer() check_code(self.code, self.name, REF)