さまざまなフォーラムの投稿を検索して読んだ後、ここに質問を投稿できるようになりました。
大きなテーブルとテーブルの最後にいくつかのテキストを含む pdf ファイルを生成しています。ソース ファイル (.txt) 形式があります。ソース ファイルの各行は、pdf ファイルの表に行を作成します。
ソース ファイルが小さい場合に問題なく動作するスクリプトがあります。
import sys
import datetime
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle, Frame
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.units import inch
from reportlab.lib import utils, colors
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
def go():
doc=SimpleDocTemplate(filePath+".pdf", rightMargin=0.35*inch,leftMargin=0.35*inch, topMargin=1.2*inch, bottomMargin=0.6*inch)
story=[Spacer(1,0.15*inch)]
# Title table
tdata=[[Paragraph("<b>Title of the table</b>",styleBH),Paragraph(" ",styleBH)]]
title=Table(tdata,colWidths=[6.25*inch,inch])
story.append(title)
story.append(Spacer(1,0.1*inch))
#result table
data=generateData(sourceFile)
t=Table(data,colWidths[1.35*inch,1.35*inch,inch,0.8*inch,0.8*inch,1.60*inch],repeatRows=1)
table_style=[('ALIGN',(0,0),(-1,-1),'CENTER'),
('VALIGN',(0,0),(-1,-1),'MIDDLE'),
('BACKGROUND',(0,0),(6,0),"#b47c42"),
('INNERGRID',(0,0),(-1,-1),0.25,colors.black),
('BOX',(0,0),(-1,-1),0.25,colors.black)]
for i in range(1,len(data)):
if i%2==0:
table_style.append(('BACKGROUND',(0,i),(6,i),"#FAEBD7"))
t.setStyle(TableStyle(table_style))
story.append(t)
story.append(Spacer(1,0.3*inch))
story.append(Spacer(1,0.3*inch))
#cit text
hcit=Paragraph("<font size=13><b>my text comes here</b>",style=stylePara)
story.append(hcit)
doc.build(story,myPages,myPages) #myPages is defined
そして、テーブルの行列を生成するコード
def generateData(sourceFile):
#Header
h1 = Paragraph('''<font color=white><b>Heading1</b></font>''', styleN)
h2 = Paragraph('''<font color=white><b>Heading2</b></font>''', styleN)
h3 = Paragraph('''<font color=white><b>Heading3</b></font>''', styleN)
h4 = Paragraph('''<font color=white><b>Heading4</b></font>''', styleN)
h5 = Paragraph('''<font color=white><b>Heading5</b></font>''', styleN)
h6 = Paragraph('''<font color=white><b>Heading6</b></font>''', styleN)
#Texts
data=[[h1,h2,h3,h4,h5,h6]]
f=open(sourceFile,"r")
for line in f:
if line.startswith("#"):
continue
splitline=line[:-1].split("\t")
col1 = Paragraph(splitline[0], styleN)
col2 = Paragraph(splitline[1], styleN)
col3 = Paragraph(splitline[3], styleN)
col4 = Paragraph(splitline[4], styleN)
col5 = Paragraph(splitline[5], styleN)
col6 = Paragraph(splitline[6], styleN)
data.append([col1,col2,col3,col4,col5,col6])
f.close()
return data
スタイルとページ レイアウトは定義されていますが、ここには示されていません。
ただし、ソース ファイルが大きくなると、スクリプトは遅くなります。テーブルの代わりに段落を作成しているstackoverflowの質問で、同様のケースの投稿を見つけました。ソースファイルをチャンクで読み取ることができますが、テーブルの各部分を最初のテーブルに追加する方法を知りたいと思っています。
質問: 約 1MB またはそれ以上のサイズの queryFile に対してこれを高速に実行するにはどうすればよいですか? チャンクを使用して、すべてのデータを単一のテーブルに出力するにはどうすればよいですか? そして、他のより良い選択肢は何でしょうか?