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}
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)
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
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
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
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)
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()
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
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):
#!/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")
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)
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)
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
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
# def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict): # template_pdf = pdfrw.PdfReader(input_pdf_path) # annotations = template_pdf.pages[0][ANNOT_KEY] # for annotation in annotations: # if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY: # if annotation[ANNOT_FIELD_KEY]: # key = annotation[ANNOT_FIELD_KEY][1:-1] # if key in data_dict.keys(): # annotation.update( # pdfrw.PdfDict(AP='', V='{}'.format(data_dict[key])) # ) # pdfrw.PdfWriter().write(output_pdf_path, template_pdf) folder_directory = Path('results') a.write(folder_directory / '{}.pdf'.format(slugify(row[0].value))) unflattened = pdfrw.PdfReader(folder_directory / '{}.pdf'.format(slugify(row[0].value))) for annotation in unflattened.pages[0]['/Annots']: annotation.update(pdfrw.PdfDict(Ff=1)) pdfrw.PdfWriter().write( folder_directory / '{}.pdf'.format(slugify(row[0].value)), unflattened) # pdf_writer.addPage(pdf_reader.getPage(0)) # page = pdf_writer.getPage(0) # # update form fields # pdf_writer.updatePageFormFieldValues(page, data_dict) # for j in range(0, len(page['/Annots'])): # writer_annot = page['/Annots'][j].getObject() # for field in data_dict:
elif obj.print_name in ["Licht", "Led", "LedStrip"]: pic_file = "licht.png" elif obj.print_name == "Kontakt": pic_file = "kontakt.png" elif obj.print_name == "Netzwerk": pic_file = "netzwerk.png" else: raise RuntimeError("Unknown Type of Object {}".format( obj.print_name)) a.add_annotation( "image", Location(x1=x - 150, y1=y - 150, x2=(x + 150), y2=y + 150, page=0), Appearance( image="data/plaene/schaltsymbole/{}".format(pic_file)), ) # a.add_annotation( # "circle", # Location(x1=x - r, y1=y - r, x2=x + r, y2=y + r, page=0), # Appearance(stroke_color=color, stroke_width=1), # ) # a.add_annotation( # "line", # Location(points=[[x - r, y - r], [x + r, y + r]], page=0), # Appearance(stroke_color=color, stroke_width=1), # ) a.write(file_name_file) # or use overwrite=True if you feel lucky