Ejemplo n.º 1
0
    if ext == 'pdf': return file
    elif ext in ['jpg', 'jpeg', 'png']:
        image = Image.open(file)
        image.convert('RGB')
        filename = f'{file}.pdf'
        image.save(filename)
        return filename
    else:
        raise TypeError('unsupported extension')


p = ArgumentParser()
p.add_argument(
    '--src',
    type=str,
    required=True,
    help='Input files separated by ",". Supports png, jpg and pdf files')
p.add_argument('--res',
               type=str,
               help='name of output file',
               default='merged.pdf')
args = p.parse_args()
pdfs = [convert(f) for f in args.src.split(',')]
m = PdfFileMerger()
for pdf in pdfs:
    m.append(pdf)
m.write(args.res)
m.close()
for pdf in pdfs:
    os.remove(pdf)
Ejemplo n.º 2
0
def home():
    if not session.get('logged_in'):
        return render_template('login.html')
    else:
        optionKeys = fline.split(';')
        options = ''
        for o in optionKeys:
            options += '<label ><input type="checkbox" name="keys" value="' + o + '" style="display:none">' + o + '</label>\n'
        if request.method == 'POST':
            fileList = request.files.getlist('files[]')
            keyOptions = request.form.getlist('keys')
            result_type = request.form['result_type']
            folder = 'ChordPro_download_' + datetime.now().strftime(
                '%Y%m%d_%H%M') + '_' + str(
                    len(fileList) * len(keyOptions)) + '_'
            path = ('user' + os.sep + session['user'] + os.sep + 'download' +
                    os.sep + folder + os.sep).encode('ascii', 'ignore')

            if os.path.exists('user' + os.sep + session['user'] + os.sep):
                shutil.rmtree('user' + os.sep + session['user'] + os.sep)
            if not os.path.exists(path):
                os.makedirs(path)

            for f in fileList:
                if f.content_type == "text/rtf":
                    for keyOption in keyOptions:
                        print f.filename, keyOption
                        keyOption = keyOption.encode('ascii', 'ignore')
                        if keyOption == 'original':
                            keyOption = None
                        try:
                            print f
                            convert(path, f, keyOption)
                        except KeyError as e:
                            print str(
                                e
                            ) + ' not found in chords for key of the file. Are you sure your file has the correct key?'
                            flash('hi')
                        except Exception as e:
                            print 'error:', e
                    print result_type
                    if result_type == 'PDF':
                        songs = os.listdir(path)
                        print songs
                        merger = PdfFileMerger()
                        result = StringIO.StringIO()
                        for song in songs:
                            merger.append(open(path + song, 'rb'))
                        merger.write(result)
                        result.seek(0)
                        result_name = folder + '.pdf'
                        #return send_file(result,attachment_filename=folder+'.pdf', as_attachment=True)
                    if result_type == 'ZIP':
                        shutil.make_archive(
                            'user' + os.sep + session['user'] + os.sep +
                            folder, 'zip', 'user' + os.sep + session['user'] +
                            os.sep + 'download' + os.sep)
                        result = 'user' + os.sep + session[
                            'user'] + os.sep + folder + '.zip'
                        result_name = folder + '.zip'
                        #return send_file('user'+os.sep+session['user']+os.sep+folder+'.zip',folder+'.zip', as_attachment=True)
                else:
                    return render_template(
                        'uploader.html',
                        error="Please only upload *.rtf files",
                        options=options)
            return send_file(result,
                             attachment_filename=result_name,
                             as_attachment=True)
        else:
            return render_template('uploader.html', options=options)
Ejemplo n.º 3
0
import os
from PyPDF2 import PdfFileMerger, PdfFileReader

# Call the PdfFileMerger
mergedObject = PdfFileMerger()

number_of_pdfs = 0

for file in os.listdir("new/"):
    print(file)
    number_of_pdfs = number_of_pdfs + 1
    print(number_of_pdfs)
    os.rename(f"new/{file}", f"new/{number_of_pdfs}.pdf")
    mergedObject.append(
        PdfFileReader("new/" + str(number_of_pdfs) + '.pdf', 'rb'))
    mergedObject.append(PdfFileReader("pb/page_break.pdf", 'rb'))

# I had 116 files in the folder that had to be merged into a single document
# Loop through all of them and append their pages
# Folder name is in the PDFFilereader(exmaple)

# # Write all the files into a file which is named as shown below
mergedObject.write("mergetest.pdf")
Ejemplo n.º 4
0
    def execute(self, entry):
        """Print a print job.

        This is the core of PrintingService.

        entry (QueueEntry): the entry containing the operation to
            perform.

        """
        # TODO: automatically re-enqueue in case of a recoverable
        # error.
        printjob_id = entry.item.printjob_id
        with SessionGen() as session:
            # Obtain print job.
            printjob = PrintJob.get_from_id(printjob_id, session)
            if printjob is None:
                raise ValueError("Print job %d not found in the database." %
                                 printjob_id)
            user = printjob.participation.user
            contest = printjob.participation.contest
            timezone = get_timezone(user, contest)
            timestr = str(
                printjob.timestamp.replace(
                    tzinfo=utc).astimezone(timezone).replace(tzinfo=None))
            filename = printjob.filename

            # Check if it's ready to be printed.
            if printjob.done:
                logger.info("Print job %d was already sent to the printer.",
                            printjob_id)

            directory = tempfile.mkdtemp(dir=config.temp_dir)
            logger.info("Preparing print job in directory %s", directory)

            # Take the base name just to be sure.
            relname = "source_" + os.path.basename(filename)
            source = os.path.join(directory, relname)
            with open(source, "wb") as file_:
                self.file_cacher.get_file_to_fobj(printjob.digest, file_)

            if filename.endswith(".pdf") and config.pdf_printing_allowed:
                source_pdf = source
            else:
                # Convert text to ps.
                source_ps = os.path.join(directory, "source.ps")
                cmd = [
                    "a2ps", source, "--delegate=no", "--output=" + source_ps,
                    "--medium=%s" % config.paper_size.capitalize(),
                    "--portrait", "--columns=1", "--rows=1",
                    "--pages=1-%d" % (config.max_pages_per_job), "--header=",
                    "--footer=", "--left-footer=", "--right-footer=",
                    "--center-title=" + filename, "--left-title=" + timestr
                ]
                ret = subprocess.call(cmd, cwd=directory)
                if ret != 0:
                    raise Exception(
                        "Failed to convert text file to ps with command: %s"
                        "(error %d)" % (pretty_print_cmdline(cmd), ret))

                if not os.path.exists(source_ps):
                    logger.warning("Unable to convert from text to ps.")
                    printjob.done = True
                    printjob.status = [N_("Invalid file")]
                    session.commit()
                    rmtree(directory)
                    return

                # Convert ps to pdf
                source_pdf = os.path.join(directory, "source.pdf")
                cmd = [
                    "ps2pdf",
                    "-sPAPERSIZE=%s" % config.paper_size.lower(), source_ps
                ]
                ret = subprocess.call(cmd, cwd=directory)
                if ret != 0:
                    raise Exception(
                        "Failed to convert ps file to pdf with command: %s"
                        "(error %d)" % (pretty_print_cmdline(cmd), ret))

            # Find out number of pages
            with open(source_pdf, "rb") as file_:
                pdfreader = PdfFileReader(file_)
                page_count = pdfreader.getNumPages()

            logger.info("Preparing %d page(s) (plus the title page)",
                        page_count)

            if page_count > config.max_pages_per_job:
                logger.info("Too many pages.")
                printjob.done = True
                printjob.status = [N_("Print job has too many pages")]
                session.commit()
                rmtree(directory)
                return

            # Add the title page
            title_tex = os.path.join(directory, "title_page.tex")
            title_pdf = os.path.join(directory, "title_page.pdf")
            with open(title_tex, "wb") as f:
                f.write(
                    self.jinja2_env.get_template("title_page.tex").render(
                        user=user,
                        filename=filename,
                        timestr=timestr,
                        page_count=page_count,
                        paper_size=config.paper_size))
            cmd = ["pdflatex", "-interaction", "nonstopmode", title_tex]
            ret = subprocess.call(cmd, cwd=directory)
            if ret != 0:
                raise Exception("Failed to create title page with command: %s"
                                "(error %d)" %
                                (pretty_print_cmdline(cmd), ret))

            pdfmerger = PdfFileMerger()
            with open(title_pdf, "rb") as file_:
                pdfmerger.append(file_)
            with open(source_pdf, "rb") as file_:
                pdfmerger.append(file_)
            result = os.path.join(directory, "document.pdf")
            with open(result, "wb") as file_:
                pdfmerger.write(file_)

            try:
                printer_connection = cups.Connection()
                printer_connection.printFile(config.printer, result,
                                             "Printout %d" % printjob_id, {})
            except cups.IPPError as error:
                logger.error("Unable to print: `%s'.", error)
            else:
                printjob.done = True
                printjob.status = [N_("Sent to printer")]
                session.commit()
            finally:
                rmtree(directory)
Ejemplo n.º 5
0
def merge(file_names):
    merged_pdf = PdfFileMerger()
    for file in file_names:
        pdf = open(file, "rb")
        merged_pdf.append(PdfFileReader(pdf))
    merged_pdf.write("merged.pdf")
Ejemplo n.º 6
0
def pdf_merge(args, name, step, total):
    dpi = 100

    merger = PdfFileMerger(strict=False)

    error_count = 0
    temp_file_list = []

    bar = IncrementalBar("%02d/%02d" % (step, total),
                         max=len(args),
                         fill='@',
                         suffix='%(percent)d%%')

    for i, item in enumerate(args):
        # 경로를 절대경로로 변환
        item = os.path.abspath(item)

        # 파일이름과 확장자 분리
        filename, extenstion = os.path.splitext(item)
        extenstion = extenstion.lower()

        # 원소가 이미지인 경우 pdf로 변환
        if extenstion in [".jpg", ".png"]:
            temp = "%s_temp.pdf" % filename
            pdf = FPDF('P', 'mm', 'A4')
            pdf.add_page()

            im = Image.open(item)
            width, height = im.size
            im.close()

            width = (width * dpi) / 25.4
            height = (height * dpi) / 25.4
            scalex = 210 / width
            scaley = 297 / height

            if scalex < scaley:
                scale = scalex
            else:
                scale = scaley

            if scale > 1:
                scale = 1

            resize_width = scale * width
            resize_height = scale * height

            x = (210 - resize_width) // 2
            y = (297 - resize_height) // 2

            if x < 0:
                x = 0
            if y < 0:
                y = 0
            # print(x)
            # print(y)
            # print(resize_width)
            # print(resize_height)

            pdf.image(item, x, y, resize_width, resize_height)
            pdf.output(temp, "F")
            temp_file_list.append(temp)
            merger.append(temp)
        else:
            merger.append(item)

        bar.next()
        sleep(0.1)

    merger.write(name)
    merger.close()

    for k in temp_file_list:
        os.remove(k)

    bar.finish()
Ejemplo n.º 7
0
def join_pdfs(pdfs, idd):
    merger = PdfFileMerger()
    for pdf in pdfs:
        merger.append(pdf)
    merger.write(os.path.join("/mnt/ramdisk", idd, "result.pdf"))
    merger.close()
Ejemplo n.º 8
0
def _merge(pdf_file_lists, out_file_path):
    merger = PdfFileMerger()
    for pdf_file in pdf_file_lists:
        merger.append(PdfFileReader(open(pdf_file, mode='rb')))
    merger.write(out_file_path)
    merger.close()
Ejemplo n.º 9
0
def output_to_pdf(output, file_name, avrgs, wat, hydro, input_list, investigated_residue):
    file_name = investigated_residue
    f = file_name + '0_occupancies.pdf'
    surface = cairo.PDFSurface(f, 595, 842)
    ctx = cairo.Context(surface)

    # title
    ctx.set_font_size(20)
    ctx.set_source_rgb(0, 0, 0)
    ctx.move_to(20, 30)
    title = investigated_residue

    if hydro:
        title += " - hydrogen stripped"

    if wat:
        title += " - water stripped"

    ctx.show_text(title)

    ctx.set_font_size(12)
    y = 50
    counter = 0
    for item in input_list:
        ctx.move_to(20, y)
        ctx.show_text("#" + str(counter) + " " + ''.join(item))
        y += 14
        counter += 1

    ctx.set_font_size(20)
    x = 20
    y = y + 15
    ctx.set_font_size(14)
    output = output.splitlines()
    pages = 0
    files = []

    for line in output:
        line = line.split(',')
        ctx.set_source_rgb(0, 0, 0)

        ctx.move_to(x, y)
        ctx.show_text(line[0])
        x += 120

        # coloring
        if len(line[0]) > 0 and line[0][0].isdigit() or line[0] == 'SUM':
            ctx.move_to(x, y)
            ctx.show_text(line[1])
            x += 75
            ctx.set_source_rgb(0, 0, 0)

            if float(line[1]) > float(line[2]):
                ctx.set_source_rgb(0.9, 0, 0)
            if float(line[1]) < float(line[2]):
                ctx.set_source_rgb(0, 0.7, 0)
            ctx.move_to(x, y)
            ctx.show_text(line[2])
            x += 75
            ctx.set_source_rgb(0, 0, 0)

            if float(line[1]) > float(line[3]):
                ctx.set_source_rgb(0.9, 0, 0)
            if float(line[1]) < float(line[3]):
                ctx.set_source_rgb(0, 0.7, 0)
            ctx.move_to(x, y)
            ctx.show_text(line[3])
            x += 75
            ctx.set_source_rgb(0, 0, 0)

            if avrgs:
                ctx.move_to(x, y)
                ctx.show_text(str(round(float(line[4]), 2)))
                x += 75
                ctx.move_to(x, y)
                ctx.show_text(str(round(float(line[5]), 2)))
                x += 75
                ctx.move_to(x, y)
                ctx.show_text(str(round(float(line[6]), 2)))
                x += 75
        else:
            for item in line[1:]:
                ctx.move_to(x, y)
                ctx.show_text(item)
                x += 75

        y += 16
        x = 20
        if y > 820:
            pages += 1
            files.append(f)
            surface.finish()
            surface.flush()
            f = file_name + str(pages) + '_occupancies.pdf'
            files.append(f)
            surface = cairo.PDFSurface(f, 595, 842)
            ctx = cairo.Context(surface)
            ctx.set_font_size(14)
            y = 30

    surface.finish()
    surface.flush()

    merger = PdfFileMerger()
    for f in files:
        merger.append(f, 'rb')
    merger.write(file_name + '_occupancies.pdf')

    # remove merged pdf files
    for item in files:
        remove(item)
Ejemplo n.º 10
0
def by_inserting():
    merger = PdfFileMerger()
    merger.append("samplePdf1.pdf")
    merger.merge(0, "samplePdf2.pdf")
    merger.write("mergedPdf1.pdf")
Ejemplo n.º 11
0
 def clear_all(self):
     self.merger = PdfFileMerger()
     self.scans.clear()
     self.scans_in_progress = False
     self.ready_to_save = False
     self.update_scan_status()
Ejemplo n.º 12
0
def file_merge(file_dict):
    merger = PdfFileMerger()
    for _, fileName in file_dict.items():
        merger.append(PdfFileReader(fileName, "rb"))
    merger.write(output_file)
    return output_file
Ejemplo n.º 13
0
 def assemble(self):
     merger = PdfFileMerger()
     filled_pages = [x.fill() for x in self.pages]
     for page in filled_pages:
         merger.append(page)
     return merger
Ejemplo n.º 14
0
def print_from_urls(urls, print_option={}):
    browser = pychrome.Browser()
    browser.print_option = default_print_option.copy()
    browser.print_option.update(print_option)

    # close_all_tabs(browser)

    tabs = []
    for i in range(len(urls)):
        tabs.append(browser.new_tab())

    for i, tab in enumerate(tabs):
        eh = EventHandler(browser, tab)
        tab.Page.frameStartedLoading = eh.frame_started_loading
        tab.Page.frameStoppedLoading = eh.frame_stopped_loading

        tab.start()

        # tab.Emulation.setVisibleSize(
        #     width=browser.print_option.get('screen-width'),
        #     height=browser.print_option.get('screen-height')
        # )

        tab.Emulation.setDeviceMetricsOverride(
            width=browser.print_option.get('screen-width'),
            height=browser.print_option.get('screen-height'),
            deviceScaleFactor=0,
            mobile=False,
            viewport={
                'x': 0,
                'y': 0,
                'width': 1024,
                'height': 1024,
                'scale': 1,
            },
        )

        tab.Page.stopLoading()
        tab.Page.enable()
        tab.url = urls[i]
        tab.Page.navigate(url=urls[i])

    # wait until all printdata ready or timeout reached
    time_limit = datetime.now() + timedelta(
        seconds=browser.print_option.get('timeout', 0))
    while True:
        all_printdata_ready = True
        for tab in tabs:
            try:
                tab.printdata['data']
            except Exception as e:
                all_printdata_ready = False
        if datetime.now() >= time_limit or all_printdata_ready:
            break
        time.sleep(1)

    # ensure close all tabs after timeout
    for tab in tabs:
        tab.stop()
        browser.close_tab(tab.id)

    merger = PdfFileMerger()
    for tab in tabs:
        try:
            tab.printdata['data']
        except Exception as e:
            continue
        else:
            merger.append(BytesIO(base64.b64decode(tab.printdata['data'])))

    merged = BytesIO()
    merger.write(merged)

    # DEBUG: save pdf to local file
    # with open("%s.pdf" % datetime.now().strftime('%c'), "wb") as fd:
    #     fd.write(merged.getvalue())

    return merged.getvalue()
Ejemplo n.º 15
0
def pdfcompiler(pdfs):
       merger = PdfFileMerger()
       for pdf in pdfs:
              merger.append(pdf)
       name = input("Please give a name for the combined PDF file (text only, no '.pdf'): ")
       merger.write(name + ".pdf")
Ejemplo n.º 16
0
def main():
    merger = PdfFileMerger()
    if (len(sys.argv) > 1):
        for file in sys.argv[1:]:
            merger.append(open(file, "rb"))
    merger.write("result.pdf")
Ejemplo n.º 17
0
def generate_print_ready_pdf(side, name):
    file_names = sorted(["cache/jpg_output/{}/{}".format(side, a) for a in os.listdir('cache/jpg_output/'+side)])
    merger = PdfFileMerger()
    for f in file_names:
        merger.append(PdfFileReader(open(f, 'rb')))
    merger.write('output_pdfs/{}_{}.pdf'.format(name, side))
Ejemplo n.º 18
0
#!/anaconda3/bin/python

from PyPDF2 import PdfFileMerger
import glob
import sys

pdfs = glob.glob("*.pdf")
failed = []
mergetool = PdfFileMerger()

for pdf in pdfs:
    try:
        mergetool.append(pdf)
    except:
        pdfs.remove(pdf)
        print('Failed to merge: ' + pdf)
        failed.append(pdf)

# naming your merged pdf
if len(sys.argv) == 1:
    filename = 'mergedpdf.pdf'
else:
    filename = sys.argv[1] + ".pdf"

# failed files
if not failed:
    print('The follwing files didn\' print:')
    for failure in failed:
        print(failure)

# create file
Ejemplo n.º 19
0
def pdf_append(file1, file2):
    merger = PdfFileMerger()
    merger.append(PdfFileReader(open(file1, 'rb')))
    merger.append(PdfFileReader(open(file2, 'rb')))
    merger.write(file1)
Ejemplo n.º 20
0
from PyPDF2 import PdfFileReader, PdfFileMerger

f1 = PdfFileReader("name")
f2 = PdfFileReader("name")
out = PdfFileMerger()
out.append(f1)
out.append(f2)
out.write(name)
    def create_report(self):

        #Load html template that will be transformed into pdf
        env = Environment(loader=FileSystemLoader('.'))
        template = env.get_template("baseball_report_template.html")

        for handedness in ['R', 'L']:
            for pitch_speed in ['fast', 'offspeed']:

                ##Load up the tbl1 and tbl2 summary stats
                tbl1 = pd.read_csv('Batting_summary/summary_' + handedness +
                                   '_' + pitch_speed + '_tbl1.csv')
                tbl1['Value'] = tbl1['Value'].astype(str)
                tbl1.loc[6]['Value'] = tbl1.loc[6]['Value'][0:5]
                for idx in range(0, 6):
                    tbl1.loc[idx]['Value'] = tbl1.loc[idx]['Value'][:-2]
                tbl2 = pd.read_csv('Batting_summary/summary_' + handedness +
                                   '_' + pitch_speed + '_tbl2.csv')

                ##Populate the place holders in the html template
                template_vars = {
                    "title":
                    "Batter Approach Review",
                    "player_nm":
                    self.player,
                    "handedness":
                    handedness,
                    "pitch_type":
                    pitch_speed,
                    "tbl1":
                    tbl1.to_html(index=False),
                    "tbl2":
                    tbl2.to_html(index=False),
                    "swing_incidence":
                    'Batting_heatmaps/swings_' + handedness + '_' +
                    pitch_speed + '.png',
                    "exit_velo":
                    'Batting_heatmaps/batting_launch_speed_' + handedness +
                    '_' + pitch_speed + '.png',
                    "hit_incidence":
                    'Batting_heatmaps/batting_hit_' + handedness + '_' +
                    pitch_speed + '.png',
                    "out_incidence":
                    'Batting_heatmaps/batting_out_' + handedness + '_' +
                    pitch_speed + '.png'
                }

                html_out = template.render(template_vars)

                # to save the results
                with open("my_new_file.html", "w") as fh:
                    fh.write(html_out)

                pdfkit.from_file(
                    'my_new_file.html',
                    'report_' + handedness + '_' + pitch_speed + '.pdf')

        ##Combine each batting_situation pds into a single report
        pdfs = []
        pdfs.append('report_R_fast.pdf')
        pdfs.append('report_R_offspeed.pdf')
        pdfs.append('report_L_fast.pdf')
        pdfs.append('report_L_offspeed.pdf')

        merger = PdfFileMerger()

        ##Merge then remove intermediary files
        for pdf in pdfs:
            merger.append(open(pdf, 'rb'), import_bookmarks=False)
            os.remove(pdf)

        with open(
                self.player.split(' ')[0] + '_' + self.player.split(' ')[1] +
                "_batting_report.pdf", "wb") as fout:
            merger.write(fout)
        os.remove('my_new_file.html')
Ejemplo n.º 22
0
def main():

    # create an IMAP4 class with SSL
    imap = imaplib.IMAP4_SSL(json_data["server_in"])
    # authenticate
    imap.login(json_data["username"], json_data["password"])
    status, messages = imap.select("INBOX")
    # search for messages that has the word remittance in subject
    typ, msg_ids = imap.search(None, '(SUBJECT "Remittance Advice")')
    print(f'Status: {status} Typ: {typ} Messages: {msg_ids}')
    # msg_ids is byte -> convert to string and split the string by whitespace
    for msg_id in msg_ids[0].decode().split():
        # print(type(msg_id))
        typ, msg_data = imap.fetch(msg_id, '(BODY.PEEK[TEXT] FLAGS)')
        # print(msg_data)

        msg_str = str(msg_data[0][1]).lower()  # For better readibility
        print(msg_str)
        # read the html tables from the document with pandas:
        tables_html = pd.read_html(msg_str, skiprows=1)
        payment_n = tables_html[0][1][2]
        payment_d = tables_html[0][1][3]
        supplier_site = tables_html[0][1][1]
        print(f'#### Start {payment_n} ###')
        # convert format 01-Sep-2020 to 2020-09-01
        payment_d = str(
            datetime.datetime.strptime(payment_d,
                                       '%d-%b-%Y').strftime('%Y-%m-%d'))

        # check if file A-2020-09-01-remittance number exists
        if not os.path.exists(json_data["pwd"] + 'A-' + payment_d + '-' +
                              payment_n + '.pdf'):
            pdfkit.from_string(
                msg_data[0][1].decode(),
                json_data["pwd"] + 'A-' + payment_d + '-' + payment_n + '.pdf')
            print(
                f'[++] The pdf file for remittance {payment_n} has been created'
            )
        else:
            print(
                f'[++] The pdf file for remittance {payment_n} does already exists'
            )

        # print(payment_n)
        # print(payment_d)
        # print(type(payment_d))
        # Get the files of working dic

        # list for existing invoices
        list_success = ['A-' + payment_d + '-' + payment_n + '.pdf']

        # list for missing invoices
        list_missing = []

        file_list = os.listdir(json_data['pwd'])

        # hol die RGnr der 2. Tabelle und pack sie in die Liste and check if exists locall

        for inv_nr in tables_html[1][0]:
            i = 0
            for fn in file_list:
                if (str(inv_nr).upper() in fn):
                    list_success.append(fn)
                    try:
                        list_missing.remove(inv_nr)
                    except:
                        pass
                    i += i
                else:
                    if (i == 0):
                        list_missing.append(inv_nr)
                        i = i + 1
            # create list with needed invoices

        # check if missing invoices exists
        print(f'[--] MISSING {payment_n} {list_missing}')
        print(f'[++] SUCCESS {payment_n} {list_success}')
        if len(list_missing) > 0:
            new_message = email.message.Message()
            new_message["From"] = "*****@*****.**"
            new_message["Subject"] = json_data['email_subject'] + \
                payment_n + supplier_site
            new_message.set_payload(','.join(map(str, list_missing)))
            imap.append('INBOX', '', imaplib.Time2Internaldate(time.time()),
                        str(new_message).encode('utf-8'))

        else:
            if not os.path.exists(json_data["pwd"] + "A-AMZ-" + payment_d +
                                  "-" + payment_n + ".pdf"):
                # create invoice with pdftk and store it
                # Call the PdfFileMerger
                mergedObject = PdfFileMerger(strict=False)
                for list_item in list_success:
                    mergedObject.append(
                        PdfFileReader(json_data["pwd"] + list_item, 'rb'))

                # Write all the files into a file which is named as shown below
                mergedObject.write(json_data["pwd"] + "A-AMZ-" + payment_d +
                                   "-" + payment_n + ".pdf")

                # delete email

                imap.store(msg_id, '+FLAGS', '\\Deleted')
                imap.expunge()

                # delete files
                for del_item in list_success:
                    if os.path.exists(json_data["pwd"] + del_item):
                        os.remove(json_data["pwd"] + del_item)
        print(f'#### END {payment_n} ###\n')

    try:
        imap.close()
    except:
        pass
    imap.logout()

    return 0
Ejemplo n.º 23
0
def merge(conf):
    merger = PdfFileMerger()
    for file in conf:
        merger.append(PdfFileReader(file), 'rb')
    merger.write('CIS-COS.pdf')
    exit()
Ejemplo n.º 24
0
def unit_resources_pdf(request, slug, unit_slug):
    merger = PdfFileMerger()
    unit = get_object_or_404(Unit, curriculum__slug=slug, slug=unit_slug)
    for lesson in unit.lessons.exclude(keywords__keyword__slug="optional"):
        lesson_string = render_to_string("curricula/lesson_title.html", {
            'unit': unit,
            'lesson': lesson
        },
                                         request=request)
        lesson_page = pdfkit.from_string(
            lesson_string,
            False,
            options=settings.WKHTMLTOPDF_CMD_OPTIONS,
            configuration=pdfkit_config)
        lesson_page_pdf = StringIO(lesson_page)
        merger.append(PdfFileReader(lesson_page_pdf))
        for resource in lesson.resources.all():
            if resource.gd:
                try:
                    remotePDF = urlopen(Request(resource.gd_pdf())).read()
                    memoryPDF = StringIO(remotePDF)
                    localPDF = PdfFileReader(memoryPDF)
                    merger.append(localPDF)
                except Exception:

                    attachments = [
                        {
                            'color': 'danger',
                            'title': 'URL',
                            'text': resource.url,
                        },
                        {
                            'color': 'danger',
                            'title': 'Related Lesson',
                            'text': lesson.get_absolute_url(),
                        },
                    ]

                    ip = get_real_ip(request)

                    slack_message(
                        'slack/message.slack', {
                            'message':
                            "tried and failed to publish resource %s - %s (pk %s). "
                            "Check to ensure that it's a publicly accessible Google Doc"
                            % (resource.name, resource.type, resource.pk),
                            'user':
                            request.user.get_username() or ip,
                        }, attachments)
                    return HttpResponse('PDF Generation Failed', status=500)

    response = HttpResponse(content_type='application/pdf')
    merger.write(response)
    response[
        'Content-Disposition'] = 'inline;filename=unit%s_resources.pdf' % unit.number

    ip = get_real_ip(request)
    slack_message(
        'slack/message.slack', {
            'message': 'created a resource PDF from %s %s' % (slug, unit_slug),
            'user': request.user.get_username() or ip,
        })

    return response
Ejemplo n.º 25
0
import os, sys
import shutil
from PyPDF2 import PdfFileWriter, PdfFileReader, PdfFileMerger

main_dir = input("Dir: ")

temp_list = os.path.split(main_dir)
fileName = temp_list[len(temp_list) - 1]

pdfs = []

for file in os.listdir(main_dir):
    if os.path.isdir(file) or file.startswith("."):
        continue
    if file.endswith(".pdf"):
        pdfs.append(os.path.join(main_dir, file))

pdfs.sort()

merger = PdfFileMerger(strict=False)

for pdf in pdfs:
    print(pdf)
    merger.append(pdf)

merger.write(os.path.join(os.path.curdir, "%s (merged).pdf" % fileName))
print(os.path.curdir)
def add_pdf_did(path_batch, path_xml, path_pdf):
    """Adds metadata from Metadatadump XML to PDF Document Information Dict."""
    with open(path_xml, 'rb') as mddump_xml:
        dump = etree.parse(mddump_xml)
        object_id = os.path.basename(path_pdf).replace('_pdf.pdf',
                                                       '').replace('_', '')
        material = dump.xpath('/shipment/@material')[0]
        try:
            if material == 'tijdschriften' or material == 'kranten':
                referredRecordID = dump.xpath(f'//issue[@ID="{object_id}"]\
                                              /@referredRecordID')[0]
                sourceProvider = dump.xpath(f'//issue[@ID="{object_id}"]\
                                            /parent::entity/@sourceProvider'
                                            )[0]
                shelfmark = dump.xpath(f'//issue[@ID="{object_id}"]\
                                       /parent::entity/@shelfmark')[0]
                title = dump.xpath(f'//record[@ID="{referredRecordID}"]\
                                   /@title')[0]
                # issue_nr = dump.xpath(f'//issue[@ID="{object_id}"]/@issueNo')[0]
                volumeNo = dump.xpath(f'//issue[@ID="{object_id}"]\
                                      /@volumeNo')[0]

            if material == 'tijdschriften':
                sequenceNo = dump.xpath(f'//issue[@ID="{object_id}"]\
                                        /@sequenceNo')[0]
                volumeYear = dump.xpath(f'//issue[@ID="{object_id}"]\
                                        /@volumeYear')[0]

                if dump.xpath(f'//issue[@ID="{object_id}"]/@part'):
                    part = dump.xpath(f'//issue[@ID="{object_id}"]/@part')
                else:
                    part = ''
                publicationYear = dump.xpath(f'//issue[@ID="{object_id}"]\
                                             /@publicationYear')[0]

                if dump.xpath(f'//issue[@ID="{object_id}"]/@publicationMonth'):
                    publicationMonth = dump.xpath(f'//issue[@ID="{object_id}"]\
                                                  /@publicationMonth')[0]
                else:
                    publicationMonth = ''
                if dump.xpath(f'//issue[@ID="{object_id}"]/@publicationDay'):
                    publicationDay = dump.xpath(f'//issue[@ID="{object_id}"]\
                                                /@publicationDay')[0]
                else:
                    publicationDay = ''
                publicationType = dump.xpath(f'//issue[@ID="{object_id}"]\
                                             /@publicationType')[0]

            if material == 'kranten':
                publicationDate = dump.xpath(f'//issue[@ID="{object_id}"]\
                                             /@publicationDate')[0]
                edition = dump.xpath(f'//issue[@ID="{object_id}"]/@edition')[0]
                issue_nr = dump.xpath(f'//issue[@ID="{object_id}"]\
                                      /@issueNo')[0]

            if material == 'boeken':
                referredRecordID = dump.xpath(f'//entity[@ID="{object_id}"]\
                                              /@referredRecordID')[0]
                sourceProvider = dump.xpath(f'//entity[@ID="{object_id}"]\
                                            /@sourceProvider')[0]
                shelfmark = dump.xpath(f'//entity[@ID="{object_id}"]\
                                       /@shelfmark')[0]
                title = dump.xpath(f'//record[@ID="{referredRecordID}"]\
                                   /@title')[0]
                author = dump.xpath(f'//record[@ID="{referredRecordID}"]\
                                    /@author')[0]
                issued = dump.xpath(f'//record[@ID="{referredRecordID}"]\
                                    /@issued')[0]
                sequenceNo = dump.xpath(f'//entity[@ID="{object_id}"]\
                                        /@sequenceNo')[0]
                if dump.xpath(f'//entity[@ID="{object_id}]"/@part'):
                    part = dump.xpath(f'//entity[@ID="{object_id}]"/@part')[0]
                else:
                    part = ''
        except IndexError as e:
            print(e)

    # Material specific metadata.
    if material == 'tijdschriften':
        pdf_title = f'{title}, jrg. {volumeNo}, {volumeYear}, {part}, {publicationDay}, {publicationMonth}, {publicationYear}, [{publicationType}, volgnr. {sequenceNo}], {sourceProvider}, {shelfmark}'
        keywords = 'Gedigitaliseerd door de Koninklijke Bibliotheek; Nederlandse geschiedenis; tijdschriften, historische tijdschriften, oude tijdschriften, archief tijdschriften, tijdschrift online, cultuur, letterkunde, religie, wetenschap, politiek, sport, economie'
    elif material == 'kranten':
        pdf_title = f'{title}, jrg. {volumeNo}, {issueNo}, {publicationDate}, editie {edition}, {sourceProvider}, {shelfmark}'
        keywords = 'Gedigitaliseerd door de Koninklijke Bibliotheek; Nederlandse geschiedenis; kranten, historische kranten, oude kranten, archief kranten, krantenarchieven, krant online, familieberichten, stamboom familie, dagblad, overlijdensberichten, nieuwsberichten, Nederlandstalige kranten, namen familie, familie Nederland, oorlogskranten, kranten van toen, Surinaamse kranten, Indische kranten, Antilliaanse kranten, databank kranten'
    elif material == 'boeken':
        pdf_title = f'{title}, {part}, {sequenceNo}, {author}, {issued}, {sourceProvider}, {shelfmark}'
        keywords = 'Gedigitaliseerd door de Koninklijke Bibliotheek; Nederlandse geschiedenis; boeken, oude drukken, Nederlands taalgebied, geschiedenis, politiek, theologie, letterkundige werken, naamlijsten, boeken online, historische teksten, oude boeken, bijzondere collecties, cultuur'

    # Add metadata as Document Information to PDF.
    now = datetime.now()
    pdf_datestamp = now.strftime("D:%Y%m%d%H%M%S+01'00'")

    merger = PdfFileMerger()
    merger.append(path_pdf)

    merger.addMetadata({
        '/Title':
        pdf_title,
        '/Keywords':
        keywords,
        '/CreationDate':
        pdf_datestamp,
        '/ModDate':
        pdf_datestamp,
        '/Copyright-information':
        'Gedigitaliseerd door de Koninklijke Bibliotheek, de Nationale Bibliotheek van Nederland  / Digitised by  the Koninklijke Bibliotheek, the National Library of the Netherlands'
    })
    if material == 'boeken':
        merger.addNetadata({'/Author': author})
    # Save final PDF in output_dir.
    merger.write(os.path.join(output_dir, os.path.basename(path_pdf)))
    merger.close()
Ejemplo n.º 27
0
    plt.text(0.5, 0.95, 'Time: '+tstring, fontsize=15,
            horizontalalignment='center', transform=plt.gcf().transFigure)
    if show:
        plt.show()
    if save:
        plt.savefig(spath+'07_hozt_vgradrho_rho_diff_%s.pdf' % tstring)
    return

if __name__=='__main__':
    from PyPDF2 import PdfFileMerger, PdfFileReader
    whichlt=7
    trange = pd.date_range(
            '2003-03-22 00:00:00', '2003-03-22 04:00:00', freq='10min')
    spath = '/home/guod/Documents/work/fig/density_cell/'\
            'why_no_low_density_cell_at_high_latitude/lat_alt/'
    outpdf_vert_wind = PdfFileMerger()
    outpdf_rho_diff = PdfFileMerger()
    outpdf_divrhov_rho_diff = PdfFileMerger()
    outpdf_vert_divv_diff = PdfFileMerger()
    outpdf_hozt_divv_diff = PdfFileMerger()
    outpdf_vert_vgradrho_diff = PdfFileMerger()
    outpdf_hozt_vgradrho_diff = PdfFileMerger()
    for t in trange:
        tstring = t.strftime('%H%M')
        filename = glob.glob(
                '/media/guod/wd2t/simulation_output/momentum_analysis/run_shrink_iondrift_4_c1'\
                '/data/3DALL_t030322_%s*.bin' % tstring)[0]
        g1a = gitm.GitmBin(filename)
        filename = glob.glob(
                '/media/guod/wd2t/simulation_output/momentum_analysis/run_no_shrink_iondrift_4_1'\
                '/data/3DALL_t030322_%s*.bin' % tstring)[0]
Ejemplo n.º 28
0
def generate_ops():
    print("hit")
    print(dir(request))
    # print(request.data)
    form = json.loads(request.data)

    data = form["OPSData"]

    # with open("./test.txt","w") as f:
    #     for key in data:
    #         f.write(str(key) + "\n")

    output_objects = []
    template = PdfJinja("./fillable_ops.pdf")

    for key in data:

        #set DSUID to "

        pdf_data = generate_pdf_data(data[key])

        if key == "Summary":
            pdf_data["DSUID"] = ""

        deal_source = pdf_data.get("DealSource")
        seller = pdf_data.get("Seller")
        status = pdf_data.get("Status")
        date = pdf_data.get("Date")

        if deal_source == None:
            pdf_data["DealSource"] = "No Data Provided"
        if seller == None:
            pdf_data["Seller"] = "No Data Provided"
        if date == None:
            pdf_data["Date"] = "N/A"
        if status == None:
            pdf_data["Status"] = "No Data Provided"

        #if DealSource not in pdf_data, then add it with "No Data Provided" as the value

        output_objects.append(pdf_data)
        rendered = template(pdf_data)
        pdf_name = "output" + key + ".pdf"
        output_file = "./output/" + pdf_name

        # append output_file to a list
        rendered.write(open(output_file, "wb"))

    # use pypdftk.concat() on the resulting list
    output_objects = []
    for filename in os.listdir("./output"):
        path_name = "./output/" + filename
        output_objects.append(path_name)

    output_objects.sort(reverse=True)\

    output = PdfFileMerger()

    for pdf in output_objects:
        pdf_file = PdfFileReader(pdf)
        output.append(pdf_file)

    # pdf_file1 = PdfFileReader("./output/outputSummary.pdf")
    # pdf_file2 = PdfFileReader("./output/output151097_29303132-1.pdf")
    # pdf_file3 = PdfFileReader("./output/output151097_17181920-1.pdf")
    # output.append(pdf_file1)
    # output.append(pdf_file2)
    # output.append(pdf_file3)
    output.write("outputMerged.pdf")

    # generated_pdf = pypdftk.fill_form('./output/', pdf_data)
    # output_file = pypdftk.concat(['./outputSummary/', generated_pdf])

    # given_value = OPSData.value

    # form = request.form
    # data = json.loads(form["OPSData"])
    # pdf_data = generate_pdf_data(data['151097_2635-1'])

    # # print(template)

    with open("./output.txt", "w") as f:
        json.dump(output_objects, f, indent=2)

    return flask.jsonify({"message": "hi"})
Ejemplo n.º 29
0
Popen(cmd, shell=True, stdout=PIPE).communicate()  #block until finish
#Popen("pandoc %s.tex -o %s.pdf"%(merged_filename_base, merged_filename_base))

#######################################################
print('\n======generate cover page and readme #####################')
#disable page number in cover page
Popen([
    'pandoc', input_folder + "coverpage.docx", "-o",
    build_folder + "coverpage.pdf"
]).communicate()
Popen(['pandoc', root_folder + "Readme.md", "-o",
       build_folder + "Readme.pdf"]).communicate()

from PyPDF2 import PdfFileReader, PdfFileMerger

pdf_files = [
    build_folder + "coverpage.pdf", build_folder + "Readme.pdf",
    merged_filename_base + ".pdf"
]
#for f in pdf_files: print(f)
merger = PdfFileMerger()

for filename in pdf_files:
    merger.append(PdfFileReader(filename, "rb"))
final_output_filename = "../pdf/" + "FreeCAD_Mod_Dev_Guide" + ts + ".pdf"
if os.path.exists(final_output_filename):
    os.remove(final_output_filename)
merger.write(final_output_filename)
print("final merged file: ", final_output_filename)
print('====== merge done!================')
Ejemplo n.º 30
0
    def build_attachments(self, attachments, merge=False):
        """build attachments

        :param attachments: list of dict(name: attachment name
                                         data: base64 encoded content)
        :type attachments: list
        :param merge: merge all attachments in one file
        :returns: list of dict(name: attachment name
                               type: attachment mime type
                               size: attachment size in bits
                               data: base64 encoded content)
        :rtype: list
        """

        build = []
        if merge and len(attachments) > 1:
            # if merging all the attachments must be pdf's
            attach_types = [
                mimetypes.guess_type(x['name'])[0] for x in attachments
            ]
            if not (len(set(attach_types)) == 1
                    and attach_types[0] == 'application/pdf'):
                exception_message = 'Merge only allowed if all files are pdfs'
                self.exception(exception_message)
            merger = PdfFileMerger()
            merge_file = TemporaryFile()
            merge_name = attachments[0]['name']
            for attachment in attachments:
                #Write attachment data to a temporary file
                temp_file = TemporaryFile()
                temp_file.write(base64.b64decode(attachment['data']))
                temp_file.seek(0)
                merger.append(temp_file)
            merger.write(merge_file)
            merge_file.seek(0)
            merge_data = base64.b64encode(merge_file.read())
            attachments = [{'name': merge_name, 'data': merge_data}]
            merger.close()
            merge_file.close()

        for attachment in attachments:
            attach_name = attachment['name']
            attach_data = attachment['data']
            # Prepare attachment data
            # Guess the mime type using the extension of the file
            attach_type = mimetypes.guess_type(attach_name)
            if attach_type[0] is not None:
                attach_type = attach_type[0]
            else:
                exception_message = 'Cannot guess mime type for %s' % attach_name
                self.exception(exception_message)
            # Attach size will be estimated
            # from attach_data (base64 encoded)
            attach_size = int(len(attach_data) * 0.75)
            attach_dict = {
                'name': attach_name,
                'type': attach_type,
                'size': attach_size,
                'data': attach_data
            }
            build.append(attach_dict)
        return build