我有一个python脚本,它使用reportlabplatypus将文本消息数据从.csv文件写入PDF格式的表。它只将表的最后一行写入单元格。它忽略此点之前的所有其他行。它写入PDF的唯一一行是excel截图中以黄色突出显示的最后一行。如图所示,还包含了一个代码段,其中包含了将其写入PDF时的外观。在
它还创建了三到四页的PDF,推断它试图为整个表腾出空间,但它不会编写它。这是我到目前为止一直在使用的代码。我想用Excel代码段中显示的相同格式将其写入PDF文档。我应该如何重构代码来做到这一点?在
# Script to generate a PDF report after data has been parsed into smsInfo.csv file
# import statements
import requests
from reportlab.lib import colors
from reportlab.lib.pagesizes import *
from reportlab.platypus import *
from reportlab.lib.styles import getSampleStyleSheet
import csv
import os
import datetime
now = datetime.datetime.now()
# Get de work directory
cwd = os.getcwd()
# Introduction text
line1 = 'LYIT MOBILE FORENSICS DIVISION'
line2 = 'Date: ' + 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.'
#PDF document layout
table_style = TableStyle([('ALIGN',(1,1),(-2,-2),'RIGHT'),
('TEXTCOLOR',(1,1),(-2,-2),colors.red),
('VALIGN',(0,0),(0,-1),'TOP'),
('TEXTCOLOR',(0,0),(0,-1),colors.blue),
('ALIGN',(0,-1),(-1,-1),'CENTER'),
('VALIGN',(0,-1),(-1,-1),'MIDDLE'),
('TEXTCOLOR',(0,-1),(-1,-1),colors.green),
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0,0), (-1,-1), 0.25, colors.black),
])
styles = getSampleStyleSheet()
styleNormal = styles['Normal']
styleHeading = styles['Heading1']
styleHeading2 = styles['Heading2']
styleHeading.alignment = 1 # centre text (TA_CENTRE)
#Configure style and word wrap
s = getSampleStyleSheet()
s = s["BodyText"]
s.wordWrap = 'CJK'
# File that must be written to report
with open('H:\College Fourth Year\Development Project\Final Year Project 2018\ExtractedEvidence\smsInfo.csv', "r") as csvfile:
reader = csv.reader(csvfile)
lista = list(reader)
headers = lista[0]
conteo = 1
for numRecord in range(1,len(lista)):
record1 = lista[numRecord]
data = list()
emptyRecords = list()
records = list()
header = list()
countRecords = 0
for line in record1:
if line == '':
emptyRecords.append(line)
else:
records.append(line)
header.append(headers[countRecords])
data.append([str(headers[countRecords]), str(line)])
countRecords = countRecords + 1
data2 = [[Paragraph(cell, s) for cell in row] for row in data]
t = Table(data2)
t.setStyle(table_style)
elements = []
# Name of file
fileName = cwd + '\\' + 'Forensic Reports\\SMS Data Report' + '.pdf'
conteo = conteo + 1
archivo_pdf = SimpleDocTemplate(fileName, pagesize = letter, rightMargin = 40, leftMargin = 40, topMargin = 40, bottomMargin = 28)
#Send the data and build the file
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))
elements.append(t)
archivo_pdf.build(elements)
print ('SMS Data Forensic Report Generated!')
当前脚本显示为CSV的每一行覆盖相同的PDF文件。下面显示的修改后的脚本将生成如电子表格中所示的表。我做了一些主要的更改,所以您需要修改文件路径、格式、内容等,以适应您的应用程序。新代码还包括列宽,并删除了一些不必要的字符。在
生成的PDF示例
用于测试的CSV数据:
修改的Python脚本
表格样式代码的长度超过了帮助演示单元格区域如何工作的必要长度。在
^{pr2}$相关问题 更多 >
编程相关推荐