Exemple #1
0
def exec_pdfmerge(files, pages, outfile, merge=False, silent=False):
    """
    Purpose:
      Executes pdfmerge (2.7x compatiable) command to grab necessary pages
      and merge them if desired

    Require installing pdfmerge:
    https://pypi.python.org/pypi/pdfmerge/0.0.7
      > pip install pdfmerge

    :param files: list
      List of files (must include full path)
    :param pages: list
      List of strings indicating pages to extract.
      E.g.: ['4,6,15,20','3,8,44,50']
    :param outfile: list or str
      Either List of files to write or a single file if merge == True
    :param merge: bool
      Indicate whether to merge into a single file, [outfile]
    :param silent: bool
      Indicate whether to silence messages

    Notes
    -----
    Created by Chun Ly, 22 January 2018
    """

    if py_vers != 2:
        version_full = sys.version.split(' ')[0]
        log.warning("exec_pdfmerge: Incompatible with python %s" % version_full)
        raise SystemError("Incompatible with python %s" % version_full)

    if not merge:
        if len(outfile) != len(files):
            log.warn('### outfile input not complete. Missing files!')

    if not silent:
        log.info('### Begin exec_pdfmerge : ' + systime())

    n_files = len(files)

    writer0 = None

    for nn in range(n_files):
        writer0 = pdfmerge.add(files[nn], rules=pages[nn], writer=writer0)

        if not merge:
            with open(outfile[nn], 'wb') as stream:
                writer0.write(stream)
            writer0 = None

    if merge:
        with open(outfile, 'wb') as stream:
            writer0.write(stream)

    if not silent:
        log.info('### End exec_pdfmerge : ' + systime())
Exemple #2
0
 def test_rule(self):
     """Merge part of a PDF."""
     writer = pdfmerge.add(TEST_PDF_2 + '[2]')
     self.assertTrue(writer is not None)
     self.assertTrue(1, writer.getNumPages())
Exemple #3
0
 def test_merge_dir(self):
     """Merge all PDFs in a directory."""
     writer = pdfmerge.add(TEST_PDF_DIR, password='******')
     self.assertTrue(writer is not None)
     self.assertTrue(4, writer.getNumPages())
Exemple #4
0
 def test_pass_single(self):
     """A non-merge."""
     writer = pdfmerge.add(TEST_PDF_1)
     self.assertTrue(writer is not None)
     self.assertTrue(1, writer.getNumPages())