docstamp/cli/cli.py
def create(input, template, field, outdir, prefix, otype, command, index,
           dpi, verbose, unicode_support):
    """Use docstamp to create documents from the content of a CSV file or
    a Google Spreadsheet.

    Examples: \n
    docstamp create -i badge.csv -t badge_template.svg -o badges
    docstamp create -i badge.csv -t badge_template.svg -o ./badges -d pdf
    """
    logging.basicConfig(level=LOGGING_LVL)
    log = logging.getLogger(__name__)

    # setup verbose mode
    verbose_switch(verbose)

    input_file = input
    fields = field

    # init set of template contents
    log.debug('Reading CSV elements from {}.'.format(input_file))
    items, fieldnames = get_items_from_csv(input_file)

    # check if got any item
    if len(items) == 0:
        click.echo('Quiting because found 0 items.')
        exit(-1)

    if not fields:
        # set the number of zeros that the files will have
        n_zeros = int(math.floor(math.log10(len(items))) + 1)
    else:
        # check that fields has all valid fields
        for field_name in fields:
            if field_name not in fieldnames:
                raise ValueError('Field name {} not found in input file '
                                 ' header.'.format(field_name))

    # filter the items if index
    if index:
        myitems = {int(idx): items[int(idx)] for idx in index}
        items = myitems
        log.debug('Using the elements with index {} of the input '
                  'file.'.format(index))

    # make output folder
    if not os.path.exists(outdir):
        os.mkdir(outdir)

    # create template document model
    log.debug('Creating the template object using the file {}.'.format(template))
    template_doc = TextDocument.from_template_file(template, command)
    log.debug('Created an object of type {}.'.format(type(template_doc)))

    # let's stamp them!
    for idx in items:
        item = items[idx]

        if not len(fields):
            file_name = str(idx).zfill(n_zeros)
        else:
            field_values = []
            try:
                for field_name in fields:
                    field_values.append(item[field_name].replace(' ', ''))
            except:
                log.exception('Could not get field {} value from'
                              ' {}'.format(field_name, item))
                exit(-1)
            else:
                file_name = '_'.join(field_values)

        log.debug('Filling template {} with values of item {}.'.format(file_name, idx))
        try:
            template_doc.fill(item)
        except:
            log.exception('Error filling document for {}th item'.format(idx))
            continue

        # set output file path
        file_extension = get_extension(template)
        if prefix is None:
            basename = os.path.basename(template).replace(file_extension, '')

        file_name = basename + '_' + file_name
        file_path = os.path.join(outdir, file_name + '.' + otype)

        kwargs = {'file_type': otype,
                  'dpi': dpi,
                  'support_unicode': unicode_support}

        log.debug('Rendering file {}.'.format(file_path))
        try:
            template_doc.render(file_path, **kwargs)
        except:
            log.exception('Error creating {} for {}.'.format(file_path, item))
            exit(-1)
        else:
            log.debug('Successfully rendered {}.'.format(file_path))
def create(input, template, field, outdir, prefix, otype, command, index,
           dpi, verbose, unicode_support):
    """Use docstamp to create documents from the content of a CSV file or
    a Google Spreadsheet.

    Examples: \n
    docstamp create -i badge.csv -t badge_template.svg -o badges
    docstamp create -i badge.csv -t badge_template.svg -o ./badges -d pdf
    """
    logging.basicConfig(level=LOGGING_LVL)
    log = logging.getLogger(__name__)

    # setup verbose mode
    verbose_switch(verbose)

    input_file = input
    fields = field

    # init set of template contents
    log.debug('Reading CSV elements from {}.'.format(input_file))
    items, fieldnames = get_items_from_csv(input_file)

    # check if got any item
    if len(items) == 0:
        click.echo('Quiting because found 0 items.')
        exit(-1)

    if not fields:
        # set the number of zeros that the files will have
        n_zeros = int(math.floor(math.log10(len(items))) + 1)
    else:
        # check that fields has all valid fields
        for field_name in fields:
            if field_name not in fieldnames:
                raise ValueError('Field name {} not found in input file '
                                 ' header.'.format(field_name))

    # filter the items if index
    if index:
        myitems = {int(idx): items[int(idx)] for idx in index}
        items = myitems
        log.debug('Using the elements with index {} of the input '
                  'file.'.format(index))

    # make output folder
    if not os.path.exists(outdir):
        os.mkdir(outdir)

    # create template document model
    log.debug('Creating the template object using the file {}.'.format(template))
    template_doc = TextDocument.from_template_file(template, command)
    log.debug('Created an object of type {}.'.format(type(template_doc)))

    # let's stamp them!
    for idx in items:
        item = items[idx]

        if not len(fields):
            file_name = str(idx).zfill(n_zeros)
        else:
            field_values = []
            try:
                for field_name in fields:
                    field_values.append(item[field_name].replace(' ', ''))
            except:
                log.exception('Could not get field {} value from'
                              ' {}'.format(field_name, item))
                exit(-1)
            else:
                file_name = '_'.join(field_values)

        log.debug('Filling template {} with values of item {}.'.format(file_name, idx))
        try:
            template_doc.fill(item)
        except:
            log.exception('Error filling document for {}th item'.format(idx))
            continue

        # set output file path
        file_extension = get_extension(template)
        if prefix is None:
            basename = os.path.basename(template).replace(file_extension, '')

        file_name = basename + '_' + file_name
        file_path = os.path.join(outdir, file_name + '.' + otype)

        kwargs = {'file_type': otype,
                  'dpi': dpi,
                  'support_unicode': unicode_support}

        log.debug('Rendering file {}.'.format(file_path))
        try:
            template_doc.render(file_path, **kwargs)
        except:
            log.exception('Error creating {} for {}.'.format(file_path, item))
            exit(-1)
        else:
            log.debug('Successfully rendered {}.'.format(file_path))
def create ( input , template , field , outdir , prefix , otype , command , index , dpi , verbose , unicode_support ) : logging . basicConfig ( level = LOGGING_LVL ) log = logging . getLogger ( __name__ ) # setup verbose mode verbose_switch ( verbose ) input_file = input fields = field # init set of template contents log . debug ( 'Reading CSV elements from {}.' . format ( input_file ) ) items , fieldnames = get_items_from_csv ( input_file ) # check if got any item if len ( items ) == 0 : click . echo ( 'Quiting because found 0 items.' ) exit ( - 1 ) if not fields : # set the number of zeros that the files will have n_zeros = int ( math . floor ( math . log10 ( len ( items ) ) ) + 1 ) else : # check that fields has all valid fields for field_name in fields : if field_name not in fieldnames : raise ValueError ( 'Field name {} not found in input file ' ' header.' . format ( field_name ) ) # filter the items if index if index : myitems = { int ( idx ) : items [ int ( idx ) ] for idx in index } items = myitems log . debug ( 'Using the elements with index {} of the input ' 'file.' . format ( index ) ) # make output folder if not os . path . exists ( outdir ) : os . mkdir ( outdir ) # create template document model log . debug ( 'Creating the template object using the file {}.' . format ( template ) ) template_doc = TextDocument . from_template_file ( template , command ) log . debug ( 'Created an object of type {}.' . format ( type ( template_doc ) ) ) # let's stamp them! for idx in items : item = items [ idx ] if not len ( fields ) : file_name = str ( idx ) . zfill ( n_zeros ) else : field_values = [ ] try : for field_name in fields : field_values . append ( item [ field_name ] . replace ( ' ' , '' ) ) except : log . exception ( 'Could not get field {} value from' ' {}' . format ( field_name , item ) ) exit ( - 1 ) else : file_name = '_' . join ( field_values ) log . debug ( 'Filling template {} with values of item {}.' . format ( file_name , idx ) ) try : template_doc . fill ( item ) except : log . exception ( 'Error filling document for {}th item' . format ( idx ) ) continue # set output file path file_extension = get_extension ( template ) if prefix is None : basename = os . path . basename ( template ) . replace ( file_extension , '' ) file_name = basename + '_' + file_name file_path = os . path . join ( outdir , file_name + '.' + otype ) kwargs = { 'file_type' : otype , 'dpi' : dpi , 'support_unicode' : unicode_support } log . debug ( 'Rendering file {}.' . format ( file_path ) ) try : template_doc . render ( file_path , * * kwargs ) except : log . exception ( 'Error creating {} for {}.' . format ( file_path , item ) ) exit ( - 1 ) else : log . debug ( 'Successfully rendered {}.' . format ( file_path ) )
Use docstamp to create documents from the content of a CSV file or
    a Google Spreadsheet.

    Examples: \n
    docstamp create -i badge.csv -t badge_template.svg -o badges
    docstamp create -i badge.csv -t badge_template.svg -o ./badges -d pdf
Use docstamp to create documents from the content of a CSV file or a Google Spreadsheet .