GreenCoder90
GreenCoder90

Reputation: 363

Python String Wrap

I want to wrap the string at 30,700 in this script. What is the best way of doing this, I have tried using textWrap but it does not seem to work. This is my code:

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

canvas = canvas.Canvas("Forensic Report.pdf", pagesize=letter)
canvas.setLineWidth(.3)
canvas.setFont('Helvetica', 12)

canvas.drawString(30,750,'LYIT MOBILE FORENSICS DIVISION')
canvas.drawString(500,750,"Date: 12/02/2018")
canvas.line(500,747,595,747)

canvas.drawString(500,725,'Case Number:')
canvas.drawString(580,725,"10")
canvas.line(500,723,595,723)


canvas.drawString(30, 700, 'This forensic report has been compiled by the forensic examiner in conclusion to the investigation into the RTA case which occured on 23/01/2018')


canvas.save()
print("Forensic Report Generated")

Upvotes: 0

Views: 1321

Answers (2)

Rebel Rae
Rebel Rae

Reputation: 131

You should wrap the string itself using a function and draw what's returned. To get a properly wrapped string you could specify a line length like so:

def wrap(string, length):
    if len(string) < length:
        return string
    return string[:length:] + '\n' + wrap(string[length::], length)

The wrap function first checks if the string length is less than the specified length and immediately returns it if so. If it's longer then it appends a newline character to the end of a substring up to length, plus sends the remainder of the string to the wrap() function to check the rest.

Running:

string = "This is a really super duper long string from some forensic report and should take up a lot of space..."
length = 20
print(wrap(string, length))

Will print out:

This is a really sup
er duper long string
 from some forensic 
report and should ta
ke up a lot of space
...

Because you PROBABLY don't want every single line to be truncated at the line width, we can fix this by adding another recursive function to check for the most recent whitespace character like so:

def seek(string, index):
    if string[index-1] == ' ':
        return index
    return seek(string, index-1)

The seek() will return the index of the last whitespace character of any string (substring in this case).

Note: seek() HAS to check the previous character string[index-1] otherwise will give you the index of the space character and wrap() will append it to each new line.

You can then modify the wrap() function like so:

def wrap(string, length):
    if len(string) < length:
        return string
    pos = seek(string, length)
    return string[:pos:] + '\n' + wrap(string[pos::], length)

Running:

string = "This is a really super duper long string from some forensic report and should take up a lot of space..."
length = 20
print(wrap(string, length))

Prints out:

This is a really 
super duper long 
string from some 
forensic report and 
should take up a 
lot of space...

Upvotes: 0

Mattia Paterna
Mattia Paterna

Reputation: 1356

Perhaps you want to use the drawText? Doing so, your code will be

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

canvas = canvas.Canvas("Forensic Report.pdf", pagesize=letter)
canvas.setLineWidth(.3)
canvas.setFont('Helvetica', 12)
canvas.drawString(30,750,'LYIT MOBILE FORENSICS DIVISION')
canvas.drawString(500,750,"Date: 12/02/2018")
canvas.line(500,747,595,747)

canvas.drawString(500,725,'Case Number:')
canvas.drawString(580,725,"10")
canvas.line(500,723,595,723)

line1 = 'This forensic report has been compiled by the forensic'
line2 = 'examiner in conclusion to the investigation into the RTA'
line3 = 'case which occured on 23/01/2018'
textobject = canvas.beginText(30, 700)
lines = [line1, line2, line3]
for line in lines:
    textobject.textLine(line)

canvas.drawText(textobject)
canvas.save()

This is also the solution suggested here. Unfortunately, I do not see it as a valid solution for automatic text wrapping in new lines, i.e. you should manage how to split the string yourself.

Upvotes: 0

Related Questions