def FindDuplicatesFilesInFolder(path): shatab = [] total = 0 small_count, dup_count, error_count = 0, 0, 0 pngdir = path if not os.path.exists(path): sg.MsgBox('Duplicate Finder', '** Folder doesn\'t exist***', path) return pngfiles = os.listdir(pngdir) total_files = len(pngfiles) for idx, f in enumerate(pngfiles): if not sg.EasyProgressMeter('Counting Duplicates', idx + 1, total_files, 'Counting Duplicate Files'): break total += 1 fname = os.path.join(pngdir, f) if os.path.isdir(fname): continue x = open(fname, "rb").read() m = hashlib.sha256() m.update(x) f_sha = m.digest() if f_sha in shatab: # uncomment next line to remove duplicate files # os.remove(fname) dup_count += 1 # sg.Print(f'Duplicate file - {f}') # cannot current use sg.Print with Progress Meter continue shatab.append(f_sha) msg = f'{total} Files processed\n'\ f'{dup_count} Duplicates found\n' sg.MsgBox('Duplicate Finder Ended', msg)
def EasyProgressMeter(): """ Easy Progress Meter This recipe shows just how easy it is to add a progress meter to your code. """ import PySimpleGUI as sg for i in range(1000): sg.EasyProgressMeter('Easy Meter Example', i+1, 1000)
raise SystemExit() t0 = mytime() # set start timer doc = fitz.open() width, height = fitz.PaperSize("a4") rect = fitz.Rect(0, 0, width, height) + (36, 36, -36, -36) imglist = os.listdir(imgdir) imgcount = len(imglist) for i, f in enumerate(imglist): path = os.path.join(imgdir, f) if not os.path.isfile(path): print("skipping non-file '%s'!" % f) continue if str is not bytes: psg.EasyProgressMeter("Embedding Files", i + 1, imgcount, "dir: " + imgdir, "file: " + f) else: print("embedding file '%s', (%i / %i)" % (f, i + 1, imgcount)) img = open(path, "rb").read() doc.embeddedFileAdd(img, f, filename=f, ufilename=f, desc=f) page = doc.newPage() # every doc needs at least one page doc.save("all-my-pics-embedded.pdf") t1 = mytime() print("%g" % (t1 - t0), "sec processing time")
doc = fitz.open() # PDF with the pictures imglist = os.listdir(imgdir) # list of them imgcount = len(imglist) # pic count for i, f in enumerate(imglist): path = os.path.join(imgdir, f) if not os.path.isfile(path): print("skipping non-file '%s'!" % f) continue if str is not bytes: psg.EasyProgressMeter( "Inserting Images", # show our progress i + 1, imgcount, "dir: " + imgdir, "file: " + f) else: print("inserting file '%s', (%i / %i)" % (f, i + 1, imgcount)) try: img = fitz.open(os.path.join(imgdir, f)) # open pic as document rect = img[0].rect # pic dimension pdfbytes = img.convertToPDF() # make a PDF stream img.close() # no longer needed imgPDF = fitz.open("pdf", pdfbytes) # open stream as PDF page = doc.newPage( width=rect.width, # new page with ... height=rect.height) # pic dimension page.showPDFpage(rect, imgPDF, 0) # image fills the page
# we may need to adjust something for CMYK pixmaps here: return getimage(pix) t0 = time.time() fname = sys.argv[1] doc = fitz.open(fname) page_count = len(doc) # number of pages xreflist = [] imglist = [] for pno in range(page_count): psg.EasyProgressMeter( "Extract Images", # show our progress pno + 1, page_count, "*** Scanning Pages ***") il = doc.getPageImageList(pno) imglist.extend([x[0] for x in il]) for img in il: xref = img[0] if xref in xreflist: continue width = img[2] height = img[3] if min(width, height) <= dimlimit: continue pix = recoverpix(doc, img) if type(pix) is dict: # we got a raw image
# insert header and footer page.insertText(rect.tl, text) page.insertText(rect.bl, "Page %i of %i" % (pno, pages)) point = rect.tl + (0, 20) # insertion point of first symbol for i, f in enumerate(imglist): path = os.path.join(imgdir,f) if not os.path.isfile(path): print("skipping non-file '%s'!" % f) continue if str is not bytes: # show progress meter if Python v3 psg.EasyProgressMeter("Attaching Files", i+1, imgcount, "dir: " + imgdir, "file: " + f) else: print("attaching file '%s', (%i / %i)" % (f, i+1, imgcount)) img = open(path, "rb").read() # file content page.addFileAnnot(point, img, filename=f) # add as attachment point += (25, 0) # position of next symbol if point.x >= rect.width: # beyond right limit? point = fitz.Point(rect.x0, point.y + 35) # start next line if point.y >= rect.height and i < imgcount -1: # beyond bottom limit? # prepare another page page = doc.newPage(width = width, height = height) pno += 1 page.insertText(rect.tl, text)
def ProgressMeter(): for i in range(1, 1000): if not sg.EasyProgressMeter('My Meter', i + 1, 1000, orientation='h'): break time.sleep(.01)
import PySimpleGUI as sg import time for i in range(1, 10): sg.EasyProgressMeter("Test Results Meter", i+1, 10, "Test Results Meter") time.sleep(i)
# display some file info print("") print(__file__, "PDF: %s, pages: %i, objects: %i" % (fname, len(doc), lenXREF - 1)) t0 = time.time() # start the timer smasks = [] # stores xrefs of /SMask objects #------------------------------------------------------------------------------ # loop through PDF images #------------------------------------------------------------------------------ for xref in range(1, lenXREF): # scan through all PDF objects psg.EasyProgressMeter( "Extract Images", # show our progress xref, lenXREF, "*** Scanning Cross Reference ***") if xref in smasks: # ignore smasks continue imgdict = doc.extractImage(xref) if not imgdict: # not an image continue img_icnt += 1 # increase read images counter smask = imgdict["smask"] if smask > 0: # store /SMask xref