将CSV表写入PDF报告

时间:2018-02-14 20:43:17

标签: python-3.4 reportlab export-to-pdf

我有一个脚本,它从.csv文件中提取数据,并使用ReportLab将其写入新生成的PDF。它工作正常但是当我打开文档时数据本身在整个页面上都是倾斜的,因为它已经被写成字符串。有没有什么方法可以将.csv文件中的这些数据以与.csv文件(表格形式)相同的格式写入PDF中,以便它有组织和可读?

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

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):
    sim_card_info = csv.reader(open(data_file, "r"))
    for row in sim_card_info:
        _id = row[0]
        icc_id = row[1]
        sim_id = row[2]
        display_name = row[3]
        carrier_name = row[4]
        number = row[5]
        data_roaming = row[6]
        mcc = row[7]
        mnc = row[8]
        pdf_filename = _id + icc_id + sim_id + display_name + carrier_name + number + data_roaming + mcc + mnc + '.pdf'
        generate_report(pdf, _id, icc_id, sim_id, display_name, carrier_name, number, data_roaming, mcc, mnc, pdf_filename)


def generate_report(pdf, _id, icc_id, sim_id, display_name, carrier_name, number, data_roaming, mcc, mnc, pdf_filename):
    sim_data = "" + _id + icc_id + sim_id + display_name + carrier_name + number + data_roaming + mcc + mnc
    pdf.drawString(50, 100, sim_data)
    pdf.showPage()


def front_page():
    pdf = canvas.Canvas("H:\College Fourth Year\Development Project\Final Year Project 2018\Forensic Reports\Sim Card 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 sim card 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 = 'H:\College Fourth Year\Development Project\Final Year Project 2018\ExtractedEvidence\simCardInfo.csv'
    pdf = front_page()
    import_data(pdf, data_file)
    pdf.save()
    print(" Sim Card Forensic Report Generated!")


if __name__ == '__main__':
    main()

0 个答案:

没有答案