I am trying to work with 9 column csv data , some of which has links and big texts. I am using reportlab to export my data to pdf.
Here is my function:
def pdfGen(main_data_csv):
elements = []
# PDF Text
# PDF Text - Styles
styles = getSampleStyleSheet()
styleNormal = styles['Normal']
# PDF Text - Content
line1 = 'LYIT MOBILE FORENSICS DIVISION'
line2 = 'Date: {}'.format(datetime.datetime.now().strftime("%d-%m-%y"))
line3 = 'Case Number: 10'
line4 = 'This forensic report on sms card data has been compiled by the forensic'
line5 = 'examiner in conclusion to the investigation into the RTA'
line6 = 'case which occurred on 23/01/2018.'
elements.append(Paragraph(line1, styleNormal))
elements.append(Paragraph(line2, styleNormal))
elements.append(Paragraph(line3, styleNormal))
elements.append(Spacer(inch, .25 * inch))
elements.append(Paragraph(line4, styleNormal))
elements.append(Paragraph(line5, styleNormal))
elements.append(Paragraph(line6, styleNormal))
elements.append(Spacer(inch, .25 * inch))
# PDF Table
# PDF Table - Styles
# [(start_column, start_row), (end_column, end_row)]
all_cells = [(0, 0), (-1, -1)]
header = [(0, 0), (-1, 0)]
column0 = [(0, 0), (0, -1)]
column1 = [(1, 0), (1, -1)]
column2 = [(2, 0), (2, -1)]
column3 = [(3, 0), (3, -1)]
column4 = [(4, 0), (4, -1)]
column5 = [(5, 0), (5, -1)]
column6 = [(6, 0), (6, -1)]
column7 = [(7, 0), (7, -1)]
column8 = [(8, 0), (8, -1)]
table_style = TableStyle([
('VALIGN', all_cells[0], all_cells[1], 'TOP'),
('LINEBELOW', header[0], header[1], 1, colors.black),
('ALIGN', column0[0], column0[1], 'LEFT'),
('ALIGN', column1[0], column1[1], 'LEFT'),
('ALIGN', column2[0], column2[1], 'LEFT'),
('ALIGN', column3[0], column3[1], 'RIGHT'),
('ALIGN', column4[0], column4[1], 'RIGHT'),
('ALIGN', column5[0], column5[1], 'LEFT'),
('ALIGN', column6[0], column6[1], 'RIGHT'),
('ALIGN', column7[0], column7[1], 'RIGHT'),
('ALIGN', column8[0], column8[1], 'RIGHT'),
])
# PDF Table - Column Widths
colWidths = [
2.7 * cm, # Column 0
3.1 * cm, # Column 1
3.7 * cm, # Column 2
1.2 * cm, # Column 3
2.5 * cm, # Column 4
2 * cm, # Column 5
1.1 * cm, # Column 6
1.1 * cm, # Column 7
1.1 * cm, # Column 8
]
# PDF Table - Strip '[]() and add word wrap to column 5
for index, row in enumerate(main_data_csv):
for col, val in enumerate(row):
if col != 5 or index == 0:
main_data_csv[index][col] = val.strip("'[]()")
else:
main_data_csv[index][col] = Paragraph(val, styles['Normal'])
# Add table to elements
t = Table(main_data_csv, colWidths=colWidths )
t.setStyle(table_style)
elements.append(t)
# Generate PDF
archivo_pdf = SimpleDocTemplate(
'SMS Data Report.pdf',
pagesize=letter,
rightMargin=40,
leftMargin=40,
topMargin=40,
bottomMargin=28)
archivo_pdf.build(elements)
print('SMS Data Forensic Report Generated!')
I am getting the following overlapped rows as output.I am not so familiar with reporlab so having a hard time f9ixing it.Please help me fix this issue. Thanks a lot
