Python Forum
Highlight text with Reportlab
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Highlight text with Reportlab
#1
Hi to all,
I am programming a project that make several PDFs with Python + Reportlab.

Before the program, I worked manually creating word files, where I highlighted the keywords that I found only in certain positions and not all the keywords in the text.

I can create PDFs with the text but on Reportlab there is no way to highlight the text that I print and that respects certain conditions.

I would like to understand if anyone has solved this problem or has any ideas.

Many thanks
Reply
#2
I am coding a general function() that print a string if highlighted paramether is True,
paramether:
filename, pos_x, pos_y, dim_char, text_to_print, bolded, highlighted, high_color

This is one way but calling this function for more text not is performing for speed.
Reply
#3
I think you need to make yourself a "highlight function“,by calculating from the size of the font, then determining the size of the rectangle which would cover that, then draw a rectangle with the fill colour you want, then draw your text.

I did not calculate the necessary size of the rectangle here, because I am not sure how! I have done that before, but it was a while ago!

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4

path2pdf = 'reportlab/pdfs/hello.pdf'

def hello(c):
    c.setFillColorRGB(1,1,1) # white text
    c.setFont("Helvetica", 30)
    c.drawString(100,100,"Hello World", )

def background(c):
    c.setFillColorRGB(1,0,0) # red background
    c.rect(95,95,165,35, stroke=0, fill=1)

c = canvas.Canvas(path2pdf, pagesize=A4)
background(c)
hello(c)
c.showPage() # closes the page, can't add more to this page
c.save()
reportlab is marvellous, but you need to work with it regularly to do things easily!

See the reportlab docs here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Reportlab - change a single page orientation beetlecad 1 295 Aug-16-2024, 05:43 PM
Last Post: beetlecad
  [SOLVED] Looking for documentation on Reportlab's canvas.transform() function NeilUK 1 929 Aug-23-2023, 01:21 PM
Last Post: NeilUK
  highlight the columns and capture amy83 0 977 Mar-24-2023, 07:19 PM
Last Post: amy83
  Right to left alignment in python report using Reportlab jalal0034 1 2,269 Sep-27-2022, 04:25 AM
Last Post: jalal0034
  Label Maker FPDF, Reportlab jamesaarr 1 3,043 Aug-09-2021, 11:57 PM
Last Post: Pedroski55
  cyrillic symbols in tables in reportlab. hiroz 5 12,545 Sep-10-2020, 04:57 AM
Last Post: bradmalcom
  Using Reportlab to create a landscape pdf SmukasPlays 2 6,112 Aug-09-2020, 09:31 PM
Last Post: SmukasPlays
  Help! - How to create a Title for a Reportlab Table crabbylou 0 5,745 Mar-29-2020, 09:14 PM
Last Post: crabbylou
  ReportLab Polypop77 0 2,029 Mar-20-2020, 01:17 PM
Last Post: Polypop77
  How to compare two columns and highlight the unique values of column two using pandas shubhamjainj 0 4,584 Feb-24-2020, 06:19 AM
Last Post: shubhamjainj

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020