def _build_table_for_sentence(self, sentence: str) -> Table: t = Table(number_of_columns=len(sentence), number_of_rows=3) for c in sentence: if c in [".", "?", "!", ",", " "]: t.add( TableCell( Paragraph(c, respect_spaces_in_text=True), background_color=X11Color("SlateGray"), ) ) else: num = ord(c.upper()) - ord("A") + 1 t.add( Paragraph( str(num), font_size=Decimal(6), text_alignment=Alignment.CENTERED, ) ) for c in sentence: t.add(Paragraph(" ", respect_spaces_in_text=True)) for c in sentence: t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), border_top=False, border_left=False, border_right=False, ) ) t.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) return t
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) N = 4 colors = [ HSVColor(Decimal(x / 360.0), Decimal(1), Decimal(1)) for x in range(0, 360, int(360 / N)) ] t = Table(number_of_rows=N, number_of_columns=N) for i in range(0, N): for _ in range(0, N): t.add( TableCell( Shape( points=BlobFactory.blob(i + 3), stroke_color=colors[i], fill_color=None, line_width=Decimal(1), horizontal_alignment=Alignment.CENTERED, ).scale_up(Decimal(100), Decimal(100)))) t.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) layout.add(t) # determine output location out_file = self.output_dir / "output.pdf" # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): sentences = [ "THE BOAT WILL ARRIVE ON MONDAY", "SHE LIVES AT THE HOUSE WITH THE BLUE DOOR", "A FRIEND IN NEED IS A FRIEND INDEED", "AN APPLE A DAY KEEPS THE DOCTOR AWAY", ] pdf = Document() page = Page() pdf.append_page(page) # layout layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Reverse the words", font_size=Decimal(20), font_color=X11Color("YellowGreen"), )) # add text layout.add( Paragraph( """ This is perhaps the simplest code to use and solve. Simply read each word backwards. """, font_color=X11Color("SlateGray"), font_size=Decimal(8), )) # add grid t = Table( number_of_rows=len(sentences) * 2, number_of_columns=2, column_widths=[Decimal(1), Decimal(9)], ) for i, s in enumerate(sentences): # code word coded_sentence = "".join([ "".join([y for y in reversed(x)]) + " " for x in s.split(" ") ]) t.add( TableCell( Paragraph(str(i + 1) + "."), border_top=False, border_right=False, border_left=False, border_bottom=False, row_span=2, )) t.add( TableCell( Paragraph(coded_sentence, respect_spaces_in_text=True), border_top=False, border_right=False, border_left=False, border_bottom=False, )) t.add( TableCell( Paragraph(".."), border_top=False, border_right=False, border_left=False, border_bottom=True, )) t.set_padding_on_all_cells(Decimal(15), Decimal(5), Decimal(5), Decimal(5)) layout.add(t) # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def __init__(self, image: typing.Union[str, PILImage.Image]): if isinstance(image, str): image = PILImage.open(requests.get( image, stream=True, ).raw) assert isinstance(image, PILImage.Image) # include solution self.include_solution = False # convert to RGB image = Nonogram._convert_png_to_jpg(image) # scale if needed if image.width * image.height > (20 * 20): scale = 20 / max(image.width, image.height) image = image.resize( (int(image.width * scale), int(image.height * scale))) clues_horizontal = [[0] for _ in range(0, image.height)] clues_vertical = [[0] for _ in range(0, image.width)] pixels = image.load() for i in range(0, image.height): for j in range(0, image.width): white_dist = self._dist(pixels[(j, i)], (255, 255, 255)) black_dist = self._dist(pixels[(j, i)], (0, 0, 0)) if black_dist < white_dist: clues_horizontal[i][-1] += 1 else: if clues_horizontal[i][-1] != 0: clues_horizontal[i].append(0) solution = [[False for _ in range(0, image.width + 1)] for _ in range(0, image.height + 1)] for i in range(0, image.width): for j in range(0, image.height): white_dist = self._dist(pixels[(i, j)], (255, 255, 255)) black_dist = self._dist(pixels[(i, j)], (0, 0, 0)) if black_dist < white_dist: clues_vertical[i][-1] += 1 solution[j][i] = True else: if clues_vertical[i][-1] != 0: clues_vertical[i].append(0) clues_horizontal = [ x if x[-1] != 0 or len(x) == 1 else x[:-1] for x in clues_horizontal ] clues_vertical = [ x if x[-1] != 0 or len(x) == 1 else x[:-1] for x in clues_vertical ] super(Nonogram, self).__init__( number_of_columns=image.width + 1, number_of_rows=image.height + 1, column_widths=[ Decimal(5) if x == 0 else Decimal(1) for x in range(0, image.width + 1) ], ) self.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), border_top=False, border_left=False, )) for c in clues_vertical: txt = "".join([str(x) + "\n" for x in c])[:-1] self.add( TableCell( Paragraph( txt, font_size=Decimal(4), horizontal_alignment=Alignment.CENTERED, respect_newlines_in_text=True, ), padding_top=Decimal(2), padding_bottom=Decimal(2), )) # add clues for i, c in enumerate(clues_horizontal): txt = "".join([str(x) + " " for x in c])[:-1] self.add( TableCell( Paragraph( txt, horizontal_alignment=Alignment.CENTERED, font_size=Decimal(4), ), padding_top=Decimal(2), padding_bottom=Decimal(5), )) for j in range(0, image.width): if solution[i][j] and self.include_solution: self.add( TableCell(Paragraph("X"), background_color=X11Color("Black"))) else: self.add(Paragraph("")) # set border width self.set_border_width_on_all_cells(Decimal(0.2))
def __init__( self, desired_sequence: typing.List[str], random_elements: typing.List[str], start_image: str, end_image: str, ): self.accepted_path = None # build grid d = int(math.sqrt(len(desired_sequence)) + 3) self._generate_grid( accepted_starts=[ Point(2, 0), Point(2, 1), Point(0, 2), Point(1, 2) ], accepted_ends=[ Point(d - 1, d - 3), Point(d - 2, d - 3), Point(d - 3, d - 1), Point(d - 3, d - 2), ], forbidden=[ Point(0, 0), Point(1, 0), Point(0, 1), Point(1, 1), Point(d - 2, d - 2), Point(d - 2, d - 1), Point(d - 1, d - 2), Point(d - 1, d - 1), ], path=[], desired_path_length=len(desired_sequence), bound=d, ) # build table super(SteppingStonePuzzle, self).__init__(number_of_columns=d, number_of_rows=d) # set cells for i in range(0, d): for j in range(0, d): if i == j == 0: self.add( TableCell( Image(start_image, width=Decimal(32), height=Decimal(32)), row_span=2, col_span=2, )) continue if i <= 1 and j <= 1: continue if i == j == d - 2: self.add( TableCell( Image(end_image, width=Decimal(32), height=Decimal(32)), row_span=2, col_span=2, )) continue if i >= d - 2 and j >= d - 2: continue if Point(i, j) in self.accepted_path: index = self.accepted_path.index(Point(i, j)) self.add( Paragraph( desired_sequence[index], horizontal_alignment=Alignment.CENTERED, )) else: self.add( Paragraph( random.choice(random_elements), horizontal_alignment=Alignment.CENTERED, )) # no border self.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) self.no_borders()
def _build_itemized_description_table(self) -> LayoutElement: table_001: Table = Table(number_of_rows=15, number_of_columns=4) for h in ["DESCRIPTION", "QTY", "UNIT PRICE", "AMOUNT"]: table_001.add( TableCell( Paragraph(h, font_color=X11Color("White")), background_color=HexColor("016934"), )) odd_color: Color = HexColor("BBBBBB") even_color: Color = HexColor("FFFFFF") for row_number, item in enumerate([("Product 1", 2, 50), ("Product 2", 4, 60), ("Labor", 14, 60)]): c = even_color if row_number % 2 == 0 else odd_color table_001.add(TableCell(Paragraph(item[0]), background_color=c)) table_001.add( TableCell(Paragraph(str(item[1])), background_color=c)) table_001.add( TableCell(Paragraph("$ " + str(item[2])), background_color=c)) table_001.add( TableCell(Paragraph("$ " + str(item[1] * item[2])), background_color=c)) for row_number in range(3, 10): c = even_color if row_number % 2 == 0 else odd_color for _ in range(0, 4): table_001.add(TableCell(Paragraph(" "), background_color=c)) table_001.add( TableCell( Paragraph( "Subtotal", font="Helvetica-Bold", horizontal_alignment=Alignment.RIGHT, ), col_span=3, )) table_001.add( TableCell( Paragraph("$ 1,180.00", horizontal_alignment=Alignment.RIGHT))) table_001.add( TableCell( Paragraph( "Discounts", font="Helvetica-Bold", horizontal_alignment=Alignment.RIGHT, ), col_span=3, )) table_001.add( TableCell( Paragraph("$ 177.00", horizontal_alignment=Alignment.RIGHT))) table_001.add( TableCell( Paragraph("Taxes", font="Helvetica-Bold", horizontal_alignment=Alignment.RIGHT), col_span=3, )) table_001.add( TableCell( Paragraph("$ 100.30", horizontal_alignment=Alignment.RIGHT))) table_001.add( TableCell( Paragraph("Total", font="Helvetica-Bold", horizontal_alignment=Alignment.RIGHT), col_span=3, )) table_001.add( TableCell( Paragraph("$ 1163.30", horizontal_alignment=Alignment.RIGHT))) table_001.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) table_001.no_borders() return table_001
def test_write_document(self): words = [ "COW", "PUPPY", "TURTLE", "PARROT", "SNAKE", "GOLDFISH", "HAMSTER", "KITTEN", "TURKEY", "DOVE", "HORSE", "BEE" "RABBIT", "DUCK", "SHRIMP", "PIG", "GOAT", "CRAB", "DOG", "DEER", "CAT", "MOUSE", "ELEPHANT", "LION", "PENGUIN", "SPARROW", "STORK", "HAWK", ] pdf = Document() page = Page() pdf.append_page(page) # layout layout = MultiColumnLayout(page, number_of_columns=2) # add title layout.add( Paragraph( """ Word scrambles or anagrams are an excellent way of helping children with their spelling as they have to recognise letter patterns. They are also a fun way of testing knowledge on a subject. We have word scrambles on lots of topics ready to print for home or classroom. """, font_color=X11Color("SlateGray"), font_size=Decimal(8), )) # add table t = Table(number_of_rows=len(words), number_of_columns=2) for i, w in enumerate(words): # shuffle word permuted_word = w while permuted_word == w: letters = [x for x in w] random.shuffle(letters) permuted_word = "".join([x for x in letters]) t.add( TableCell( Paragraph(str(i + 1) + ". " + permuted_word), border_top=False, border_right=False, border_left=False, border_bottom=False, )) # empty column t.add( TableCell( Paragraph(""), border_top=False, border_right=False, border_left=False, )) layout.add(t) # go to next column layout.switch_to_next_column() # add title layout.add( Paragraph( "Word Scramble", font_size=Decimal(20), font_color=X11Color("YellowGreen"), )) # add Image layout.add( Image( "https://www.how-to-draw-funny-cartoons.com/images/cartoon-tree-012.jpg" )) # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) layout = SingleColumnLayout(page) # add title layout.add( Paragraph( "Count The Pictures Puzzle", font_size=Decimal(20), font_color=X11Color("YellowGreen"), ) ) # add explanation layout.add( Paragraph( """ These simple "match up" puzzles help children with observation skills. They will also need to learn a way of marking or remembering which items they have matched, so that they can identify the odd ones out. If you would like to reuse puzzles you could place counters on each "pair" that your child finds, perhaps.""", font_color=X11Color("SlateGray"), font_size=Decimal(8), ) ) # random locations for each image imgs = [ "https://icons.iconarchive.com/icons/chanut/role-playing/128/Orc-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/King-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Knight-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Medusa-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Monster-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Sorceress-Witch-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Centaur-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Elf-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Poison-Spider-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Unicorn-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Viking-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Villager-icon.png", "https://icons.iconarchive.com/icons/chanut/role-playing/128/Dragon-Egg-icon.png", ] N = 32 w = int(sqrt(N) + 2) random.shuffle(imgs) image_positions: typing.Dict[int, str] = {} images_used = [] for i in [random.randint(0, len(imgs)) for _ in range(0, N)]: i = (i + len(imgs)) % len(imgs) # place image p0 = random.randint(0, w ** 2) while p0 in image_positions: p0 = random.randint(0, N ** 2) url = imgs[i] if url not in images_used: images_used.append(url) image_positions[p0] = url t = Table(number_of_rows=w, number_of_columns=w) for i in range(0, w ** 2): if i in image_positions: t.add(Image(image_positions[i], width=Decimal(32), height=Decimal(32))) else: t.add(Paragraph(" ", respect_spaces_in_text=True)) t.no_borders() t.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) layout.add(t) # write count table for _ in range(0, 5): layout.add(Paragraph(" ", respect_spaces_in_text=True)) t2 = Table(number_of_rows=1, number_of_columns=len(images_used) * 2) for url in images_used: t2.add( TableCell( Image(url, width=Decimal(16), height=Decimal(15)), border_top=False, border_right=False, border_bottom=False, border_left=False, ) ) t2.add(Paragraph(" ", respect_spaces_in_text=True)) t2.set_padding_on_all_cells(Decimal(2), Decimal(2), Decimal(2), Decimal(2)) layout.add(t2) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) t = Table(number_of_rows=20, number_of_columns=20) colors = [ HSVColor(Decimal(x / 360), Decimal(1), Decimal(1)) for x in range(0, 360, int(360 / 20)) ] for i in range(0, 18 * 20): t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), background_color=colors[i % len(colors)], )) for i in range(0, 20): t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), background_color=colors[i % len(colors)].darker(), )) for i in range(0, 20): t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), background_color=colors[i % len(colors)].darker().darker(), )) t.no_borders() t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) table_rect = t.layout( page, bounding_box=Rectangle(Decimal(20), Decimal(600), Decimal(500), Decimal(200)), ) Paragraph( text="Love is love", font_size=Decimal(8), font_color=X11Color("Gray"), horizontal_alignment=Alignment.RIGHT, ).layout( page, bounding_box=Rectangle(Decimal(20), table_rect.y - 40, table_rect.width, Decimal(20)), ) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)
def test_write_document(self): # create output directory if it does not exist yet if not self.output_dir.exists(): self.output_dir.mkdir() # create document pdf = Document() # add page page = Page() pdf.append_page(page) t = Table(number_of_rows=5, number_of_columns=3) t.add( TableCell( Paragraph(" ", respect_spaces_in_text=True), border_top=False, border_left=False, ) ) t.add( Paragraph( "Language", font_color=X11Color("SteelBlue"), font_size=Decimal(20) ) ) t.add( Paragraph( "Nof. Questions", font_color=X11Color("SteelBlue"), font_size=Decimal(20), ) ) t.add( TableCell( Paragraph("front-end", font_color=X11Color("SteelBlue")), row_span=2 ) ) t.add(Paragraph("Javascript")) t.add(Paragraph("2,167,178")) t.add(Paragraph("Php")) t.add(Paragraph("1,391,524")) t.add( TableCell( Paragraph("back-end", font_color=X11Color("SteelBlue")), row_span=2 ) ) t.add(Paragraph("C++")) t.add(Paragraph("711,944")) t.add(Paragraph("Java")) t.add(Paragraph("1,752,877")) t.set_border_width_on_all_cells(Decimal(0.2)) t.set_padding_on_all_cells(Decimal(5), Decimal(5), Decimal(5), Decimal(5)) table_rect = t.layout( page, bounding_box=Rectangle( Decimal(20), Decimal(600), Decimal(500), Decimal(200) ), ) Paragraph( text="**Data gathered from Stackoverflow.com on 10th of february 2021", font_size=Decimal(8), font_color=X11Color("Gray"), ).layout( page, bounding_box=Rectangle( Decimal(20), table_rect.y - 40, table_rect.width, Decimal(20) ), ) # determine output location out_file = self.output_dir / ("output.pdf") # attempt to store PDF with open(out_file, "wb") as in_file_handle: PDF.dumps(in_file_handle, pdf) # attempt to re-open PDF with open(out_file, "rb") as in_file_handle: PDF.loads(in_file_handle)