def multiple_contexts_and_templates_to_pdf_data(contexts_templates, context_instance=None, filename=None): context_instance = context_instance or Context() if USE_PYPDF2: merger = PdfFileMerger() else: all_parts = [] old_lang = translation.get_language() for context, template_name in contexts_templates: parser = get_parser(template_name) if 'language' in context: translation.activate(context['language']) input = render_to_string(template_name, context, context_instance) if USE_PYPDF2: outstream = StringIO.StringIO() outstream.write(parser.parse(input)) reader = PdfFileReader(outstream) merger.append(reader) else: parts = parser.parse_parts(input) all_parts += parts all_parts.append(PageBreak()) translation.activate(old_lang) if USE_PYPDF2: output = StringIO.StringIO() merger.write(output) output = output.getvalue() else: output = parser.merge_parts(all_parts) return output
def merge_pdf(self, cr, uid, ids, context): print context["active_ids"] stock_pick_obj = self.pool.get("stock.picking.out") attach_object = self.pool.get("ir.attachment") count = 0 merge_file = "/var/www/merge.pdf" if os.path.exists(merge_file): os.remove(merge_file) merger = PdfFileMerger() for ative_id in context["active_ids"]: print ative_id count += 1 stock_data = stock_pick_obj.browse(cr, uid, ative_id) attach_id = attach_object.search( cr, uid, [("res_id", "=", ative_id), ("res_model", "=", context["active_model"])] ) attachment_data = attach_object.browse(cr, uid, attach_id) # print attachment_data[0].res_id child_pdf_file = "/var/www/master_" + stock_data.carrier_tracking_ref + ".pdf" merger.append(PdfFileReader(file(child_pdf_file, "rb"))) merger.write(merge_file) url = "http://localhost/merge.pdf" return {"type": "ir.actions.act_url", "url": url, "target": "new"}
def rmerge(): """merge pdf files """ os.chdir("..") proj_root = os.getcwd() pathcalc = os.path.join(proj_root, "dbcalc") pathreprt = os.path.join(proj_root, "reprt") os.chdir(pathreprt) with open("reportmerge.txt",'r') as file0: calc1 = file0.readlines() calc2 = ["cover.pdf", "reporttoc.pdf"] for itm1 in calc1: if itm1.split('|')[0].strip() == 'c': calc2.append(itm1.split('|')[1].strip()) #print('clist', calc2) os.chdir(pathcalc) print('3', os.getcwd()) list1 = glob.glob('*.pdf') print('list1', list1) for itm1 in list1: print("item",itm1) shutil.copy(itm1, pathreprt) os.chdir(pathreprt) merger = PdfFileMerger() for filename in calc2: merger.append(PdfFileReader(open(filename, 'rb'))) merger.write("calcreport.pdf")
def merge_book_parts(self, part_filepaths, output_filepath): pdf_merger = PdfFileMerger() for part_filepath in part_filepaths: pdf_merger.append(PdfFileReader(open(part_filepath, 'rb'))) pdf_merger.write(output_filepath)
def run_mupla(dir_path): pdfs = [] filen = 0 while(True): pdf_path = dir_path+"/"+str(filen)+".pdf" if os.path.isfile(pdf_path): pdfs.append(pdf_path) filen += 1 else: break merged_pdf_path = dir_path+"/merged.pdf" if len(pdfs) == 0: raise Exception("No PDF file available") elif len(pdfs) == 1: shutil.copyfile(pdfs[0], merged_pdf_path) else: merger = PdfFileMerger() for pdf in pdfs: with open(pdf, "rb") as f: merger.append(PdfFileReader(f)) f.close() with open(merged_pdf_path, "wb") as output: merger.write(output) output.close() js = mupla_cython.PyMuPlaRun(merged_pdf_path) if len(js) == 0: raise Exception("Invalid PDF file") with open(dir_path+"/merged.js", 'w') as f: f.write(json.dumps(js)) f.close()
def handle(self, *args, **kwargs): data = input('Conference short name and year (e.g. ICGT 2015): ').split() try: short_name = " ".join(data[:-1]) year = int(data[-1]) instance = ConferenceInstance.objects.get( start_date__year=year, conference__short_name=short_name) except Exception as error: print(error) print('ERROR: Conference not found.') exit() merger = PdfFileMerger() for paper in instance.papers.order_by('proceedings_relative_order').all(): print('adding %s' % paper.pdf_path()) try: merger.append(PdfFileReader(open(paper.pdf_path(), 'rb'))) except Exception as err: print('ERROR: %s' % err) print('\nAll ok, now let\'s save it...') output_path = input('\nInsert the output path [./proceedings.pdf]:') if not output_path: output_path = _DEFAULT_OUTPUT_PATH merger.write(output_path) print('\nPDF file written to %s' % output_path)
def generate_pdf(course_info, students): ''' Arguments: course_info: Dictionary with the following keys instructor_name,instructor_id,training_center_name, training_center_id, training_center_address,course_location,course_date,card_issue_date, card_expire_date,written_test,child_cpr,infant_cpr students: List of dictionarys with the following keys student_name,student_address_1,student_address_2 ''' course_info_page = generate_course_info_page_pdf(course_info, len(students)) ci_packet = StringIO.StringIO() course_info_page.write(ci_packet) ci_packet.seek(0) merged_roster = PdfFileMerger() merged_roster.append(PdfFileReader(ci_packet)) #TODO: create student pages for studentSubGroup in grouper(students, 10): next_roster_page = generate_student_page_pdf(studentSubGroup, course_info['instructor_name'], course_info['course_date']) nrp_packet = StringIO.StringIO() next_roster_page.write(nrp_packet) nrp_packet.seek(0) merged_roster.append(PdfFileReader(nrp_packet)) return merged_roster
def merge_in_pdf(fa, fb): for f in [fa, fb]: if _is_img(f.name): img = Image.open(f.file) try: r, g, b, a = img.split() #alpha except Exception as e: r, g, b = img.split() img = Image.merge('RGB', (r, g, b)) temp_file = TemporaryFile() img.save(temp_file, "PDF", resolution=100, transparency=0) temp_file.seek(0) f.file = temp_file merger = PdfFileMerger() for f in [fa, fb]: merger.append(PdfFileReader(f.file)) temp_file = TemporaryFile() merger.write(temp_file) temp_file.seek(0) pdf_file = File(temp_file) pdf_file.name = 'id_card.pdf' return pdf_file
def generate_pdf(request, site): reportPdfUrl = 'http://%s/report/view/%s' % (request.META['HTTP_HOST'],str(site.pk)) outputStream = BytesIO() reportPdfFile = '%s/download_%s.pdf' % (settings.DOWNLOAD_ROOT, site.pk) wkhtmltopdfBinLocationString = '/usr/local/bin/wkhtmltopdf' wkhtmltopdfBinLocationBytes = wkhtmltopdfBinLocationString.encode('utf-8') config = pdfkit.configuration(wkhtmltopdf=wkhtmltopdfBinLocationBytes) pdfkit.from_url(reportPdfUrl, reportPdfFile, configuration=config, options={ 'javascript-delay': 1500, 'load-error-handling': 'ignore' }) new_pdf = PdfFileMerger() new_pdf.append('%s/download_%s.pdf' % (settings.DOWNLOAD_ROOT, site.pk)) # finally, return output new_pdf.write(outputStream) final = outputStream.getvalue() outputStream.close() os.remove(reportPdfFile) return final
def merge_pdf(list_of_file_paths, destination_directory, file_name, delete_source=False): merge = PdfFileMerger() operation_result = {} invalid_file_paths = [] for files in list_of_file_paths: if os.path.exists(files): continue else: invalid_file_paths.append(files) list_of_file_paths.remove(files) for files in list_of_file_paths: merge.append(PdfFileReader(files, True)) merged_pdf_filename = file_name + ".pdf" merge.write(os.path.join(destination_directory, merged_pdf_filename)) if delete_source is True: for files in list_of_file_paths: os.remove(files) if not invalid_file_paths: operation_result.update({"code": 0, "invalid_file_paths": "None"}) else: invalid_files = ",".join(list_of_file_paths) operation_result.update({"code": 1, "invalid_file_paths": invalid_files}) return operation_result
def make_pdf(self, fileName, content, pformat, merge_all=True, pages=None): if fileName: fileName = fileName[fileName.rfind(os.sep) + 1:] output = cStringIO.StringIO() file_name = fileName.replace(" ", "-").replace("/", "-") if pformat == "pdf" and self.report_origin == "local": output.write(content[0]) return file_name, output if pformat=="pdf" and merge_all == True: merger = PdfFileMerger() for n in range(len(content)): merger.append(BytesIO(content[n])) merger.write(output) elif pformat=="pdf": merger = PdfFileMerger() for page in pages: merger.append(BytesIO(content[page])) merger.write(output) else: fname = file_name.split(".") myzip = zipfile.ZipFile(output, 'w', compression=zipfile.ZIP_DEFLATED) try: pgs = range(len(content)) if pages is None else pages for n in pgs: myzip.writestr(fname[0] + "_" + str(n) + "." + fname[1], content[n]) finally: myzip.close() file_name = fname[0] + ".zip" return file_name, output else: frappe.msgprint(_("There is no report."))
def print_to_pdf(self, path): """ Custom print based on metadata: generate one per slide """ merger = PdfFileMerger() for i, slide in enumerate(self.notebook.metadata.nbpresent.slides): print("\n\n\nprinting slide", i, slide) # science help you if you have 9999 slides filename = self.in_static("notebook-{0:04d}.pdf".format(i)) # this is weird, but it seems to always need it self.session.show() self.screenshot(filename) merger.append(PdfFileReader(filename, "rb")) # advance the slides result, resources = self.session.evaluate( """ console.log(window.nbpresent); console.log(window.nbpresent.mode.presenter.speaker.advance()); """) # always seem to get some weirdness... perhaps could inttegrate # ioloop... time.sleep(1) # all done! merger.write(path)
def main(Crnt, arg=Arg, num_intrp_l = [1, 2, 4, 8]): dirname_l = [] for num in num_intrp_l: dirname = simulat(num, arg, Crnt) dirname_l.append(dirname) dirname_allplots = os.path.join("plots", "testinterpolation_C="+str(Crnt)+"_num_interp="+str(num_intrp_l)) if os.path.exists(dirname_allplots): call(["rm", "-r", dirname_allplots]) call(["mkdir", dirname_allplots]) # joining files from all directories for every timestep for it in arg["it_output_l"]: time = str((it+1)*arg["dt"]) filename_l = [] for dir in dirname_l: filename_l.append(os.path.join(dir, "profiles_time="+time+"s.pdf")) merger = PdfFileMerger() for filename in filename_l: merger.append(PdfFileReader(filename, "rb")) merger.write(os.path.join(dirname_allplots, "all_plots_time="+time+".pdf")) plotting_comparison(arg, dirname_l, Crnt, num_intrp_l)
def main(rootdir, outdir): print rootdir print '\n' print outdir outputReport = [] for subdir, dirs, files in os.walk(rootdir): merger = PdfFileMerger() if subdir[-4:] == "0000": outFileName = subdir[-14:] output = join(outdir, outFileName + '_PhD_WellFile.pdf') if not os.path.exists(output): print "Folder: " + outFileName fileArray = [] for f in files: fileArray.append(join(subdir,f)) sort_nicely(fileArray) for f in fileArray: try: print "Merging: " + join(subdir, f) merger.append(open(join(subdir, f), "rb")) except: print "Couldn't add file: " + f print "Writing Output file: " + output try: merger.write(output) except: outputReport.append("Couldn't create file: " + subdir[-14:]) for i in outputReport: print i
def main(): root = normpath(argv[1]) pdfs_merged = argv[2] merger = PdfFileMerger() for path, f in visit(root, '.pdf'): merger.append(file((join(path, f)), 'rb')) merger.write(pdfs_merged)
def multiprocesPdfs(pdfList,output): """This function takes two arguments. The first is a directory containing a list of absolute pdf file locations. The second is the output file name. This should also be specified in absolute terms. This function will, using all avliable CPUs get the pdfs and join them into a single large pdf at the output location.""" #We set the workers to the cpu core count or the pdf file count, whatever #is lower. That way we are never wasting reasources. if multiprocessing.cpu_count() > len(pdfList): workers = len(pdfList) else: workers = multiprocessing.cpu_count() #Create the pool and process the data pool = Pool(processes=workers) pdfBinaryList = pool.map(_worker, pdfList) #Create the PDF File Merger Instance masterPdf = PdfFileMerger() #Warn if files are not processed, else add them to master pdf and write #the file out. for pdfBinary in pdfBinaryList: if pdfBinary['status'] != True: print('Could not process %s'% pdfBinary['fileName']) else: masterPdf.append(pdfBinary['byteCode']) masterPdf.write(output)
def merge_PDF(self, tmp_list): merger = PdfFileMerger() for filename in tmp_list: merger.append(PdfFileReader(file(os.path.join(os.getcwd(), '_tmp', filename), 'rb'))) merger.write("presentation.pdf") os.system('rm -rf _tmp')
def merge(dest, srcDir): '''merge pdf files in srcDir''' if os.path.exists(srcDir) and os.path.isdir(srcDir): files = os.listdir(srcDir) if len(files) > 0: merger = PdfFileMerger() for i,f in enumerate(files): Log.i(DuoPdf.__name__, 'merge file [%s]' % (f, )) filePath = os.path.join(srcDir, f) if (os.path.isfile(filePath)): try: srcFileHdl = open(filePath, 'rb') merger.merge(position=i, fileobj=srcFileHdl) except: Log.w(DuoPdf.__name__, 'merge [%s] failed' % (filePath)) traceback.print_exc() else: Log.i(DuoPdf.__name__, 'skip file [%s]' % (filePath,)) Log.i(DuoPdf.__name__, 'save to file [%s]...' % (dest, )) destFileStream = file(dest, 'wb') merger.write(destFileStream) destFileStream.close() Log.i(DuoPdf.__name__, 'done') else: Log.w(DuoPdf.__name__, 'no file in [%s] to merge' % (srcDir)) else: Log.w(DuoPdf.__name__, 'dir [%s] not exist.' % (srcDir,))
def PDFMerger(*filenames): merger = PdfFileMerger() for i in range(len(filenames)): inputfile = open(filenames[i],"rb") merger.append(inputfile) output = open(".\\output.pdf","wb") merger.write(output) print "Merge Success"
def merge(file_names, new_file_name): '''Merges the list of `file_names` into one file called `new_file_name`.''' merger = PdfFileMerger() for file_name in file_names: merger.append(PdfFileReader(file_name, 'rb')) merger.write(new_file_name)
def main(): abs_path = os.environ['abs_path'] query = os.environ['query'] page_count = int(query) class AlfredPdfSuiteError(Exception): pass class NegativeValueError(AlfredPdfSuiteError): pass try: if not abs_path: raise NoFileError('You must select a PDF file.') if not abs_path.endswith('.pdf'): raise NotPdfError('The selected object is not a PDF file.') if page_count < 0: raise NegativeValueError('Negative integer is not a valid argument.') start = 0 stop = page_count inp_file = PdfFileReader(open(abs_path, 'rb')) num_pages = int(inp_file.getNumPages()) quotient = num_pages / page_count if quotient.is_integer(): for i in xrange(int(quotient)): merger = PdfFileMerger() merger.append(inp_file, pages=(start, stop)) no_ext_path = os.path.splitext(abs_path)[0] merger.write(no_ext_path + (' (part {}).pdf').format(i+1)) start = stop stop = start + page_count else: for i in xrange(int(quotient) + 1): merger = PdfFileMerger() merger.append(inp_file, pages=(start, stop)) no_ext_path = os.path.splitext(abs_path)[0] merger.write(no_ext_path + (' (part {}).pdf').format(i+1)) if i != int(quotient) - 1: start = stop stop = start + page_count else: start = int(quotient) * page_count stop = num_pages except NegativeValueError as err: print err except ZeroDivisionError: print 'Zero is not a valid argument. Enter a positive integer instead.'
def main (desti): merger = PdfFileMerger() dirs = sys.argv files = [x for x in os.listdir(dirs[1]) if x.endswith(".pdf")] for fname in sorted(files): merger.append(PdfFileReader(open (os.path.join(dirs[1], fname), 'rb'))) merger.write(os.path.join(dirs[1],desti)) print ("All content from directory %s has been merged into %s" % (dirs[1], desti)) return "OK"
def merge_pdfs(f_names): merger = PdfFileMerger() fps = [open(f,'rb') for f in f_names] [merger.append(f) for f in fps] out_file = tempfile.mktemp("-merge.pdf") with open(out_file,'wb') as f: merger.write(f) [f.close() for f in fps] print("Merged output is in '{}'.".format(out_file))
def mergePages(pages, filename): merger = PdfFileMerger() for p in pages: f = open(p, "rb") merger.append(f) with open(filename, "wb") as outfile: merger.write(outfile) print "Saved binder: %s" % filename
def merge_pdfs(source_file_pathes, merge_file_path): """Merge a list of source files into a new file using uno and libreoffice """ merger = PdfFileMerger() for source_file_path in source_file_pathes: merger.append(PdfFileReader(open(source_file_path, 'rb'))) merger.write(str(merge_file_path))
def main(args): parser = argparse.ArgumentParser( description='Amend one page of a pdf document.') parser.add_argument('orig_pdf', type=str) parser.add_argument('page_num_to_amend', type=int) parser.add_argument('-a', '--amend-with', dest='amend_with') parser.add_argument('-o', '--output') args = parser.parse_args(args) page = args.page_num_to_amend pidx = page - 1 main, ext = os.path.splitext(args.orig_pdf) new_pdf = args.output or "%s.amended%s" % (main, ext) amend_with = args.amend_with or "%s.page-%s.amended%s" % (main, page, ext) if os.path.exists(amend_with): print("using existing amended page %s" % amend_with) else: print("%s does not exist; I will now open GIMP so you can edit page #%s in that file." % (amend_with, page)) print("After you have made your edits, do File -> Overwrite %s, then exit GIMP." % amend_with) print("(It's safe to 'Discard Changes' after Overwrite.)") input("Press enter to continue and run GIMP ") amend_with_png = "%s.page-%s.amended.png" % (main, page) C(("convert -density %s -units pixelsperinch" % DENSITY_DPI).split() + ['%s[%s]' % (args.orig_pdf, pidx), amend_with_png]) C(["gimp", amend_with_png]) C(["convert", amend_with_png, amend_with]) with open(args.orig_pdf, 'rb') as orig, open(amend_with, 'rb') as new: pdf = PdfFileMerger() pdf.merge(0, orig, pages = PageRange("0:%s" % pidx)) pdf.merge(pidx, new) pdf.merge(pidx+1, orig, pages = PageRange("%s:" % (pidx+1))) pdf.write(new_pdf) print("wrote %s" % new_pdf)
def main(): parser = OptionParser(usage="usage: %prog [options] PDF-file") parser.add_option("-o", "--out", dest="pdfOut", help="Name of PDF File to create") parser.add_option("-b", "--begin", dest="begin", action="store_true", default=False, help="Insert blank page at beginning of file, instead of end") parser.add_option("-d", "--debug", action="store_true", dest="debug", default=False, help="Print debug messages") (options, args) = parser.parse_args() if len(args) != 1: parser.error("1 and only 1 PDF File, please.") pdfIn = args[0] if options.pdfOut: pdfOut = options.pdfOut else: pdfOut = re.sub('\.[pP][dD][fF]$', '', pdfIn) pdfOut = pdfOut + '--blankified.pdf' originalPDF = open(pdfIn, 'rb') pdfReader = PdfFileReader(originalPDF) pageSize = pdfReader.getPage(0).mediaBox.upperRight tempPDF = tempfile.TemporaryFile() writer = PdfFileWriter() writer.addBlankPage(pageSize[0], pageSize[1]) writer.write(tempPDF) merger = PdfFileMerger() merger.append(originalPDF) if options.begin: merger.merge(position=0, fileobj=tempPDF) else: merger.append(tempPDF) merger.write(open(pdfOut, 'wb'))
def cat(args): """ %prog cat *.pdf -o output.pdf Concatenate pages from pdf files into a single pdf file. Page ranges refer to the previously-named file. A file not followed by a page range means all the pages of the file. PAGE RANGES are like Python slices. {page_range_help} EXAMPLES pdfcat -o output.pdf head.pdf content.pdf :6 7: tail.pdf -1 Concatenate all of head.pdf, all but page seven of content.pdf, and the last page of tail.pdf, producing output.pdf. pdfcat chapter*.pdf >book.pdf You can specify the output file by redirection. pdfcat chapter?.pdf chapter10.pdf >book.pdf In case you don't want chapter 10 before chapter 2. """ p = OptionParser(cat.__doc__.format(page_range_help=PAGE_RANGE_HELP)) p.set_outfile() p.set_verbose(help="Show page ranges as they are being read") opts, args = p.parse_args(args) if len(args) < 1: sys.exit(not p.print_help()) outfile = opts.outfile if outfile in args: args.remove(outfile) args = natsorted(args) filename_page_ranges = parse_filename_page_ranges(args) verbose = opts.verbose fw = must_open(outfile, "wb") merger = PdfFileMerger() in_fs = {} try: for (filename, page_range) in filename_page_ranges: if verbose: print >> sys.stderr, filename, page_range if filename not in in_fs: in_fs[filename] = open(filename, "rb") merger.append(in_fs[filename], pages=page_range) except: print >> sys.stderr, traceback.format_exc() print >> sys.stderr, "Error while reading " + filename sys.exit(1) merger.write(fw) fw.close()
def merge_pdfs(_path): ''' Merge pdf files from tmp folder and write into destination one ''' print "Creating release pdf..." file_list = glob.glob(_path) merger = PdfFileMerger() for filename in file_list: merger.append(PdfFileReader(file(filename, 'rb'))) merger.write(final_file)
def mergePdfIfNewer(singlePdfs, combined): from PyPDF2 import PdfFileMerger, PdfFileReader singles_time = np.array([]) for fi in singlePdfs: singles_time = np.append(singles_time, [getTime(fi)]) combined_time = getTime(combined) if np.any(singles_time>combined_time): allcombined = PdfFileMerger() for fl in singlePdfs: allcombined.append(PdfFileReader(fl,"rb")) allcombined.write(combined)
def merge(self, pdfs, merged_name): merger = PdfFileMerger() for pdf in pdfs: merger.append(PdfFileReader(pdf)) merger.write(f"{merged_name}.pdf")
def cat(args): """ %prog cat *.pdf -o output.pdf Concatenate pages from pdf files into a single pdf file. Page ranges refer to the previously-named file. A file not followed by a page range means all the pages of the file. PAGE RANGES are like Python slices. {page_range_help} EXAMPLES pdfcat -o output.pdf head.pdf content.pdf :6 7: tail.pdf -1 Concatenate all of head.pdf, all but page seven of content.pdf, and the last page of tail.pdf, producing output.pdf. pdfcat chapter*.pdf >book.pdf You can specify the output file by redirection. pdfcat chapter?.pdf chapter10.pdf >book.pdf In case you don't want chapter 10 before chapter 2. """ p = OptionParser(cat.__doc__.format(page_range_help=PAGE_RANGE_HELP)) sp1.add_argument("--nosort", default=False, action="store_true", help="Do not sort file names") sp1.add_argument("--cleanup", default=False, action="store_true", help="Remove individual pdfs after merging") p.set_outfile() p.set_verbose(help="Show page ranges as they are being read") opts, args = p.parse_args(args) if len(args) < 1: sys.exit(not p.print_help()) outfile = args.outfile if outfile in args: args.remove(outfile) if not args.nosort: args = natsorted(args) filename_page_ranges = parse_filename_page_ranges(args) verbose = args.verbose fw = must_open(outfile, "wb") merger = PdfFileMerger() in_fs = {} try: for (filename, page_range) in filename_page_ranges: if verbose: print >> sys.stderr, filename, page_range if filename not in in_fs: in_fs[filename] = open(filename, "rb") merger.append(in_fs[filename], pages=page_range) except: print >> sys.stderr, traceback.format_exc() print >> sys.stderr, "Error while reading " + filename sys.exit(1) merger.write(fw) fw.close() if args.cleanup: logging.debug("Cleaning up {} files".format(len(args))) for arg in args: os.remove(arg)
def make_pdf(self, fileName, content, pformat, merge_all=True, pages=None): if fileName: fileName = fileName[fileName.rfind(os.sep) + 1:] output = cStringIO.StringIO() file_name = fileName.replace(" ", "-").replace("/", "-") if pformat == "pdf" and self.report_origin == "local": output.write(content[0]) return file_name, output if pformat == "pdf" and merge_all == True: merger = PdfFileMerger() for n in range(len(content)): merger.append(BytesIO(content[n])) merger.write(output) elif pformat == "pdf": merger = PdfFileMerger() for page in pages: merger.append(BytesIO(content[page])) merger.write(output) else: fname = file_name.split(".") myzip = zipfile.ZipFile(output, 'w', compression=zipfile.ZIP_DEFLATED) try: pgs = range(len(content)) if pages is None else pages for n in pgs: myzip.writestr( fname[0] + "_" + str(n) + "." + fname[1], content[n]) finally: myzip.close() file_name = fname[0] + ".zip" return file_name, output else: frappe.msgprint(_("There is no report."))
from PyPDF2 import PdfFileReader, PdfFileMerger import os, glob FINAL_DEST_FOLDER = 'C:\\Users\\Ravi Shankar\\Documents\\Upwork\\law firm\\initial_dest\\' pdf_files = [f for f in os.listdir(FINAL_DEST_FOLDER) if f.endswith("PDF")] merger = PdfFileMerger() ##for filename in pdf_files: ## print filename ## merger.append(PdfFileReader(os.path.join(FINAL_DEST_FOLDER, filename), "rb")) ## ##merger.write("merged_full.pdf") import re numbers = re.compile(r'(\d+)') def numericalSort(value): parts = numbers.split(value) parts[1::2] = map(int, parts[1::2]) return parts os.chdir(FINAL_DEST_FOLDER) for infile in sorted(glob.glob('*.PDF'), key=numericalSort): print "Current File Being Processed is: " + infile merger.append(PdfFileReader(os.path.join(FINAL_DEST_FOLDER, infile), "rb")) merger.write("merged_full.pdf")
from PyPDF2 import PdfFileMerger import os import re usrInput = '' pdfs = [] while usrInput != '*': usrInput = input('Enter the filename in the local directory or enter * to finish: ') if usrInput != '*': pdfs.append(usrInput) merger = PdfFileMerger() for pdf in pdfs: merger.append(pdf) merger.write("output.pdf") merger.close() def list_pdfs(): ''' This function should return a list all PDFs in the current directory. ''' dircontents = os.listdir(os.getcwd()) pdflist = [] for i in dircontents: if i[-4:] == ".pdf": pdflist.append(i) return pdflist
from PyPDF2 import PdfFileMerger print("\nMake sure you have copied two pdfs in the same FOLDER as this script :) \n") input("\nPress ENTER to begin the SCRIPT !!!\n") firstPdf = input("\nEnter the EXACT NAME of your first PDF: \n") + ".pdf" secondPdf = input("\nEnter the PATH of your second PDF:\n") + ".pdf" result = input("\nEnter the final name of the Merged PDF: \n") + ".pdf" pdfs = [firstPdf, secondPdf] merger = PdfFileMerger() for pdf in pdfs: merger.append(pdf) merger.write(result) merger.close() print("\nYour PDF has been merged :)\n Thank You for using this Script :D Cheers !!! \n")
import os from PyPDF2 import PdfFileMerger # use dict to sort by filepath or filename file_dict = {} for subdir, dirs, files in os.walk("a"): for file in files: filepath = subdir + os.sep + file # you can have multiple endswith if filepath.endswith((".pdf", ".PDF")): file_dict[file] = filepath # use strict = False to ignore PdfReadError: Illegal character error merger = PdfFileMerger(strict=False) for k, v in file_dict.items(): print(k, v) merger.append(v) merger.write("combined_result.pdf")
'LOWCOAST': "erc_lgc", 'RIOGRAND': "erc_rgp", 'SOUTHPLN': "erc_sp", 'UPRCOAST': "erc_ugc", 'WESTPINE': "erc_wpw" } for key, fn in PSAPDF.items(): psapdf = 'http://ticc.tamu.edu/Documents/PredictiveServices/Fuels/' + fn + '.pdf' psapng = 'http://ticc.tamu.edu/Documents/PredictiveServices/Fuels/' + fn + '.png' print key, psapng links.append((psapdf, 'http')) #print links for link in links: if '.pdf' in link[0]: index = link[0].rfind('/') + 1 filename = link[0][index:] print filename + ' is downloading...' try: urllib.urlretrieve(link[0], os.path.join(Download, filename)) except: print "Unexpected error:", sys.exc_info()[0] filenames = os.listdir(Download) merger = PdfFileMerger() os.chdir(Download) for filename in filenames: merger.append(PdfFileReader(file(filename, 'rb'))) ArchiveFile = os.path.join(Archive, today + "_ERC.pdf") merger.write(ArchiveFile)
def makeZine(jsonPathList, collaborators, pkAIList, pkAuthorDict): # read jsons files dataList = [] for jsonPath in jsonPathList: with open(jsonPath) as jsonFile: dataList.append(json.load(jsonFile)) print('## jsons are ready!') # initialize pdf pdf = FPDF(orientation='P', unit='mm', format='A5') # add NeutralStd fonts addFonts(pdf) # add results print('# preparing results...') addResults(pdf, dataList, pkAIList, pkAuthorDict) print('## results are ready!') # add collaborators addCollaborators(pdf, collaborators) # add glossary page print('# preparing zine pdf...') # output partial zine pdf.output("src/pdfPages/partialZine.pdf") # add cover page zine = PdfFileMerger() zine.merge(0, 'src/pdfPages/capa.pdf') zine.merge(1, 'src/pdfPages/blankPage.pdf') zine.merge(2, 'src/pdfPages/apresentacao.pdf') zine.merge(3, 'src/pdfPages/blankPage.pdf') zine.merge(4, 'src/pdfPages/partialZine.pdf') partialZine = PdfFileReader('src/pdfPages/partialZine.pdf') numPages = partialZine.getNumPages() if numPages % 2 != 0: zine.merge(500, 'src/pdfPages/blankPage.pdf') zine.merge(501, 'src/pdfPages/contracapa.pdf') zine.write('zine.pdf') print('## zine is ready!')
def plot_data(func_df, job_df): merged_pdf = PdfFileMerger() for appname in func_df.AppName.unique(): fig = plt.figure(figsize=(10,6)) appdf = func_df[func_df['AppName'] == appname] baseline_df = appdf.loc[appdf['Expt'] == "Baseline"] slackpred_df = appdf.loc[appdf['Expt'] == "SlackPrediction"] sns.distplot(baseline_df['total_turnaround_lat'], label = "Baseline", hist=True, kde=False, rug=False) sns.distplot(slackpred_df['total_turnaround_lat'], label = "SlackPrediction", hist=True, kde=False, rug=False) plt.title(appname) plt.legend() close_fig(merged_pdf) fig = plt.figure(figsize=(10,6)) baseline_df = job_df.loc[job_df['Expt'] == "Baseline"] slackpred_df = job_df.loc[job_df['Expt'] == "SlackPrediction"] sns.distplot(baseline_df['total_turnaround_lat'], label = "Baseline", hist=True, kde=True, rug=False).set(xlim=(0)) sns.distplot(slackpred_df['total_turnaround_lat'], label = "SlackPrediction", hist=True, kde=True, rug=False).set(xlim=(0)) plt.title("All Jobs - Baseline vs SlackPrediction") plt.legend() close_fig(merged_pdf) fig, ax = plt.subplots(figsize=(10,6)) job_df['start_time_int64'] = job_df.Minimum.astype(np.int64) job_df.plot(x='start_time_int64', y='total_turnaround_lat', kind='scatter', ax=ax) ## Need to fix why we see 00:00:00 after we convert back to time. # ax.set_xticklabels([dt.date.fromtimestamp(ts / 1e9).strftime('%H:%M:%S') for ts in ax.get_xticks()]) # ax.set_yticklabels([dt.date.fromtimestamp(ts / 1e9).strftime('%H:%M:%S') for ts in ax.get_yticks()]) close_fig(merged_pdf) baseline_df = func_df.loc[func_df['Expt'] == "Baseline"] baseline_df = baseline_df.reset_index() slackpred_df = func_df.loc[func_df['Expt'] == "SlackPrediction"] slackpred_df = slackpred_df.reset_index() for delta_plots_var in ['delta_c1.lastTransitionTime', 'delta_init_cs_state.startedAt', 'delta_init_cs_state.finishedAt', 'delta_c0.lastTransitionTime', 'delta_c3.lastTransitionTime', 'delta_cs_state.startedAt', 'delta_cs_state.finishedAt'] : fig, ax = plt.subplots(figsize=(10,6)) print ("Charts for .. ", delta_plots_var) # ax = sns.lineplot(x=baseline_df.index, y=delta_plots_var, label="Baseline", data=baseline_df) # ax = sns.lineplot(x=slackpred_df.index, y=delta_plots_var, label="SlackPredictions", data=slackpred_df) # ax = sns.scatterplot(x=baseline_df.index, y=delta_plots_var, label="Baseline", data=baseline_df) # ax = sns.scatterplot(x=slackpred_df.index, y=delta_plots_var, label="SlackPredictions", data=slackpred_df) # ax = sns.violinplot(x=func_df.index, y=delta_plots_var, hue='Expt', split=True, inner="quart", data=func_df) # sns.despine(left=True) sns.distplot(baseline_df[delta_plots_var], label = "Baseline", norm_hist=True, kde=False, rug=False) sns.distplot(slackpred_df[delta_plots_var], label = "SlackPrediction", norm_hist=True, kde=False, rug=False) plt.legend() close_fig(merged_pdf) print ("Done", delta_plots_var) fig, ax = plt.subplots(figsize=(10,6)) ax = sns.lineplot(x=baseline_df.index, y='delta_init_cs_state.startedAt', data=baseline_df) ax = sns.lineplot(x=slackpred_df.index, y='delta_init_cs_state.startedAt', data=slackpred_df) close_fig(merged_pdf) print (slackpred_df['delta_c3.lastTransitionTime']) ## Write all charts to PDF here write_pdf(merged_pdf) return func_df, job_df
def _post_pdf(self, save_in_attachment, pdf_content=None, res_ids=None): """ Inherit the method _post_pdf to add addendums in the content. The main method will split the content by res_id and add (if required) the generated document in attachments. Odoo will use the tag /Root/Dest (created by wkhtmltopdf) to know how to cut the PDF content by document (res_id). It means that if we simply add some pages, the value of the tag will no longer be right. To avoid to modify tags in the PDF content, we simply split the document by res_id and call the main method (super) record by record. """ if not pdf_content or not res_ids: return super()._post_pdf(save_in_attachment, pdf_content=pdf_content, res_ids=res_ids) addendums = self.env['ago.addendum'].search([('ir_act_report_id', '=', self.id)]) if not addendums: return super()._post_pdf(save_in_attachment, pdf_content=pdf_content, res_ids=res_ids) # Evaluate the domain of each addendum res_ids_by_addendum = {} for addendum in addendums: if addendum.condition == 'always': res_ids_by_addendum[addendum] = res_ids else: domain = safe_eval( addendum.filter_domain) + [('id', 'in', res_ids)] res_ids_by_addendum[addendum] = self.env[self.model].search( domain).ids pdf_content_stream = io.BytesIO(pdf_content) reader = PdfFileReader(pdf_content_stream) # Read the PDF to be able to cut the PDF (copy past of the main method) streams = [] if len(res_ids) == 1: streams.append((res_ids[0], pdf_content_stream)) else: if reader.trailer['/Root'].get('/Dests'): outlines_pages = sorted( set(outline.getObject()[0] for outline in reader.trailer['/Root']['/Dests'].values())) assert len(outlines_pages) == len(res_ids) for i, num in enumerate(outlines_pages): to = outlines_pages[ i + 1] if i + 1 < len(outlines_pages) else reader.numPages attachment_writer = PdfFileWriter() for j in range(num, to): attachment_writer.addPage(reader.getPage(j)) stream = io.BytesIO() attachment_writer.write(stream) streams.append((res_ids[i], stream)) else: # If no outlines available, do not save each record streams.append((None, pdf_content_stream)) # Create the main PDF merger (contains all documents) full_pdf_merger = PdfFileMerger() # Look on all streams (a stream is a document for a res_id) for res_id, stream in streams: pdf_merger = PdfFileMerger() for addendum in addendums.filtered( lambda line: line.position == 'top'): # Append the addendum in the stream if required if res_id not in res_ids_by_addendum[addendum]: continue pdf_addendum_content = io.BytesIO( base64.b64decode(addendum.addendum)) pdf_addendum_reader = PdfFileReader(pdf_addendum_content) pdf_merger.append(pdf_addendum_reader) pdf_reader = PdfFileReader(stream) pdf_merger.append(pdf_reader, import_bookmarks=False) for addendum in addendums.filtered( lambda line: line.position == 'bottom'): # Append the addendum in the stream if required if res_id not in res_ids_by_addendum[addendum]: continue pdf_addendum_content = io.BytesIO( base64.b64decode(addendum.addendum)) pdf_addendum_reader = PdfFileReader(pdf_addendum_content) pdf_merger.append(pdf_addendum_reader) # Retrieve the PDF content for the current document pdf_merger.write(stream) record_pdf_content = stream.getvalue() try: stream.close() except Exception: pass # Call the method super with ONLY the current res_id and his PDF content. # This method return the content of the PDF. The content can change during the call new_pdf_content = super()._post_pdf(save_in_attachment, pdf_content=record_pdf_content, res_ids=[res_id]) # We recreate a new PDF with the new content new_reader_buffer = io.BytesIO(new_pdf_content) new_reader = PdfFileReader(new_reader_buffer) # Add the PDF content to the main PDF merger full_pdf_merger.append(new_reader) # Generate the content of the main PDF full_pdf = io.BytesIO() full_pdf_merger.write(full_pdf) pdf_content = full_pdf.getvalue() full_pdf.close() return pdf_content
def generate_report(username="", password=None, environment="production", result_list=[], only_combined=True, client_obj=None): """Generates and downloads a PDF report of test results This method will generate and download a PDF report of the specified test results. The report will consist of all information relevant to that particular result, such as: * result info * model info * model instance info * test info * test instance info * output files associated with result Parameters ---------- username : string Your HBP collaboratory username. environment : string, optional Used to indicate whether being used for development/testing purposes. Set as `production` as default for using the production system, which is appropriate for most users. When set to `dev`, it uses the `development` system. For other values, an external config file would be read (the latter is currently not implemented). result_list : list List of result UUIDs that need to be included in report. only_combined : boolean, optional Indicates whether only a single combined PDF should be saved. Set to `True` as default. When set to `False`, then `n+2` PDFs will be saved, where `n` is the number of valid result UUIDs. These would include: * Combined PDF report * Summary of call to `generate_report()` * One PDF for each valid result UUID client_obj : ModelCatalog/TestLibrary object Used to easily create a new ModelCatalog/TestLibrary object if either exist already. Avoids need for repeated authentications; improves performance. Also, helps minimize being blocked out by the authentication server for repeated authentication requests (applicable when running several tests in quick succession, e.g. in a loop). Returns ------- list List of valid UUIDs for which the PDF report was generated path The absolute path of the generated report Examples -------- >>> result_list = ["a618a6b1-e92e-4ac6-955a-7b8c6859285a", "793e5852-761b-4801-84cb-53af6f6c1acf"] >>> valid_uuids, report_path = utils.generate_report(username="******", result_list=result_list) """ # This method can be significantly improved in future. try: from fpdf import FPDF except ImportError: print("Please install the following package: fpdf") return try: from PyPDF2 import PdfFileMerger, PdfFileReader except ImportError: print("Please install the following package: PyPDF2") return class PDF(FPDF): def header(self): # Logo self.image('https://i.imgur.com/sHi1OSs.png', 80, 8, 50) # Arial bold 15 self.set_font('Arial', 'B', 18) # Move to the right self.ln(15) self.cell(45) # Title self.cell(100, 10, 'Validation Framework Report', 1, 0, 'C') # Line break self.ln(20) # # Page footer # def footer(self): # # Position at 1.5 cm from bottom # self.set_y(-15) # # Arial italic 8 # self.set_font('Arial', 'I', 8) # # Page number # self.cell(0, 10, 'Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C') if client_obj: model_catalog = ModelCatalog.from_existing(client_obj) else: model_catalog = ModelCatalog(username, password, environment=environment) test_library = TestLibrary.from_existing(model_catalog) result_data = {} valid_uuids = [] for result_id in result_list: result = test_library.get_result(result_id=result_id) if len(result["results"]) != 0: valid_uuids.append(result_id) result_data[result_id] = result["results"][0] def _print_param_value(pdf, param, value, fontsize): pdf.set_font('Arial', 'B', fontsize) pdf.cell(40, 10, param) pdf.set_font('Arial', '', fontsize) pdf.cell(0, 10, value) pdf = PDF() # pdf.alias_nb_pages() timestamp = datetime.now() filename = str("HBP_VF_Report_" + timestamp.strftime("%Y%m%d-%H%M%S") + ".pdf") # Cover Page pdf.add_page() _print_param_value(pdf, "Report Name: ", filename, 14) pdf.ln(10) _print_param_value(pdf, "Created Date: ", timestamp.strftime("%Y-%m-%d %H:%M:%S"), 14) pdf.ln(20) pdf.set_font('Arial', 'B', 14) pdf.cell(40, 10, "Contains data for following Result UUIDs: ") pdf.ln(10) pdf.set_font('Arial', '', 14) for result_id in valid_uuids: pdf.cell(40) pdf.cell(0, 10, result_id) pdf.ln(10) if len(valid_uuids) < len(result_list): pdf.ln(10) pdf.set_font('Arial', 'B', 14) pdf.cell(40, 10, "Following UUIDs were invalid: ") pdf.ln(10) pdf.set_font('Arial', '', 14) for result_id in result_list: if result_id not in valid_uuids: pdf.cell(40) pdf.cell(0, 10, result_id) pdf.ln(10) pdf.ln(50) pdf.set_font('Arial', 'B', 14) pdf.cell(190, 10, 'Report generated by the HBP Validation Framework', 0, 1, 'C') pdf.ln(10) pdf.set_font('Arial', 'I', 12) pdf.cell(90, 10, 'For more information, you may visit:') pdf.ln(10) pdf.cell(15) _print_param_value(pdf, "Python Client: ", "https://github.com/apdavison/hbp-validation-client/", 12) pdf.ln(10) pdf.cell(15) _print_param_value(pdf, "Documentation: ", "http://hbp-validation-client.readthedocs.io/", 12) if not os.path.exists("./report/"): os.makedirs("./report/") pdf.output(str("./report/" + filename[:-4] + "_cover.pdf"), 'F') result_ctr = 0 # Result Pages for result_id in valid_uuids: pdf = PDF() # pdf.alias_nb_pages() pdf.add_page() # General Result Info model_instance_id = result_data[result_id]["model_version_id"] model_instance_info = model_catalog.get_model_instance( instance_id=model_instance_id) model_id = model_instance_info["model_id"] model_info = model_catalog.get_model(model_id=model_id, instances=False, images=False) test_instance_id = result_data[result_id]["test_code_id"] test_instance_info = test_library.get_test_instance( instance_id=test_instance_id) test_id = test_instance_info["test_definition_id"] test_info = test_library.get_test_definition(test_id=test_id) test_info.pop("codes") # pdf.add_page() _print_param_value(pdf, "Result UUID: ", result_id, 14) # Result Info pdf.ln(10) pdf.set_font('Arial', 'BU', 14) pdf.ln(10) pdf.cell(190, 10, 'Result Info', 0, 1, 'C') for key, val in result_data[result_id].items(): _print_param_value(pdf, str(key + ": "), str(val), 12) pdf.ln(10) # Model Info pdf.ln(10) pdf.set_font('Arial', 'BU', 14) pdf.ln(10) pdf.cell(190, 10, 'Model Info', 0, 1, 'C') for key, val in model_info.items(): if key == "app": _print_param_value(pdf, "collab_id", str(val["collab_id"]), 12) pdf.ln(10) if "id" in val: _print_param_value(pdf, "app_id", str(val["id"]), 12) else: _print_param_value( pdf, str(key + ": "), unicodedata.normalize('NFKD', val).encode( 'ascii', 'ignore') if isinstance(val, unicode) else str(val), 12) pdf.ln(10) # Model Instance Info pdf.ln(10) pdf.set_font('Arial', 'BU', 14) pdf.ln(10) pdf.cell(190, 10, 'Model Instance Info', 0, 1, 'C') for key, val in model_instance_info.items(): _print_param_value(pdf, str(key + ": "), str(val), 12) pdf.ln(10) # Test Info pdf.ln(10) pdf.set_font('Arial', 'BU', 14) pdf.ln(10) pdf.cell(190, 10, 'Test Info', 0, 1, 'C') for key, val in test_info.items(): _print_param_value(pdf, str(key + ": "), str(val), 12) pdf.ln(10) # Test Instance Info pdf.ln(10) pdf.set_font('Arial', 'BU', 14) pdf.ln(10) pdf.cell(190, 10, 'Test Instance Info', 0, 1, 'C') for key, val in test_instance_info.items(): _print_param_value(pdf, str(key + ": "), str(val), 12) pdf.ln(10) pdf.output( str("./report/" + filename[:-4] + "_temp_" + str(result_ctr) + ".pdf"), 'F') # Additional Files if result_data[result_id]["results_storage"]: datastore = CollabDataStore(auth=model_catalog.auth) entity_uuid = datastore._translate_URL_to_UUID( result_data[result_id]["results_storage"]) file_list = datastore.download_data_using_uuid(entity_uuid) merger = PdfFileMerger() merger.append( str("./report/" + filename[:-4] + "_temp_" + str(result_ctr) + ".pdf")) temp_txt_files = [] for datafile in file_list: if datafile.endswith(".pdf"): merger.append(PdfFileReader(file(datafile, 'rb'))) elif datafile.endswith((".txt", ".json")): txt_pdf = FPDF() txt_pdf.add_page() txt_pdf.set_font('Arial', 'BU', 14) txt_pdf.cell(0, 10, os.path.basename(datafile), 0, 1, 'C') txt_pdf.set_font('Courier', '', 8) with open(datafile, 'r') as txt_file: txt_content = txt_file.read().splitlines() for txt_line in txt_content: txt_pdf.cell(0, 0, txt_line) txt_pdf.ln(5) savepath = os.path.join( "./report", "temp_" + os.path.splitext(os.path.basename(datafile))[0] + "_" + str(result_ctr) + ".pdf") temp_txt_files.append(savepath) txt_pdf.output(str(savepath), 'F') merger.append(PdfFileReader(file(savepath, 'rb'))) merger.write( str("./report/" + filename[:-4] + "_" + str(result_ctr) + ".pdf")) os.remove( str("./report/" + filename[:-4] + "_temp_" + str(result_ctr) + ".pdf")) for tempfile in temp_txt_files: os.remove(tempfile) result_ctr = result_ctr + 1 merger = PdfFileMerger() merger.append(str("./report/" + filename[:-4] + "_cover.pdf")) if only_combined: os.remove(str("./report/" + filename[:-4] + "_cover.pdf")) for i in range(result_ctr): merger.append(str("./report/" + filename[:-4] + "_" + str(i) + ".pdf")) if only_combined: os.remove(str("./report/" + filename[:-4] + "_" + str(i) + ".pdf")) merger.write(str("./report/" + filename)) report_path = os.path.abspath("./report/" + filename) print("Report generated at: {}".format(report_path)) return valid_uuids, report_path
filelist = glob.glob("*.pdf") # Get a list of files in the current directory filelist.sort() for pdf in filelist: print("found " + str(pdf)) for pdf in filelist: try: #opening the pdf and reading the metadata file_in = open(pdf, 'rb+') pdf_reader = PdfFileReader(file_in) metadata = pdf_reader.getDocumentInfo() #creating a new file, appending the pdf and adding custom metadata pdf_merger = PdfFileMerger() pdf_merger.append(file_in) pdf_merger.addMetadata({ '/Author': "", '/Title': "", '/Subject': "", '/Producer': "", '/Creator': "", '/Keywords': "", }) #writing the the new pdf document to a new file file_out = open('new.pdf', 'wb+') pdf_merger.write(file_out) file_in.close() file_out.close()
def PDF_merger(pdf_list, output_path): merger = PdfFileMerger() for pdf in pdf_list: merger.append(pdf) merger.write(output_path) merger.close()
# In[20]: #INSTALL PyPDF2 library get_ipython().run_line_magic('pip', 'install PyPDF2') # In[21]: # merge the pdfs from PyPDF2 import PdfFileMerger pdfs = ['fintech.pdf', 'fintechreview.pdf'] merger = PdfFileMerger() for pdf in pdfs: merger.append(pdf) a = merger.write("result.pdf") merger.close() # In[22]: # count the number of pages in the merged pdf import PyPDF2 # pdf file object pdfFileObj = open('result.pdf', 'rb')
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()
from PyPDF2 import PdfFileMerger import glob pdfs = sorted(glob.glob('*.pdf')) merger = PdfFileMerger() for pdf in pdfs: merger.append(open(pdf, 'rb')) with open('mergedfile.pdf', 'wb') as fout: merger.write(fout)
from PyPDF2 import PdfFileMerger, PdfFileReader from os import listdir from os.path import isfile, join pathToPdfs = 'pdfs' onlyfiles = [f for f in listdir(pathToPdfs) if isfile(join(pathToPdfs, f))] fileNumbers = [] for filePath in onlyfiles: filePath = filePath.split('.', 1) #print(filePath[0]) fileNumbers.append(int(filePath[0])) fileNumbers.sort() """ for n in fileNumbers: print(n) """ # Call the PdfFileMerger mergedObject = PdfFileMerger() # 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 for fileNumber in fileNumbers: mergedObject.append(PdfFileReader('pdfs/' + str(fileNumber) + '.pdf', 'rb')) #mergedObject.append(PdfFileReader('6_yuddhakanda_' + str(fileNumber)+ '.pdf', 'rb')) # Write all the files into a file which is named as shown below mergedObject.write("output.pdf")
def _pdfmerge(self, file_list, pdfname): """Merge a list of PDF files. file_list kan være en liste med filnavne, eller en liste med tupler af filnavne og bogmærkenavne (til indholdsfortegnelsen).""" # det er egentlig noget hø, sådan automagisk at bøje sig rundt # om forskellige typer i argumenterne. Det nærmest tigger og # be'r om at blive til en bug i fremtiden. Men det giver # mening som et format, og bagudkompatibilitet er vigtigt. Så # nu er vi her. # check, om output er nyere end input try: output_modtime = os.stat(pdfname).st_mtime except FileNotFoundError: # ingen output er ikke nyere end nogen ting output_modtime = float('-inf') merge_args = () # argumenter til PyPDF2 append() found_fresh_input = False # flag (::sadface::) for fe in file_list: f, bookmark = fe if isinstance(fe, tuple) else (fe, None) if type(f) == str and f[-3:] == 'pdf': merge_args += ({"fileobj": f, "bookmark": bookmark}, ) found_fresh_input |= (os.stat(f).st_mtime > output_modtime) elif type(f).__name__ == "Revue": for act in f.acts: for m in act.materials: pdfpath = os.path.join( self.conf["Paths"]["pdf"], os.path.dirname(os.path.relpath(m.path)), os.path.splitext(m.file_name)[0] + ".pdf") merge_args += ({ "fileobj": pdfpath, "bookmark": bookmark or m.title or None }, ) found_fresh_input |= (os.path.getmtime(pdfpath) > output_modtime) elif type(f).__name__ == "Actor": for role in f.roles: pdfpath = os.path.join( self.conf["Paths"]["pdf"], os.path.dirname(os.path.relpath(role.material.path)), os.path.splitext(role.material.file_name)[0] + ".pdf") merge_args += ({ "fileobj": pdfpath, "bookmark": bookmark or role.material.title or None }, ) found_fresh_input |= (os.path.getmtime(pdfpath) > output_modtime) else: raise TypeError("List must only contain PDF file paths, " "a Revue object or an Actor object.") if (not found_fresh_input and not self.conf.getboolean( "TeXing", "force TeXing of all files")): return # intet nyt ind = intet nyt ud merger = PdfFileMerger() # så kører bussen tex_translations = { "\\texttrademark": "™", "--": "–", "---": "—", "''": "“", "``": "”" } for kwargs in merge_args: for t in tex_translations: if kwargs["bookmark"]: kwargs["bookmark"] = (kwargs["bookmark"].replace( t, tex_translations[t])) merger.append(**kwargs) try: with open(pdfname, "wb") as output: merger.write(output) output.close() except FileNotFoundError: err = "File could not be opened: {}".format(pdfname) rc = 1 except: rc = 1 else: rc = 0 print("{:<42}".format("\033[0;37;1m{}:\033[0m".format( os.path.split(pdfname)[1])), end="") if rc == 0: print("\033[0;32m Success!\033[0m") else: print("\033[0;31m Failed!\033[0m") print(" -> {}".format(err))
defined_lines=defined_lines+1+len(lines) except : break pdf.set_x(10) pdf.set_font("Arial", size=20) pdf.set_text_color(224, 165, 0) pdf.cell(-1, 10, '3. Some statistics', ln=1) pdf_titol.set_x(20) pdf_titol.set_font("Arial", 'B' , size=20) pdf_titol.cell(-1, 10, '3. Some statistics') pdf_titol.set_x(-30) pdf_titol.cell(-1, 10, '%d' %pdf.page_no() ,ln=1) pdf.set_font("Arial", size=12) pdf.set_text_color(0, 0, 0) pdf_titol.cell(-1, 10, '' ,ln=1) pdf_titol.set_x(10) pdf.multi_cell(190, 10, 'The project has %s programs, with %s defined functions. The program define %s variables and import %s libraries (external libraries) on %s lines of python code.' %( num_prog ,defined_function ,len(defined_variable),len(defined_libraries),defined_lines)) pdf_titol.output("simple_demo_titol.pdf") pdf.output("simple_demo.pdf") merger = PdfFileMerger() merger.append('simple_demo_titol.pdf') merger.append('simple_demo.pdf') merger.write("BG_SA_Scripting_Lab_2.pdf") merger.close() pdf.close() pdf_titol.close() os.remove("simple_demo_titol.pdf") os.remove("simple_demo.pdf") print("fin")
import os from PyPDF2 import PdfFileMerger import re def atoi(text): return int(text) if text.isdigit() else text def natural_keys(text): return [ atoi(c) for c in re.split('(\d+)',text) ] pdfs = os.listdir('./input') pdfs.sort(key=natural_keys) merger = PdfFileMerger() for pdf in pdfs: merger.append(f'./input/{pdf}') merger.write("./out/merged.pdf") merger.close()
def merge(filenames, out): merger = PdfFileMerger() for filename in filenames: merger.append(PdfFileReader(file(filename, 'rb'))) merger.write(out)
def SpeedCoach(readCSV, workout): summary = readCSV[15] values = readCSV for i in range(0, 30): values = np.delete(values, 0) values = np.delete(values, len(values) - 1) distance = [] time = [] split = [] StrokeRate = [] Stroke = [] DistancePerStroke = [] HeartRate = [] for row in values: distanceR = float(row[1]) timeR = row[3] splitR = row[4] StrokeRateR = float(row[8]) StrokeR = float(row[9]) DistancePerStrokeR = float(row[10]) if row[12] == "---": HeartRateR = 0 else: HeartRateR = float(row[12]) distance.append(distanceR) time.append(timeR) split.append(splitR) StrokeRate.append(StrokeRateR) Stroke.append(StrokeR) DistancePerStroke.append(DistancePerStrokeR) HeartRate.append(HeartRateR) path = "data/" + workout + '/' # -Abspeichern als Arrays und umrechnung in float----------------------------------------------------------------------# StrokeRate = np.asarray(StrokeRate) Stroke = np.asarray(Stroke) DistancePerStroke = np.asarray(DistancePerStroke) distance = np.asarray(distance) time = np.asarray(time) time_sec = get_sec(time) split = np.asarray(split) split_sec = get_sec(split) correct_HR_values = True for i in HeartRate: if i == 0: correct_HR_values = False # -Durchschnitts- & Maximalwerte---------------------------------------------------------------------------------------# # Schlagzahl StrokeRateMean = round(np.mean(StrokeRate), 1) # StrokeRateMean = np.mean(StrokeRate) MaxStrokeRate = 0 for k in range(len(StrokeRate)): if MaxStrokeRate < StrokeRate[k] and StrokeRate[k] < 50: MaxStrokeRate = StrokeRate[k] # print('Stroke Rate Mean: %s spm\nMax. Stroke Rate: %s spm\n' % (StrokeRateMean, MaxStrokeRate)) # Distanz pro Schlag DistancePerStrokeMean = round(np.mean(DistancePerStroke), 1) # DistancePerStrokeMean = np.mean(DistancePerStroke) MaxDPS = 0 for k in range(len(DistancePerStroke)): if MaxDPS < DistancePerStroke[k] and DistancePerStroke[k] < 15: MaxDPS = DistancePerStroke[k] # print('Distance p. Stoke Mean = %s m\nMax. Distance p. Stroke = %s m\n' % (DistancePerStrokeMean, MaxDPS)) # Split splitmean_sec = round(np.mean(split_sec), 1) splitmean = get_time(splitmean_sec) maxsplit = np.min(split_sec) # print('Split Mean = %s /500m\nMax. Split = %s /500m\n' % (splitmean, get_time(maxsplit))) minsplit = np.min(split_sec) if len(distance) > 100: rangel = 30 else: rangel = 10 for k in range(rangel, len(split_sec)): if minsplit < split_sec[k]: minsplit = split_sec[k] # Herzfrequenz if correct_HR_values == True: HRmean = round(statistics.mean(HeartRate), 1) HRmax = max(HeartRate) # print('Mean heart rate = %s spm\nMax. heart rate = %s spm' % (HRmean, HRmax)) # else: # print("Heart rate values are not correct!") # -Diagramme-----------------------------------------------------------------------------------------------------------# newpath = workout if not os.path.exists(path): os.makedirs(path) if minsplit > 200: minimumy = 208 else: minimumy = minsplit # split plt.plot(distance, split_sec, color='navy', label='Split', lw=0.8) plt.yticks([ 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 105, 110, 115, 120, 130, 140, 150, 160, 170, 180, 190, 200 ], [ '1:20.0', '1:22.0', '1:24.0', '1:26.0', '1:28.0', '1:30.0', '1:32.0', '1:34.0', '1:36.0', '1:38.0', '1:40.0', '1:45.0', '1:50.0', '1:55.0', '2:00.0', '2:10.0', '2:20.0', '2:30.0', '2:40.0', '2:50.0', '3:00.0', '3:10.0', '3:20.0' ]) plt.grid(color='grey', ls=':') plt.axhline(y=splitmean_sec, xmin=0.05, xmax=0.95, color='red', ls='--', lw=0.5, label='Avg. split (%s min/500m)' % (splitmean)) plt.ylim(maxsplit - 2, minimumy + 2) plt.xlim(0, distance[len(distance) - 1] + 10) plt.xlabel(r'Distance [$m$]', size=10) plt.ylabel(r'Split [$min/500m$]', size=10) plt.title(workout + ' - Split') plt.legend(fancybox=True) plt.gca().invert_yaxis() plt.tight_layout() plt.savefig(path + 'Split.pdf', format='PDF') plt.draw() plt.gcf().clear() # Stroke Rate plt.plot(distance, StrokeRate, color='navy', label='Stroke Rate', lw=0.8) plt.grid(color='grey', ls=':') plt.ylim(MaxStrokeRate + 2, np.min(StrokeRate) - 2) plt.xlim(0, distance[len(distance) - 1] + 10) plt.axhline(y=StrokeRateMean, xmin=0.05, xmax=0.95, color='red', ls='--', lw=0.5, label='Avg. Stroke Rate (%s spm)' % StrokeRateMean) plt.xlabel(r'Distance [$m$]', size=10) plt.ylabel(r'Stroke Rate [$spm$]', size=10) plt.title(workout + ' - Stroke Rate') plt.legend() plt.gca().invert_yaxis() plt.tight_layout() plt.savefig(path + 'StrokeRate.pdf', format='PDF') plt.draw() plt.gcf().clear() # Heart Rate if correct_HR_values == True: plt.plot(distance, HeartRate, color='navy', label='Heart Rate', lw=0.8) plt.grid(color='grey', ls=':') plt.ylim(HRmax + 2, np.min(HeartRate) - 2) plt.xlim(0, distance[len(distance) - 1] + 10) plt.axhline(y=HRmean, xmin=0.05, xmax=0.95, color='red', ls='--', lw=0.5, label='Avg. Heart Rate (%s spm)' % HRmean) plt.xlabel(r'Distance [$m$]', size=10) plt.ylabel(r'Heart Rate [$spm$]', size=10) plt.title(workout + ' - Heart Rate') plt.legend() plt.gca().invert_yaxis() plt.tight_layout() plt.savefig(path + 'HeartRate.pdf', format='PDF') plt.draw() plt.gcf().clear() # Distance per Stroke per StrokeRate plt.plot(distance, DistancePerStroke, color='navy', label='Distance p. Stroke', lw=0, marker='o', markersize=5) plt.axhline(y=DistancePerStrokeMean, xmin=0.05, xmax=0.95, color='red', ls='--', lw=0.5, label='Avg. Heart Rate (%s spm)' % DistancePerStrokeMean) plt.grid(color='grey', ls=':') plt.ylim(5, 15) plt.xlim(0, distance[len(distance) - 1] + 10) plt.xlabel(r'Distance [$m$]', size=10) plt.ylabel(r'Distance p. Stroke [$m$]', size=10) plt.title(workout + ' - Distance p. Stroke') plt.legend() plt.tight_layout() plt.savefig(path + 'DPS.pdf', format='PDF') plt.draw() plt.gcf().clear() # -PDF Merge-----------------------------------------------------------------------------------------------------------# if correct_HR_values == True: pdf_files = ['Split.pdf', 'StrokeRate.pdf', 'HeartRate.pdf', 'DPS.pdf'] else: pdf_files = ['Split.pdf', 'StrokeRate.pdf', 'DPS.pdf'] merger = PdfFileMerger() for files in pdf_files: merger.append(path + files) merger.write(path + workout + '.pdf') merger.close() os.remove(path + 'StrokeRate.pdf') os.remove(path + 'Split.pdf') if correct_HR_values == True: os.remove(path + 'HeartRate.pdf') os.remove(path + 'DPS.pdf') test = path + workout + '.pdf' path = path file = workout + '.pdf' # -Ende----------------------------------------------------------------------------------------------------------------# return path, file
import pprint from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger import time if getattr(sys, 'frozen', False): bundle_dir = sys._MEIPASS else: bundle_dir = os.path.dirname(os.path.abspath(__file__)) os.chdir(bundle_dir) app = xls.App(visible=True, add_book=True) app.books[0].close() print("遍历指定目录下所有的文件和文件夹,包括子目录内的") list_dirs = os.walk(sys.path[0]) pdf_new = PdfFileMerger() for root, dirs, files in list_dirs: for f in files: # 分离文件名与扩展名,仅显示txt后缀的文件 if os.path.splitext(f)[1] == '.xls' or os.path.splitext( f)[1] == '.xlsx' or os.path.splitext( f)[1] == '.xlsm' or os.path.splitext(f)[1] == '.XLS': file_path = os.path.join(root, f) app.books.open(file_path, update_links=True) obj_file = os.path.splitext(f)[0] + ".pdf" obj_path = os.path.join(root, obj_file) print("保存到:", obj_path) #设置excel为手动计算 #app.api.Calculation=-4105 #执行手动计算
import sys from PyPDF2 import PdfFileMerger, PdfFileReader merger = PdfFileMerger() a = sys.argv[1] + ".pdf" merger.write(a)
def pdf_append(file1, file2, file_out): merger = PdfFileMerger(strict=False) merger.append(file1) merger.append(file2) merger.write(file_out) merger.close()
def diagnostic_now(): original_date = os.getcwd()[-10::] ##### Flags ##### # thresholds and constants global pdfs, num_flags, num_sigma, fwhm_tol, pos_tol, peak_tol, ext_FWHM_num_sigma, ext_prof_tol, background_fit_tol, wave_fit_tol num_flags = 23 num_sigma = 2 fwhm_tol = 1 pos_tol = 5 peak_tol = 500 ext_FWHM_num_sigma = 2 ext_prof_tol = 5 background_fit_tol = 5 wave_fit_tol = 0.15 pdfs = glob('diagnostics_plots.pdf') for f in pdfs: os.remove(f) pdfs = [] # Use the FWHM_records file to determine how many total exposures there were for the given date fwhm_files = glob('FWHM*.txt') file_name = str(fwhm_files[0]) flags = setup_flags_table(file_name) ##### ------------------------------------------------------------------ ##### # Sort file names by type cal_files = glob('reduction*.txt') fwhm_files = glob('FWHM*.txt') wave_cal_files = glob('wavecal*.txt') model_cal_files = glob('continuum_normalization*.txt') extraction_files = glob('extraction_*_*.txt') spectra_files = glob('flux_fits*.txt') ##### ------------------------------------------------------------------ ##### # Calibrations for i in range(len(cal_files)): # Repeat copy of data below file_name = str(cal_files[i]) diagnostic_plots_cals(file_name, flags) ##### ------------------------------------------------------------------ ##### # FWHM for i in range(len(fwhm_files)): # First line not commented out file_name = str(fwhm_files[i]) diagnostic_plots_FWHM(file_name, flags) ##### ------------------------------------------------------------------ ##### # Wavelength Calibrations star_names = [] for i in range(len(wave_cal_files)): star_names.append(wave_cal_files[i][8:-21]) with open(wave_cal_files[i], 'r') as f: first_line = f.readline() unique_names = unique_star_names(star_names) for sub in unique_names: file_names = [x for x in wave_cal_files if str(sub) in x] diagnostic_plots_wavecal(file_names, flags) ##### ------------------------------------------------------------------ ##### # Model Calibrations star_names = [] for i in range(len(model_cal_files)): star_names.append(model_cal_files[i][24:-21]) with open(model_cal_files[i], 'r') as f: first_line = f.readline() unique_names = unique_star_names(star_names) for sub in unique_names: file_name = [x for x in model_cal_files if str(sub) in x] diagnostic_plots_continuum(file_name[0], flags) ##### ------------------------------------------------------------------ ##### # Extraction for i in range(len(extraction_files)): file_name = str(extraction_files[i]) diagnostic_plots_extraction(file_name, flags) ######------------------------------------------------------------------ ##### for i in range(len(spectra_files)): file_name = str(spectra_files[i]) diagnostic_plots_spectra(file_name, flags) ######------------------------------------------------------------------ ##### # Merge all pdfs of plots #pdfs = glob('*.pdf') outfile = PdfFileMerger() for f in pdfs: outfile.append(open(f, 'rb')) os.remove(f) outfile.write(open('diagnostic_plots.pdf', 'wb')) flags.to_csv('diagnostics_flags.csv')
def combine(pdfs, output): merger = PdfFileMerger() combine_imp(merger, pdfs, 0, None) merger.write(output)
cd_results_directory_name = work_dir + 'plots/cd/data/cd' condition_results_file_name = work_dir + 'plots/condition_number/data/condition/condition_results.dat' condition_results_directory_name = work_dir + 'plots/condition_number/data/condition' aoa_results_file_name = work_dir + 'plots/aoa/cl_aoa.dat' with open(aoa_results_file_name,'w') as cl_aoa_file: cl_aoa_file.flush() domain_results_file_name = work_dir + "plots/cl_error_domain_size/cl_error_results_domain.dat" with open(domain_results_file_name,'w') as cl_domain_file: cl_domain_file.flush() loads_output.write_header_all_cases(work_dir) merger_global = PdfFileMerger() merger_global_far_field_x = PdfFileMerger() merger_global_far_field_y = PdfFileMerger() merger_global_jump = PdfFileMerger() case = 0 Domain_Length = Initial_Domain_Size Domain_Width = Initial_Domain_Size for k in range(Number_Of_Domains_Size): Domain_Length = int(Domain_Length) Domain_Width = int(Domain_Width) FarField_MeshSize = int(Domain_Length / 50.0) AOA = Initial_AOA os.mkdir(output_gid_path + 'DS_' + str(Domain_Length))
def _issue_licence(self, request, application, issue_licence_form): # do credit card payment if required payment_status = payment_utils.PAYMENT_STATUSES.get( payment_utils.get_application_payment_status(application)) if payment_status == payment_utils.PAYMENT_STATUS_AWAITING: raise PaymentException( 'Payment is required before licence can be issued') elif payment_status == payment_utils.PAYMENT_STATUSES.get( payment_utils.PAYMENT_STATUS_CC_READY): payment_utils.invoke_credit_card_payment(application) licence = application.licence licence.issuer = request.user previous_licence = None if application.previous_application is not None: previous_licence = application.previous_application.licence licence.licence_number = previous_licence.licence_number # if licence is renewal, start with previous licence's sequence number if licence.licence_sequence == 0: licence.licence_sequence = previous_licence.licence_sequence if not licence.licence_number: licence.save(no_revision=True) licence.licence_number = '%s-%s' % ( str(licence.licence_type.pk).zfill(LICENCE_TYPE_NUM_CHARS), str(licence.id).zfill(LICENCE_NUMBER_NUM_CHARS)) # for re-issuing original_issue_date = application.licence.issue_date if application.licence.is_issued else None licence.licence_sequence += 1 licence.issue_date = date.today() # reset renewal_sent flag in case of reissue licence.renewal_sent = False licence_filename = 'licence-%s-%d.pdf' % (licence.licence_number, licence.licence_sequence) cover_letter_filename = 'cover-letter-%s-%d.pdf' % ( licence.licence_number, licence.licence_sequence) licence.cover_letter_document = create_cover_letter_pdf_document( cover_letter_filename, licence, request.build_absolute_uri(reverse('home'))) licence.save() if previous_licence is not None: previous_licence.replaced_by = licence previous_licence.save() licence_issued.send(sender=self.__class__, wildlife_licence=licence) # update statuses application.customer_status = 'approved' application.processing_status = 'issued' Assessment.objects.filter(application=application, status='awaiting_assessment').\ update(status='assessment_expired') application.save() # The licence should be emailed to the customer if they applied for it online. If an officer entered # the application on their behalf, the licence needs to be posted to the user. # CC's and attachments # Rules for emails: # If application lodged by proxy officer and there's a CC list: send email to CCs (to recipients = CCs) # else send the email to customer and if there are CCs put them into the bccs of the email ccs = None if 'ccs' in issue_licence_form.cleaned_data and issue_licence_form.cleaned_data[ 'ccs']: ccs = re.split('[,;]', issue_licence_form.cleaned_data['ccs']) attachments = [] if request.FILES and 'attachments' in request.FILES: for _file in request.FILES.getlist('attachments'): doc = Document.objects.create(file=_file, name=_file.name) attachments.append(doc) # Merge documents if attachments and not isinstance(attachments, list): attachments = list(attachments) current_attachment = create_licence_pdf_document( licence_filename, licence, application, settings.WL_PDF_URL, original_issue_date) if attachments: other_attachments = [] pdf_attachments = [] merger = PdfFileMerger() merger.append(PdfFileReader(current_attachment.file.path)) for a in attachments: if a.file.name.endswith('.pdf'): merger.append(PdfFileReader(a.file.path)) else: other_attachments.append(a) output = BytesIO() merger.write(output) # Delete old document current_attachment.delete() # Attach new document new_doc = Document.objects.create(name=licence_filename) new_doc.file.save(licence_filename, File(output), save=True) licence.licence_document = new_doc licence.save() output.close() else: licence.licence_document = current_attachment licence.save() # check we have an email address to send to if licence.profile.email and not licence.profile.user.is_dummy_user: to = [licence.profile.email] messages.success( request, 'The licence has now been issued and sent as an email attachment to the ' 'licencee: {}.'.format(licence.profile.email)) send_licence_issued_email(licence, application, request, to=to, bcc=ccs, additional_attachments=attachments) else: # no email messages.success( request, 'The licence has now been issued and must be posted to the licencee. Click ' 'this link to show the licence <a href="{0}" target="_blank">Licence PDF' '</a><img height="20px" src="{1}"></img> and this link to show the cover letter ' '<a href="{2}" target="_blank">Cover Letter PDF</a><img height="20px" src="{3}">' '</img>'.format(licence.licence_document.file.url, static('wl/img/pdf.png'), licence.cover_letter_document.file.url, static('wl/img/pdf.png'))) if ccs: send_licence_issued_email(licence, application, request, to=ccs, additional_attachments=attachments) application.log_user_action( ApplicationUserAction.ACTION_ISSUE_LICENCE_.format(licence), request)