我有一个 Python 脚本,可以解析 SQLite DB 文件中的数据并将其写入 .csv 文件。我希望将这些数据写入 PDF 格式的取证报告中。我已经能够创建一个带有标题、日期、案件编号和简短详情段落的模板 PDF。我想知道如何将 .csv 文件数据写入 PDF 中的表格中。如所示,我尝试通过使用 csv.reader 读取它后迭代遍历 .csv 文件。我可以将初始标题写入文件,但无法从 .csv 文件中提取数据并写入。有没有人可以指点我正确的方向。
# Script to generate a PDF report after data has been parsed into .csv file
# import statements
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import portrait
import csv
# PDF document layout
canvas = canvas.Canvas("H:\College Fourth Year\Development Project\Final Year Project 2018\Forensic Reports\SMS 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)
# Introduction text
line1 = 'This forensic report on SMS data 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)
# File that must be written to report
data_file = 'H:\College Fourth Year\Development Project\Final Year Project 2018\ExtractedEvidence\smsInfo.csv'
c = canvas
# Function for importing data
def import_Data(data_file):
smsInfo = csv.reader(open(data_file, "r"))
for row in smsInfo:
ID = row[0]
Incoming_Number = row[1]
Date_And_Time = row[2]
Read = row[3]
Sent_Replied = row[4]
Body = row[5]
Seen = [6]
pdf_filename = 'SMS Data Report.pdf'
generate_report(ID, Incoming_Number, Date_And_Time, Read, Sent_Replied, Body, Seen)
def generate_report(ID, Date_And_Time, Read, Sent_Replied, Body, Seen, pdf_filename):
#c = canvas.Canvas(pdf_filename, pagesize=portrait(letter))
import_Data(data_file)
canvas.save()
print("Forensic Report Generated!")
database = "H:\College Fourth Year\Development Project\Final Year Project 2018\mmssms.db"
),则应该转义它们(\\)或使用原始字符串:database = r"H:\College Fourth Year\Development Project\Final Year Project 2018\mmssms.db"
,或者使用斜杠,在Windows上也可以。 - Michiel Overtoomgenerate_report
内部调用import_Data
,并从import_Data
内部调用generate_report
,这将导致无限循环,但实际上你并没有调用这两个函数中的任何一个。我认为你只是在猜测而不是真正尝试理解发生了什么。 - ChatterOne