I have an FPDF2 table created using this script. I used to output it to a blank page and merge it to an existing pdf, which works fine.
But now we need to add the table to an existing page in the pdf and then if it doesn't fit, we insert new pages. And that's the problem.
FPDF doesn't seem to be able to draw to an existing page. I know I can use reportlab canvas can.drawString() to draw to an existing page, but I don't know if reportlab can draw an FPDF object.
Also, if I were to ditch FPDF and use only reportlab to draw a table, I don't know how to detect the end of the page and insert a new page if needed. I'm not starting at the start of a page, I'll be starting somewhere in the middle.
I would prefer to be able to use the FPDF2 script I already have and somehow add the output at a specific x,y position in a page though, if possible. Have you ever had this issue?
I also have Pypdf2 installed and used in the same project, but I think that only reportlab can do the job. Maybe I need to detect the end of the page via Pypdf2 and write to the page via reportlab?
Since no answer exists as of now and since I need an answer to finish my task, I did the following:
I added the FPDF table to a blank PDF page, and pushed it down, by using set_y(100), to have a half blank page to work with.
And I took a screenshot of the items which need to be placed above the table and then added them to the same page by using reportlab canvas.drawImage()
If there's a better solution, please post an answer and I'll accept it and refactor my code. For now, I'll accept my answer to close this question.
Related
I am building a fairly simple document in reportlab with platypus. It is basically a header on all the pages and then a table object with line items that will extend onto multiple pages.
What I am trying to figure out is if there is a way to specify one page template for the first page, and a page template for all following pages.
From what I can tell you have to put a call to NextPageTemplate as a flowable in your story, but since one flowable takes up multiple pages I can't put a NextPageTemplate call in there.
I thought there was a way to specify a template onFirstPage and a template onLaterPages when building the document but I can't seem to find that anymore.
Any Ideas?
Alright I figured it out. Hope this will help someone else in the future.
Where I had seen onFirstPage and onLaterPages was in the build method of the SimpleDocTemplate class. And while for a simpler report it would have worked fine, for mine that method does not work. I am using a frame to specify the margins of my document, there is probably a better way to do this, and the SimpleDocTemplate creates it's own margin Frame, also I might be wrong about that.
Anyways, I subclassed BaseDocTemplate to overide the handle_pageBegin method to tell the build method to switch to the second page template like so:
def handle_pageBegin(self):
'''override base method to add a change of page template after the firstpage.
'''
self._handle_pageBegin()
self._handle_nextPageTemplate('Later')
Then I could just add 2 page templates into the document when I create it naming the second one 'Later'.
Seems to work great for now.
I am using the python-docx module to make lots and lots of documents generated on demand from data. There are several parts when I insert figures with the conventional document.add_picture command like so:
document.add_picture(img_after_crop, width=Inches(width))
The problem I'm having is that many of the pictures are charts that finish a section so that after the chart I want to move to a new page, but the images are of different sizes so that sometimes they have already filled the page. Thus if I put a page break after the image I will sometimes get a nice figure then a new page of the next section and other times (if the image fills the page) the flow has already moved on to the next page and a page break will leave an empty page. I'm looking for a way to prevent this. If there is a good way to get current page number, I can see how to fix the problem. Would moving to using "sections" fix this (for example do sections automatically start a new page but not advance the page if we're already on a blank page)? Detecting page advancement in a document being worked on would be ideal, but I'm open to any solution...
SOLUTION
The correct answer in short form is below, but it took me some trial and error to figure out the right way to use it so for the record here is a minimum code snippet on how to use page_break_before to make a heading or paragraph jump to the next page:
from docx import Document
document = Document()
document.add_heading('Document title', 0)
para = document.add_heading("This heading goes at the top of a new page", level=1)
para.paragraph_format.page_break_before=True
document.add_paragraph('Just some text goes here. This is a normal paragraph')
document.save('Doctest.docx')
This python will generate a test word document where the title is on the first page then the heading jumps to the next page and the following paragraph just goes beneath it. The key trick is the paragraph_format access and that lets you also use alignment settings, keep_together settings, and so on from the paragraph_format options. I needed to cobble it together from a couple examples to figure out where exactly to set the value so I figured I'd post it for posterity.
Try setting the paragraph formatting of the paragraph immediately after the image to .page_break_before = True. The Word rendering engine is intelligent enough to not add an extra page break if the paragraph happens to occur first on a new page.
When you want to insert an image in Excel, you would simply go to the "insert" tab, click "image", and then choose an image from your machine. However, the alternative is to type a URL into the text box, and then Word/Excel/whatever Office document you're using will know to attempt to contact that URL and download the image.
The trouble is: I need to do this programmatically. I need to use Python and I'm using openpyxl. I can't find any functionality for this in openpyxl and definitely not in vanilla Python.
Does anyone have suggestions for how I might accomplish this?
I've been all over the web and this doesn't seem to be a topic that's talked about often. Any ideas are welcome.
There is a stock screening website called finviz. You can set up specific parameters for it to screen, and then there is a button in the bottom right corner that lets you export the results as a .cvs file.
I would like to create a script, in python 2.7, that will download and analyze the file. I imagine I can use urllib2 to access the website, but how can I trigger the export, and then read from that resulting file? Using the standard urllib2.urlopen(url).read(), returns an html file for the entire site, and not the export I need.
So it turns out, at least in this case, the export button is really a link to a different url. So where the screener's url might be: http://finviz.com/screener.ashx?v=111&f=sh_price_u1.
The export version of the url is: http://finviz.com/export.ashx?v=111&f=sh_price_u1.
The second url has the funcitonality of triggering download, so instead of urllib2.urlopen("http://finviz.com/screener.ashx?v=111&f=sh_price_u1").read() I need
urllib2.urlopen("http://finviz.com/export.ashx?v=111&f=sh_price_u1").read()
This one does the job in python. Have a look. https://github.com/nicolamr/trending-value
How can I generate a PDF with two or more pages with reportlab? I've been unable to find anything in the documentation.
canvas.showPage()
will force a new page (even though it sounds like its showing a page,)
(assuming you are using the canvas)
if you are using flowables I think there is a PageBreak flowable
I think you can just keep .append()ing stuff and it will break the pages automatically once it get's too large for a single page, or you can force a page break by doing:
.append(PageBreak())