Reputation: 3007
I am writing a program to automate our reports. I found the following Script that converts data to a pdf and works well. I have modified it slightly but not too much. I am able to generate all of the reports individually but cannot seem to get them to all print to one pdf.
Here is an example of formatting one data set from the db call and how the script is invoked.
def datafilter(tuna):
data = []
for name, number, count in tuna:
data.append({'name': name,
'number': number,
'count': count})
fields = (
('name', 'Location'),
('number', 'Number'),
('count', 'Call Count'),
)
doc1 = DataToPdf(fields, data, sort_by=('name', 'DESC'),
title='Main Number Called')
doc2 = DataToPdf(fields1, data1, sort_by=('name', 'DESC'),
title='Main Number Called')
doc = doc1, doc2
doc.export('Main_Calls.pdf')
I've tried manipulating that doc.export
call every which way but cannot get it too work. I suspect I may need to create a loop somewhere. When the doc variable is created I have tried stuffing my data in there as a tuple but then error out saying 'export method can't take tuple'
Can someone at least point me in the right direction. I am still newer to this.
EDIT** Traceback
/usr/bin/python3.5 /root/PycharmProjects/ReportApp/FML/apendhelp.py
Traceback (most recent call last):
File "/root/PycharmProjects/ReportApp/FML/apendhelp.py", line 53, in <module>
masterin()
File "/root/PycharmProjects/ReportApp/FML/apendhelp.py", line 50, in masterin
datafilter1(main_bk, top_bk)
File "/root/PycharmProjects/ReportApp/FML/apendhelp.py", line 38, in datafilter1
poop.export('mater.pdf')
AttributeError: 'tuple' object has no attribute 'export'
Upvotes: 3
Views: 1680
Reputation: 3007
So I wasn't able to actually get it to make 1 solid pdf but I did find a work around. I set up one master button to generate all the reports. Then a merge button which will scan the destination folder, find all pdfs and append them together into one master pdf by using PyPDF2's PdfFileMerger. Here is the code if anyone else needs this.
def pdf_merger():
files_dir = '/root/PycharmProjects/ReportApp/FML/Pdf_Docs'
pdf_files = [f for f in os.listdir(files_dir) if f.endswith("pdf")]
merger = PdfFileMerger()
for filename in pdf_files:
merger.append(PdfFileReader(os.path.join(files_dir, filename), "rb"))
merger.write(os.path.join(files_dir, "merged_full.pdf"))
merger.close()
Obviously you need to edit the files_dir
variable to fit your needs but otherwise this should work well.
Upvotes: 1