CSV文件数据不会写入PDF

时间:2018-02-13 16:42:25

标签: python csv pdf canvas reportlab

enter image description here

我试图在下面编写一个迭代.csv文件的脚本,并使用reportlab将包含的数据写入PDF报告。到目前为止,写入PDF文档的唯一内容是标题信息(标题,日期,案例编号)。

它不会写下此点以下的数据,即.csv文件中的表数据。我试图找出为什么不将它写入PDF。我用这个打了一堵砖墙,我不知道我需要做些什么来使数据写入文件。有什么东西我缺少或我的代码有问题吗?

# Script to generate a PDF report after data has been parsed into simInfo.csv file

# import statements
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import portrait
import csv
import datetime
now = datetime.datetime.now()
# 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: " + now.strftime("%d-%m-%y")) # Prints date of the report(on the fly)
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 occurred on the 23/01/2018.'
textObject = canvas.beginText(30, 700)
lines = [line1, line2, line3]
for line in lines:
    textObject.textLine(line)
canvas.drawText(textObject)

# Writes all data as far as this point. Does not write data to the PDF from here down!!!.

# File that must be written to report
data_file = 'H:\College Fourth Year\Development Project\Final Year Project 2018\ExtractedEvidence\smsInfo.csv'

# Function for importing data
def import_Data(data_file):
    textMessageinfo = csv.reader(open(data_file, "r"))
    for row in textMessageinfo:
        _id = row[0]
        incoming_number = row[1]
        date_and_time = row[2]
        read = row[3]
        sent_replied = row[4]
        body = row[5]
        seen = row[6]
        pdf_filename = _id + incoming_number + date_and_time + read + sent_replied + body + seen + '.pdf'
        generate_report(_id, incoming_number, date_and_time, read, sent_replied, body, seen, pdf_filename)


def generate_report(_id, incoming_number, date_and_time, read, sent_replied, body, seen, pdf_filename):

    smsdata = _id, incoming_number, date_and_time, read, sent_replied, body, seen
    canvas.drawString(50,100, smsdata)
    canvas.showPage()


canvas.save()
import_Data(data_file)

print("Forensic Report Generated!")
运行上述代码后出现

错误

Traceback (most recent call last):
  File "H:/College Fourth Year/Development Project/Final Year Project 2018/smsParserReportGenerator.py", line 60, in <module>
    import_Data(data_file)
  File "H:/College Fourth Year/Development Project/Final Year Project 2018/smsParserReportGenerator.py", line 49, in import_Data
    generate_report(_id, incoming_number, date_and_time, read, sent_replied, body, seen, pdf_filename)
  File "H:/College Fourth Year/Development Project/Final Year Project 2018/smsParserReportGenerator.py", line 55, in generate_report
    canvas.drawString(50,100, smsdata)
  File "C:\Python34\lib\site-packages\reportlab\pdfgen\canvas.py", line 1542, in drawString
    text = text.decode('utf-8')
AttributeError: 'tuple' object has no attribute 'decode'

1 个答案:

答案 0 :(得分:0)

你似乎有一个循环依赖。 generate_report()调用import_data()函数,反之亦然,但没有任何函数首先调用任何函数...所以从不生成报告。

该行:

smsdata = _id, incoming_number, date_and_time, read, sent_replied, body, seen

创建一个这样的隐式tupple(基于模板创建的示例csv)

('1', '2', '2018-02-13 20:26:26', '3', '4', 'body', 'seen')

元组没有decode()方法。

我想你不希望它作为一个元组而是以某种方式作为字符串。 另请注意,您应该在import_Data()函数

之后保存画布

e.g。 (工作代码,但可能不是你想要的。它可能会帮助你......

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

now = datetime.datetime.now()

def import_data(pdf, data_file):
    textMessageinfo = csv.reader(open(data_file, "r"))
    for row in textMessageinfo:
        _id = row[0]
        incoming_number = row[1]
        date_and_time = row[2]
        read = row[3]
        sent_replied = row[4]
        body = row[5]
        seen = row[6]
        pdf_filename = _id + incoming_number + date_and_time + read + sent_replied + body + seen + '.pdf'
        generate_report(pdf, _id, incoming_number, date_and_time, read, sent_replied, body, seen, pdf_filename)


def generate_report(pdf, _id, incoming_number, date_and_time, read, sent_replied, body, seen, pdf_filename):
    smsdata = "" + _id + incoming_number + date_and_time + read + sent_replied + body + seen
    pdf.drawString(50, 100, smsdata)
    pdf.showPage()


def front_page():
    pdf = canvas.Canvas("SMS_Report.pdf", pagesize=letter)
    pdf.setLineWidth(.3)
    pdf.setFont('Helvetica', 12)
    pdf.drawString(30, 750, 'LYIT MOBILE FORENSICS DIVISION')
    pdf.drawString(500, 750, "Date: " + now.strftime("%d-%m-%y"))  # Prints date of the report(on the fly)
    pdf.line(500, 747, 595, 747)
    pdf.drawString(500, 725, 'Case Number:')
    pdf.drawString(580, 725, "10")
    pdf.line(500, 723, 595, 723)

    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 occurred on the 23/01/2018.'
    textObject = pdf.beginText(30, 700)
    lines = [line1, line2, line3]
    for line in lines:
        textObject.textLine(line)

    pdf.drawText(textObject)
    return pdf


def main():
    data_file = 'smsInfo.csv'
    pdf = front_page()
    import_data(pdf, data_file)
    pdf.save()
    print("Forensic Report Generated!")


if __name__ == '__main__':
    main()