def draw_bounding_poly_pdf(
        self,
        pdf: PdfReader,
        vertices: List[Dict],
        color: AnyStr,
    ):
        """Annotate a PDF document by drawing a bounding polygon of a given color

        Args:
            pdf: PDF document opened with pdfrw.PdfReader
            vertices: List of 4 vertices describing the polygon
                Each vertex should a dictionary with normalized coordinates e.g. {"x": 0.1, "y": 0.3}
            color: Name of the color e.g. "red", "teal", "skyblue"
                Full list on https://matplotlib.org/3.3.2/gallery/color/named_colors.html

        """
        if len(vertices) == 4:
            pdf_annotator = PdfAnnotator(pdf)
            pdf_annotator.set_page_dimensions(
                dimensions=(1, 1), page_number=0)  # normalize page dimensions
            pdf_annotator.add_annotation(
                annotation_type="polygon",
                location=Location(
                    points=[(vertices[i].get("x", 0.0),
                             1.0 - vertices[i].get("y", 0.0))
                            for i in range(4)],
                    page=0,
                ),
                appearance=Appearance(stroke_color=colors.to_rgba(color)),
            )
        else:
            raise ValueError(
                f"Bounding polygon does not contain 4 vertices: {vertices}")
        return pdf
async def makePdf(Item: Item):
    pdfBytes = base64.b64decode(Item.pdffile)
    reader = PdfReader(fdata=pdfBytes)
    annotator = PdfAnnotator(reader)
    for i in Item.annotations:
        if i.anType == "line":
            await addLine(annotator, i)
        elif i.anType == 'polyline':
            await addLine(annotator, i)
        elif i.anType == 'highlighter':
            await addHighlight(annotator, i)
        elif i.anType == 'highlighterPolyline':
            await addHighlight(annotator, i)
        elif i.anType == 'text':
            await addText(annotator, i)
        elif i.anType == 'identity':
            await addIdentity(annotator, i)
    # Creating file results
    fileid = uuid.uuid4()
    fileName = str(fileid) + ".pdf"
    annotator.write(fileName)
    encoded_string = ""
    with open(fileName, "rb") as pdf_file:
        encoded_string = base64.b64encode(pdf_file.read())
    os.remove(fileName)
    return {"result": encoded_string}
예제 #3
0
    def produce_debug_pdf(self):
        debug_path = path.join('debug', self.filename.split('/', 1)[1])
        makedirs(path.dirname(debug_path), exist_ok=True)

        annotator = PdfAnnotator(self.filename)
        for shape, loc, appearance in self.annotations:
            annotator.add_annotation(shape, loc, appearance)

        annotator.write(debug_path)
예제 #4
0
def redactDocument(dirname, filename, boxes):
    a = PdfAnnotator(dirname+"/"+filename)
    for i in boxes:
        a.add_annotation('square', Location(x1=i[0]*72, y1=(790-i[1]*72), x2=i[2]*72, y2=(790-i[3]*72), page=0),
        Appearance(stroke_color=(0, 0, 0), stroke_width=13, fill=(0,0,0)),)
    resultFilename = 'Redacted'+filename
    a.write(resultFilename) 
    print("[INFO]: Process Completed.")
    return resultFilename
예제 #5
0
파일: PDFutils.py 프로젝트: j-benoit/Prayon
def pdf_add_Annot_old(path, file, stamp):
    from pdf_annotate import PdfAnnotator, Location, Appearance
    a = PdfAnnotator(os.path.join(path, file))
    a.add_annotation(
        'square',
        Location(x1=50, y1=50, x2=100, y2=100, page=0),
        Appearance(stroke_color=(1, 0, 0), stroke_width=5),
    )
    a.write(os.path.join(
        path, 'new_w_stamp.pdf'))  # or use overwrite=True if you feel lucky
예제 #6
0
def redactDocument(dirname, filename, boxes):
    print("Dirname: " + dirname[0:len(dirname) - 14])
    print("Filename: " + filename)
    dirname = dirname[0:len(dirname) - 14]
    a = PdfAnnotator(dirname+filename)
    for i in boxes:
        a.add_annotation('square', Location(x1=i[0]*72, y1=(790-i[1]*72), x2=i[2]*72, y2=(790-i[3]*72), page=0),
        Appearance(stroke_color=(0, 0, 0), stroke_width=13, fill=(0,0,0)),)
    a.write(dirname + filename) 
    print("[INFO]: Process Completed.")
    return dirname + filename
예제 #7
0
    def annotate(self, annotations, infile, outfile):
        """
        Annotates a file.

        Args:
            annotations: list of annotations (title, rgb color, page #, x1, y1, x2, y2)
            infile: full path to input file
            outfile: full path to output file
        """

        annotator = PdfAnnotator(infile)

        # List of text ranges already defined
        ranges = []

        for title, rgb, page, x1, y1, x2, y2 in annotations:
            # Highlight text
            annotator.add_annotation(
                "square", Location(x1=x1, y1=y1, x2=x2, y2=y2, page=page),
                Appearance(fill=rgb + (0.3, ),
                           stroke_color=rgb + (0.3, ),
                           stroke_width=0))

            if title:
                # Determine if title text should be in left or right margin
                if x1 < 250:
                    x1, x2 = max(5, x1 - 35), x1
                else:
                    x1, x2 = x2, x2 + 35

                # Calculate center of highlight annotation and offset
                center = y1 + ((y2 - y1) / 2)
                offset = min(max(5, len(title)), 20)

                # Set position of text annotation. Handle column layout conflicts.
                y1, y2 = self.position(ranges, page, x1 >= 250, center, offset)

                # Add title annotation next to highlight
                annotator.add_annotation(
                    "text", Location(x1=x1, y1=y1, x2=x2, y2=y2, page=page),
                    Appearance(fill=rgb + (1, ),
                               font_size=7,
                               stroke_width=1,
                               content=title))

                # Register range
                ranges.append((page, 0 if x1 < 250 else 1, y1, y2))

        annotator.write(outfile)
예제 #8
0
    def test_get_page_bounding_box(self):
        a = PdfAnnotator(files.SIMPLE)
        # The simple file has both MediaBox and CropBox set to the same value
        assert a.get_page_bounding_box(0) == [0, 0, 612, 792]

        page = a._pdf.get_page(0)

        # Still works without CropBox
        page.CropBox = None
        assert a.get_page_bounding_box(0) == [0, 0, 612, 792]
        page.MediaBox = [1, 1, 611, 791]
        assert a.get_page_bounding_box(0) == [1, 1, 611, 791]

        # But CropBox takes precedence
        page.CropBox = [12, 16, 600, 792]
        assert a.get_page_bounding_box(0) == [12, 16, 600, 792]
예제 #9
0
    def test_add_annotation_page_dimensions(self):
        # Ensure that changing a page's dimensions results in annotations being
        # placed in the proper locations.
        a = PdfAnnotator(files.SIMPLE)
        # Act like the PDF was rastered at 144 DPI (2x default user space)
        a.set_page_dimensions((1224, 1584), 0)
        a.add_annotation(
            'square',
            Location(x1=10, y1=20, x2=20, y2=30, page=0),
            Appearance(),
        )
        with write_to_temp(a) as t:
            annotations = load_annotations_from_pdf(t)

        square = annotations.pop()
        assert len(annotations) == 0
        assert square.Subtype == '/Square'
        # The outer bounding box of the square is padded outward by the stroke
        # width, and then scaled down by two.
        self.assertEqual(square.Rect, ['4.5', '9.5', '10.5', '15.5'])
예제 #10
0
#!/usr/bin/env vpython3
from pdf_annotate import PdfAnnotator, Location, Appearance

annotationtext = "some text"

a = PdfAnnotator("pdf.pdf")
a.add_annotation(
    "text",
    Location(x1=50, y1=50, x2=200, y2=100, page=0),
    Appearance(
        fill=(0, 0, 0),
        stroke_width=1,
        wrap_text=True,
        font_size=12,
        content=annotationtext,
    ),
)
a.write("pdf-a.pdf")
예제 #11
0
 def test_write_with_compress_off_smoke_test(self):
     a = PdfAnnotator(PdfReader(files.SIMPLE), compress=False)
     with write_to_temp(a) as t:
         assert load_annotations_from_pdf(t) is None
 def test_end_to_end(self):
     a = PdfAnnotator(self.INPUT_FILENAME)
     self._add_annotations(a)
     output_file = self._get_output_file()
     a.write(output_file)
예제 #13
0
    def draw_rectangles_for_solution(self, f_in, f_out, solution, points):
        """Drawing green filled rectangles near the correct answers for every problem,
        in order to indicate the correct solution.
        It calculates and writes the total score achieved too.
        
        Parameters:
            f_in (str): Path to the input PDF file.
            f_out (str): Path to the output PDF file.
            solution (dict): The solution (correct answers) corresponding to the input PDF file (f_in).
            points (float): Total points achieved.
        """
        pr = PdfFileReader(f_in)
        dest = pr.getNamedDestinations()
        fields = pr.getFields()

        # """IT IS NOT WORKING IF THIS COMES FIRST:"""
        #         a = PdfAnnotator(f_in)
        #         for p in range(pr.getNumPages()):
        #             for dk, dv in dest.items():
        #                 if pr.getDestinationPageNumber(dv) == p and dk.startswith('ht_'):
        #                     inds = [int(ind) for ind in dk[3:].split(':')]
        # #                     if inds[2] in solution[inds[1]][1]:
        #                     if inds[4] in solution[inds[1]][1]:
        #                         # using some hard-coded values:
        #                         a.add_annotation('square',
        #                                          Location(x1=float(dv['/Left']), y1=float(dv['/Top']), x2=float(dv['/Left'])+5, y2=float(dv['/Top'])+5, page=p),
        #                                          Appearance(stroke_color=(0, 1, 0), stroke_width=5),)
        #         a.write(f_out)

        pw = PdfFileWriter()
        #         pr = PdfFileReader(f_out, strict=False)
        pr = PdfFileReader(f_in, strict=False)
        pw.appendPagesFromReader(pr)
        pw._root_object.update(
            {NameObject("/AcroForm"): pr.trailer["/Root"]["/AcroForm"]})
        pw._root_object["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})
        for p in range(pr.getNumPages()):
            self._update_page_form_checkbox_values(pw.getPage(p), {
                fk: fv['/V']
                for fk, fv in fields.items() if '/V' in fv.keys()
            })  # sometimes '/V' disappears from the keys
        self._update_page_form_checkbox_values(pw.getPage(0),
                                               {'points': str(points)})
        f = codecs.open(f_out, 'wb')
        pw.write(f)
        f.close()

        a = PdfAnnotator(f_out)
        for p in range(pr.getNumPages()):
            for dk, dv in dest.items():
                if pr.getDestinationPageNumber(dv) == p and dk.startswith(
                        'ht_'):
                    inds = [int(ind) for ind in dk[3:].split(':')]
                    #                     if inds[2] in solution[inds[1]][1]:
                    if inds[4] in solution[inds[1]][1]:
                        # using some hard-coded values:
                        a.add_annotation(
                            'square',
                            Location(x1=float(dv['/Left']),
                                     y1=float(dv['/Top']),
                                     x2=float(dv['/Left']) + 5,
                                     y2=float(dv['/Top']) + 5,
                                     page=p),
                            Appearance(stroke_color=(0, 1, 0), stroke_width=5),
                        )
        a.write(f_out)
예제 #14
0
 def test_get_scale_default(self):
     a = PdfAnnotator(files.SIMPLE)
     assert a.get_scale(0) == (1, 1)
예제 #15
0
    def scrape_pdf(self):
        pdfFileObject = open(os.getcwd() + "/" + self.path, 'rb')
        pdf_info = list(
            filter(
                lambda x: any(c.isalnum() for c in x),
                PyPDF2.PdfFileReader(pdfFileObject).getPage(
                    0).extractText().split('\n')))
        for i, x in enumerate(pdf_info):
            pdf_info[i] = x.strip()

        annotator = PdfAnnotator(os.getcwd() + self.path)
        annotated = False

        self.company_name = pdf_info[1]

        if pdf_info.index('Trans Type') == pdf_info.index('Quantity') - 2:
            self.transaction_type = pdf_info[pdf_info.index('Trans Type') + 1]
        else:
            self.transaction_type = None
            annotator.add_annotation(
                'square',
                Location(x1=80, y1=435, x2=140, y2=490, page=0),
                Appearance(stroke_color=(1, 0, 0), stroke_width=3),
            )
            annotated = True

        if pdf_info.index('Quantity') == pdf_info.index('PPS') - 2:
            self.quantity = int(pdf_info[pdf_info.index('Quantity') +
                                         1].replace(',', ''))
        else:
            self.quantity = None
            annotator.add_annotation(
                'square',
                Location(x1=160, y1=435, x2=220, y2=490, page=0),
                Appearance(stroke_color=(1, 0, 0), stroke_width=3),
            )
            annotated = True

        if pdf_info.index('PPS') == pdf_info.index('Security') - 2:
            self.price_per_share = int(
                pdf_info[pdf_info.index('PPS') +
                         1][1:len(pdf_info[pdf_info.index('PPS') + 1]) - 3])
        else:
            self.price_per_share = None
            annotator.add_annotation(
                'square',
                Location(x1=225, y1=435, x2=285, y2=490, page=0),
                Appearance(stroke_color=(1, 0, 0), stroke_width=3),
            )
            annotated = True

        if len(pdf_info[3]) > 14:
            self.date_of_order = pdf_info[3][15:]
        else:
            print("date of order missing")
            self.date_of_order = None
            annotator.add_annotation(
                'square',
                Location(x1=150, y1=570, x2=280, y2=590, page=0),
                Appearance(stroke_color=(1, 0, 0), stroke_width=3),
            )
            annotated = True

        if len(pdf_info[4]) > 10:
            self.custodian = pdf_info[4][11:]
        else:
            self.custodian = None
            annotator.add_annotation(
                'square',
                Location(x1=130, y1=540, x2=280, y2=565, page=0),
                Appearance(stroke_color=(1, 0, 0), stroke_width=3),
            )
            annotated = True

        if pdf_info.index('Security') == pdf_info.index('Type of Order') - 2:
            self.security = pdf_info[pdf_info.index('Security') + 1]
        else:
            self.security = None
            annotator.add_annotation(
                'square',
                Location(x1=295, y1=435, x2=360, y2=490, page=0),
                Appearance(stroke_color=(1, 0, 0), stroke_width=3),
            )
            annotated = True

        if pdf_info.index(
                'Type of Order') == pdf_info.index('Instructions') - 2:
            self.type = pdf_info[pdf_info.index('Type of Order') + 1]
        else:
            self.type = None
            annotator.add_annotation(
                'square',
                Location(x1=360, y1=435, x2=460, y2=490, page=0),
                Appearance(stroke_color=(1, 0, 0), stroke_width=3),
            )
            annotated = True

        if pdf_info.index('Broker/Dealer') == pdf_info.index(
                'Broker/Dealer Representative') - 2:
            self.broker_dealer = pdf_info[pdf_info.index('Broker/Dealer') + 1]
        else:
            self.broker_dealer = None
            annotator.add_annotation(
                'square',
                Location(x1=70, y1=330, x2=285, y2=400, page=0),
                Appearance(stroke_color=(1, 0, 0), stroke_width=3),
            )
            annotated = True

        if pdf_info.index('Broker/Dealer Representative'
                          ) == pdf_info.index('Comments:') - 2:
            self.broker_dealer_representative = pdf_info[
                pdf_info.index('Broker/Dealer Representative') + 1]
        else:
            self.broker_dealer_representative = None
            annotator.add_annotation(
                'square',
                Location(x1=300, y1=330, x2=500, y2=400, page=0),
                Appearance(stroke_color=(1, 0, 0), stroke_width=3),
            )
            annotated = True

        if pdf_info.index('Phone:') == pdf_info.index('Signature:') - 3:
            self.phone = pdf_info[pdf_info.index('Phone:') +
                                  1] + "-" + pdf_info[pdf_info.index('Phone:')
                                                      + 2]
        else:
            self.phone = None
            annotator.add_annotation(
                'square',
                Location(x1=300, y1=265, x2=500, y2=330, page=0),
                Appearance(stroke_color=(1, 0, 0), stroke_width=3),
            )
            annotated = True

        try:
            self.amount = self.quantity * self.price_per_share
        except:
            self.amount = None

        if annotated:
            # os.mkdir(os.getcwd() + "/ANNOTATED/trade_tickets")
            annotator.write(os.getcwd() + "/ANNOTATED" + self.path)
            # else maybe move it to the completed folder

        self.valid_pdf = self.amount != None and self.transaction_type != None and self.quantity != None
        pdfFileObject.close()
예제 #16
0
 def test_init_with_reader(self):
     a = PdfAnnotator(PdfReader(files.SIMPLE))
     assert a._pdf is not None
예제 #17
0
 def test_get_size(self):
     a = PdfAnnotator(files.SIMPLE)
     size = a.get_size(0)
     assert size == (612.0, 792.0)
예제 #18
0
    def process_drawing_stamps(self):
        try:
            os.mkdir(f'{self.file_path_drawing_to_stamp}/Stamped')
        except Exception as e:
            print(e)
            self.ui.listWidget_drawing_stamper.clear()
            self.ui.listWidget_drawing_stamper.addItem("Unable to create directory - error code below:\n")
            self.ui.listWidget_drawing_stamper.addItem(str(e))

        today = date.today()
        today_date = today.strftime("%d/%m/%y")

        drawing_status = ""
        if self.ui.radioButton_status_a.isChecked() == True:
            drawing_status = "A"
        elif self.ui.radioButton_status_b.isChecked() == True:
            drawing_status = "B"
        elif self.ui.radioButton_status_c.isChecked() == True:
            drawing_status = "C"
        else:
            drawing_status = "Unknown"

        self.ui.listWidget_drawing_stamper.clear()
        self.ui.listWidget_drawing_stamper.addItem("Adding annotations:\n")
        for drawing in self.list_of_drawingstostamp:
            try:
                full_drawing_path = self.file_path_drawing_to_stamp + '/' + drawing
                self.ui.listWidget_drawing_stamper.addItem(drawing)
                QtCore.QCoreApplication.processEvents()
                full_path_drawing_stamp = self.file_path_drawing_to_stamp + "/Blank_Stamp.png"
                a = PdfAnnotator(full_drawing_path)
                a.add_annotation(
                    'image',
                    Location(x1=50, y1=50, x2=400, y2=400, page=0),
                    Appearance(image=full_path_drawing_stamp)
                )
            except Exception as e:
                print("Unable to add image")

            try:
                a.add_annotation(
                    'text',
                    Location(x1=120, y1=320, x2=300, y2=332, page=0),
                    Appearance(stroke_color=(1, 1, 1), stroke_width=5, content=self.ui.lineEdit_drawing_stamper_jobnumber.text(), fill=(0.705, 0.094, 0.125, 1))
                ) #https://doc.instantreality.org/tools/color_calculator/
                a.add_annotation(
                    'text',
                    Location(x1=130, y1=305, x2=300, y2=317, page=0),
                    Appearance(stroke_color=(1, 1, 1), stroke_width=5, content=self.ui.lineEdit_drawing_stamper_date.text(), fill=(0.705, 0.094, 0.125, 1))
                )
                a.add_annotation(
                    'text',
                    Location(x1=75, y1=276, x2=300, y2=288, page=0),
                    Appearance(stroke_color=(1, 1, 1), stroke_width=5, content=self.ui.lineEdit_drawing_stamper_reviewerinitials.text(), fill=(0.705, 0.094, 0.125, 1))
                )
                a.add_annotation(
                    'text',
                    Location(x1=200, y1=276, x2=320, y2=288, page=0),
                    Appearance(stroke_color=(1, 1, 1), stroke_width=5, content=f"Status {drawing_status}", fill=(0.705, 0.094, 0.125, 1))
                )
                a.add_annotation(
                    'text',
                    Location(x1=330, y1=276, x2=400, y2=288, page=0),
                    Appearance(stroke_color=(1, 1, 1), stroke_width=5, content=today_date, fill=(0.705, 0.094, 0.125, 1))
                )

                # Put an X in the box noting the status
                if drawing_status == "A":
                    a.add_annotation(
                        'text',
                        Location(x1=117, y1=203, x2=300, y2=215, page=0),
                        Appearance(stroke_color=(1, 1, 1), stroke_width=5,
                                   content="X", fill=(0.705, 0.094, 0.125, 1))
                    )
                if drawing_status == "B":
                    a.add_annotation(
                        'text',
                        Location(x1=117, y1=189, x2=300, y2=201, page=0),
                        Appearance(stroke_color=(1, 1, 1), stroke_width=5,
                                   content="X", fill=(0.705, 0.094, 0.125, 1))
                    )
                if drawing_status == "C":
                    a.add_annotation(
                        'text',
                        Location(x1=117, y1=174, x2=300, y2=186, page=0),
                        Appearance(stroke_color=(1, 1, 1), stroke_width=5,
                                   content="X", fill=(0.705, 0.094, 0.125, 1))
                    )

            except Exception as e:
                print(e)
                self.ui.listWidget_drawing_stamper.addItem("Unable to add annotation - error code below:\n")
                self.ui.listWidget_drawing_stamper.addItem(str(e))
                self.ui.listWidget_drawing_stamper.addItem("Check - is this file a PDF?")


            try:
                #Write the resultant file
                a.write(f'{self.file_path_drawing_to_stamp}/Stamped/{drawing}')
            except Exception as e:
                print(e)
                self.ui.listWidget_drawing_stamper.addItem("Unable to save file - error code below:\n")
                self.ui.listWidget_drawing_stamper.addItem(str(e))
                self.ui.listWidget_drawing_stamper.addItem("Check - do these files already exist?")
                return

        #Display success message
        self.ui.listWidget_drawing_stamper.clear()
        self.ui.listWidget_drawing_stamper.addItem("Stamps added successfully!")

        return
예제 #19
0
 def test_get_rotated_scale_from_dimensions(self):
     a = PdfAnnotator(files.ROTATED_90)
     a.set_page_dimensions((3300, 1275), 0)
     assert a.get_scale(0) == (0.24, 0.48)
예제 #20
0
 def test_get_scale_from_page_dimensions(self):
     a = PdfAnnotator(files.SIMPLE)
     # Rastered at different X and Y scales. Why would you do that?
     a.set_page_dimensions((1275, 3300), 0)
     assert a.get_scale(0) == (0.48, 0.24)
예제 #21
0
 def test_get_scale_from_init(self):
     a = PdfAnnotator(files.SIMPLE, scale=0.48)
     assert a.get_scale(0) == (0.48, 0.48)
예제 #22
0
from pdfscrape import Scraped_PDF
import PyPDF2
import os
from pdf_annotate import PdfAnnotator, Appearance, Location

annotator = PdfAnnotator(os.getcwd()+'/trade_tickets/Company_Q2_p1.pdf')
annotator.add_annotation('square',Location(x1=300, y1=265, x2=500, y2=330, page=0),Appearance(stroke_color=(1, 0, 0), stroke_width=3),)
annotator.write('annotated.pdf')

class Annotate(object):
    def __init__(self, pdf_obj):
예제 #23
0
def annotatePDF(pathToPDF):
    try:
        randomNumber1 = str(randint(0, 9))
        randomNumber2 = str(randint(1000, 9999))

        a = PdfAnnotator(pathToPDF)
        #Name and adress
        a.add_annotation(
            'text',
            Location(x1=350, y1=590, x2=560, y2=650, page=0),
            Appearance(content=name + "\n" + adress + "\n" + city.upper(),
                       font_size=11,
                       fill=(0, 0, 0),
                       line_spacing=1.8),
        )
        #Edition date
        a.add_annotation(
            'text',
            Location(x1=393, y1=550, x2=570, y2=573, page=0),
            Appearance(content=beautifulDate,
                       font_size=9,
                       fill=(0, 0, 0),
                       line_spacing=1.65),
        )
        #Request number
        a.add_annotation(
            'text',
            Location(x1=1.37 * 72,
                     y1=8.23 * 72,
                     x2=3.58 * 72,
                     y2=8.40 * 72,
                     page=0),
            Appearance(content=date + '-' + randomNumber1 + '-' +
                       randomNumber2,
                       font_size=9,
                       fill=(0, 0, 0),
                       line_spacing=1.65),
        )
        #name and date of birth
        a.add_annotation(
            'text',
            Location(x1=0.73 * 72, y1=8 * 72, x2=2.1 * 72, y2=8.3 * 72,
                     page=0),
            Appearance(content=lastName.upper() + ' le : ' + birthDate,
                       font_size=9,
                       fill=(0, 0, 0),
                       line_spacing=1.5),
        )
        #PrelevementDateTime
        a.add_annotation(
            'text',
            Location(x1=1.32 * 72,
                     y1=7.43 * 72,
                     x2=3 * 72,
                     y2=7.7 * 72,
                     page=0),
            Appearance(content=date + ' . ' + hour,
                       font_size=9,
                       fill=(0, 0, 0),
                       line_spacing=1.1),
        )
        #ValidationDate1
        a.add_annotation(
            'text',
            Location(x1=5.34 * 72,
                     y1=1.75 * 72,
                     x2=5.85 * 72,
                     y2=1.93 * 72,
                     page=0),
            Appearance(content=date,
                       font_size=9,
                       fill=(0, 0, 0),
                       line_spacing=1.1),
        )
        #ValidationDate2
        a.add_annotation(
            'text',
            Location(x1=3.6 * 72,
                     y1=0.45 * 72,
                     x2=4.1 * 72,
                     y2=0.60 * 72,
                     page=0),
            Appearance(content=date,
                       font_size=9,
                       fill=(0, 0, 0),
                       line_spacing=1.1),
        )

        #Create a new file
        a.write(pathToPDF.replace('inputs', 'outputs'))
        return 'Done'
    except Exception as e:
        return e
예제 #24
0
def generate_timesheet(form):
    # Setting default strings
    sunday_in = ''
    monday_in = ''
    tuesday_in = ''
    wednesday_in = ''
    thursday_in = ''
    friday_in = ''
    saturday_in = ''
    sunday_out = ''
    monday_out = ''
    tuesday_out = ''
    wednesday_out = ''
    thursday_out = ''
    friday_out = ''
    saturday_out = ''
    sunday_hours = ''
    monday_hours = ''
    tuesday_hours = ''
    wednesday_hours = ''
    thursday_hours = ''
    friday_hours = ''
    saturday_hours = ''
    fund = ''

    def calc_hours_worked(time_in, time_out):
        hours = datetime.datetime.combine(
            datetime.date.today(), time_out.data) - datetime.datetime.combine(
                datetime.date.today(), time_in.data)
        return round(hours.seconds / 3600, 2)

    def next_saturday(d):
        if d.weekday() == 5:
            return d
        else:
            days_ahead = 5 - d.weekday()
            if days_ahead < 0:
                days_ahead += 7
            return d + datetime.timedelta(days_ahead)

    def previous_sunday(d):
        if d.weekday() == 6:
            return d
        else:
            days_behind = -d.weekday() - 1
            return d + datetime.timedelta(days_behind)

    name = form['name'].data
    mcgill_id = form['mcgill_id'].data
    hourly_rate = round(float(form['hourly_rate'].data), 2)
    if form['fund_number'].data is not None:
        fund = form['fund_number'].data

    week = datetime.date(int(form['week_of_year'].data),
                         int(form['week_of_month'].data),
                         int(form['week_of_day'].data))
    # find the closest past sunday
    week_start = previous_sunday(week)
    # find the closest future saturday
    week_end = next_saturday(week)

    total_hours = 0
    if form['sunday_in'].data is not None and form[
            'sunday_out'].data is not None:
        sunday_in = form['sunday_in'].data.strftime('%H:%M')
        sunday_out = form['sunday_out'].data.strftime('%H:%M')
        sunday_hours = calc_hours_worked(form['sunday_in'], form['sunday_out'])
        total_hours += sunday_hours
    if form['monday_in'].data is not None and form[
            'monday_out'].data is not None:
        monday_in = form['monday_in'].data.strftime('%H:%M')
        monday_out = form['monday_out'].data.strftime('%H:%M')
        monday_hours = calc_hours_worked(form['monday_in'], form['monday_out'])
        total_hours += monday_hours
    if form['tuesday_in'].data is not None and form[
            'tuesday_out'].data is not None:
        tuesday_in = form['tuesday_in'].data.strftime('%H:%M')
        tuesday_out = form['tuesday_out'].data.strftime('%H:%M')
        tuesday_hours = calc_hours_worked(form['tuesday_in'],
                                          form['tuesday_out'])
        total_hours += tuesday_hours
    if form['wednesday_in'].data is not None and form[
            'wednesday_out'].data is not None:
        wednesday_in = form['wednesday_in'].data.strftime('%H:%M')
        wednesday_out = form['wednesday_out'].data.strftime('%H:%M')
        wednesday_hours = calc_hours_worked(form['wednesday_in'],
                                            form['wednesday_out'])
        total_hours += wednesday_hours
    if form['thursday_in'].data is not None and form[
            'thursday_out'].data is not None:
        thursday_in = form['thursday_in'].data.strftime('%H:%M')
        thursday_out = form['thursday_out'].data.strftime('%H:%M')
        thursday_hours = calc_hours_worked(form['thursday_in'],
                                           form['thursday_out'])
        total_hours += thursday_hours
    if form['friday_in'].data is not None and form[
            'friday_out'].data is not None:
        friday_in = form['friday_in'].data.strftime('%H:%M')
        friday_out = form['friday_out'].data.strftime('%H:%M')
        friday_hours = calc_hours_worked(form['friday_in'], form['friday_out'])
        total_hours += friday_hours
    if form['saturday_in'].data is not None and form[
            'saturday_out'].data is not None:
        saturday_in = form['saturday_in'].data.strftime('%H:%M')
        saturday_out = form['saturday_out'].data.strftime('%H:%M')
        saturday_hours = calc_hours_worked(form['saturday_in'],
                                           form['saturday_out'])
        total_hours += saturday_hours

    total_money = round(total_hours * hourly_rate, 2)

    if form['timesheet_template'].data == '1':
        base_timesheet = 'academic_casual_timesheet_-_2017_0.pdf'
        annotations = constants.annotations_academic_casual_timesheet
        personal_data = constants.personal_data_academic_casual_timesheet
    else:
        base_timesheet = 'admin_support_staff_casual_employee_timesheet_-_2017.pdf'
        annotations = constants.annotations_admin_support_staff_casual_employee_timesheet
        personal_data = constants.personal_data_admin_support_staff_casual_employee_timesheet

    pdf_out_dir = 'pdf/'
    out_file = form['name'].data + ' ' + week_start.strftime(
        '%d %b %Y') + '.pdf'
    # out_file = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(32)]) + '.pdf'
    out_path = pdf_out_dir + out_file

    department = 'Building 21'
    signature_date = datetime.date.today()

    timesheet = PdfAnnotator(base_timesheet)

    for annotation in annotations:
        x1 = annotation[1]
        y1 = annotation[2]
        x2 = x1 + annotation[3]
        y2 = y1 + annotation[4]
        timesheet.add_annotation(
            'text',
            Location(x1=x1, y1=y1, x2=x2, y2=y2, page=0),
            Appearance(content=str(eval(annotation[0])),
                       fill=(0, 0, 0),
                       text_baseline='bottom'),
        )

    for annotation in personal_data:
        x1 = annotation[1]
        y1 = annotation[2]
        x2 = x1 + annotation[3]
        y2 = y1 + annotation[4]
        timesheet.add_annotation(
            'text',
            Location(x1=x1, y1=y1, x2=x2, y2=y2, page=0),
            Appearance(content=str(eval(annotation[0])),
                       fill=(0, 0, 0),
                       text_baseline='middle',
                       text_align='center'),
        )

    timesheet.write('static/' + out_path)

    return out_path
예제 #25
0
]
dim_geschoss = [
    [1447.5 - 2 * 18, 1049],
    [1447.5 - 2 * 18, 1209 - 17 - 18],
    [1447.5 - 2 * 18, 883 + 292.5 + 31.5],
]
file_name = ["UG", "EG", "DG"]
yaml_file = "data/setup.yaml"
haus = read_setup(yaml_file)
for i in range(0, 3):
    geschoss = haus.geschosse[i]
    file_name_file = "{}.pdf".format(file_name[i])
    file_name_full = "data/plaene/{}".format(file_name_file)
    print("Reading:{} and storing to:{}".format(file_name_full,
                                                file_name_file))
    a = PdfAnnotator(file_name_full)
    a.set_page_dimensions((pdf_dim[i][0], pdf_dim[i][1]), 0)
    pos_x1 = pdf_dim[i][2]
    pos_x2 = pdf_dim[i][3]
    pos_y1 = pdf_dim[i][4]
    pos_y2 = pdf_dim[i][5]
    for wall in geschoss.walls:

        x = (wall.x) / (dim_geschoss[i][0]) * (pos_x2 - pos_x1) + pos_x1
        y = (wall.y) / (dim_geschoss[i][1]) * (pos_y1 - pos_y2) + pos_y2
        x2 = (wall.x + wall.dx) / (dim_geschoss[i][0]) * (pos_x2 -
                                                          pos_x1) + pos_x1
        y2 = (wall.y + wall.dy) / (dim_geschoss[i][1]) * (pos_y1 -
                                                          pos_y2) + pos_y2
        a.add_annotation(
            "square",
예제 #26
0
파일: mian.py 프로젝트: guoyanz/ReadSpec
import re
import io
import sys


start_time = time.time()
check_version = "developer"
test_times = 1
# Open a PDF file.
if check_version == "developer":
    spec_number = r"acrf_CRAD001Y2301-SPEC"
    pdf_path = r"F:\Novartis\ReaderSpec\files/" + spec_number + '.pdf'
    test_times = test_times + 1
    output_path = r"F:\Novartis\ReaderSpec\result/" + spec_number + "_" + str(test_times) + ".xlsx"
    offset = 1.5
    annotator = PdfAnnotator(pdf_path)
else:
    offset = 1.5

# Read the PDF with pdfplumber.
df = pd.DataFrame()
with pdfplumber.open(pdf_path) as pdf:
    all_pages = pdf.pages
    for curpage in range(len(all_pages)):
        page = all_pages[curpage]
        axis_info_ = page.tmp_find_tables(table_settings={"keep_blank_chars": True})
        counttable = 0
        for table in page.extract_tables(table_settings={"keep_blank_chars": True}):
            axis_info_1 = axis_info_[counttable]
            counttable += 1
            header_index = 0
예제 #27
0
from pdf_annotate import PdfAnnotator, Location, Appearance

a = PdfAnnotator('template.pdf')


def create_annotation(node: PdfAnnotator, is_name: bool, count: int, value):
    coordinate_dict_name = {
        '1': {
            'x1': 95,
            'y1': 720,
            'x2': 160,
            'y2': 740
        },
        '2': {
            'x1': 335,
            'y1': 720,
            'x2': 405,
            'y2': 740
        },
        '3': {
            'x1': 95,
            'y1': 690,
            'x2': 160,
            'y2': 710
        },
        '4': {
            'x1': 335,
            'y1': 690,
            'x2': 405,
            'y2': 710
        }
예제 #28
0
 def test_get_rotation(self):
     assert PdfAnnotator(files.SIMPLE).get_rotation(0) == 0
     assert PdfAnnotator(files.ROTATED_90).get_rotation(0) == 90