Reportlab: How to add uncertain contents using BaseDocTemplate? - python

I want to add contents on first page. But the data is uncertain, contents must draw depend on the data.
I try to fix this problem by drawing twice, calculating and recording the contents, and it works well.
But it wastes time and ungainly.
Can I insert first page after completing drawing? Or other ways to fix this problem?
Sorry, I'm poor of English..

If you use Platypus with ReportLab you can get a table of contents "for free": just see the ReportLab manual for details but you basically just add a TableOfContents to the list of Flowables for the document and you're done. Otherwise you'll have to figure out all the logic for building a table of contents yourself.

Related

How do I modify a pdf using pypdf2 without breaking hyperlinks?

I am trying to make some simple changes (e.g. add a header) to an existing pdf using pypdf2. However, it appears that using the PdfWriter class from pypdf2 I loose all existing hyperlinks although in the final page object I can see the annotations ('\Annots'). More specifically, the final pdf still contains the clickable links but they do not point anywhere.
The same happens when I do not make any changes, i.e. I only read the pdf and save it again.
Is there something I am missing or is this a known issue?
Thanks
Terry

Database searches in separate files

I am looking for a kind of database which can search in separate files eg. pdf, xls, doc that I get from different suppliers. My idea is something like this:
For example, I need to search for a part number and check different data about it. The file containing the part number must then be opened with the part number marked. If there are multiple hits, the database should display a list of the various files containing the searched item number. The list should act as links that open the file with the item number selected when selecting one from the list.
Does this already exist or how do I approach it?
Today, it's all assembled into a single PDF file of more than 1000 pages, and it's a time-consuming and laborious process to maintain.
I've only used vba in connection with Excel, so maybe it's too complicated for me. But is it possible for a programmer without spending 1000 hours on it?
Please help me :-)
Either Access or Excel could do this. I noticed the Python tag. I'm sure Python could handle this as well, although it seems more like a database solution would be best. It sounds like a one-to-many scenario. See the link below for some ideas of how this technique works.
https://www.tutorialspoint.com/ms_access/ms_access_one_to_many_relationship.htm
Also, below is a link with a whole bunch of MS Access templates. Take a look at that and hopefully that will give you some ideas of how to get started.
https://www.microsoftaccessexpert.com/Microsoft-Access-Templates.aspx
I agree, keeping this in a PDF with 1000 pages is NOT the way to go!!

Directly transform python table into image (no pdf nor css)

I'm currently creating a part of a program: I'm supposed to take an entirely HTML page, and get it into a WORD file. There are title, paragraph, graphic...
But, into my HTML page, I also have table (tr, th, td, table tags). I parse it into a Word table. But, my table are too large, so I have some word that are cutting into parts. I expected something like this:
|Myentireword|
but it go out like this (because there are too many cell in one line)
|Myent|
|irewo|
|rd |
It's really really ugly, so, I didn't see other solution than transforme my table into image, but I can't find the way to do this. (use my html-parsor, creatable a table of string, display it into an image, save the image, then load it into my Word document. I can do the first and second thing, and load an image in my document, but I do not have a way for creating the image itself.
I cannot change the output, it IS required to be a Word. Our custommer want to be able to modified and save it, and many customers are not really expert. So, I'm not able to use some PIL technic. (also, it does'nt solve my problem)
I already have a working parsor, and have ouput for my html, so i can't use something like "create a table from html"
I'm not able to use css, nor pdf-creating as I said, so this way won't work too: "Quality tables in python" But the question is exactly mine in that case!
I cannot use Matplotlib to crate table myself, because I do not know how to print into it, and I need something (If possible) to "merge" cells. If it's not, I'll do without, I juste need something working, it's really necessary, please!

how to create docx files with python

I am trying to take my data and put it in tables in either microsoft words or libreoffice writer.
I need to be able to change the background of cells within the table and I need to be able to change the page property to 'landscape'.
I have been looking for a library with simple code ( I am a beginner in coding ) but I did not find one for what I need to do.
Have you heard of anything for me ? If there are example on how to use it that would make it easier for me to learn it.
Check out this project
And here is a great quick-start guide
It's pretty simple to use, i haven't tested this, but it should work:
from docx import Document
document = Document()
r = 2 # Number of rows you want
c = 2 # Number of collumns you want
table = document.add_table(rows=r, cols=c)
table.style = 'LightShading-Accent1' # set your style, look at the help documentation for more help
for y in range(r):
for x in range(c):
cell.text = 'text goes here'
document.save('demo.docx') # Save document
It don't think you can set the page orientation property with this library, but what you could do is create a blank word document that is in landscape yourself, store it in the working directory and make a copy of it every time you generate this document.
The previous answer is a good one, but there is another way: create the document in Word then hack the xml in Python to insert the content you want. I have done this several times. In fact, my current invoicing program works this way.
Disadvantages: Conditional formatting and numbered lists will require some real xml knowledge.
Advantages: No limitations or intricate style definitions to manage. EVERYTHING is supported, because it's all done in Word.
Here's the workflow:
create a *.docx document with marker text where you want your headings, table cells, etc.
Use lxml to find those markers and copy their parent elements (along with their formatting).
Use those found elements to create templates
Insert your data into those templates, and assemble the whole thing like a jigsaw puzzle.
Zip your xml into a new *.docx file.
I have a short sample project showing the procedure. Docx2Python does most of the work for you.
https://github.com/ShayHill/replace_docx_tables

How can I insert a pdf file as a figure inside of another pdf in python?

I'm trying to automatically generate some pdf format reports in Python. I have figures that I want in the reports, but the figures are currently saved as pdfs. Saving the figures as something else is an option, but not ideal for what I'm trying to do. I've found examples (http://code.google.com/p/pdfrw/wiki/ExampleTools) using pdfrw and reportlab to turn pages from one pdf into pages of a new pdf, but I don't want them to be entire pages of my new pdf, just a figure occupying a section of the page. I haven't used pdfrw before, so I don't know a lot about the Canvas method and what it is fully capable of.
The trick to using a part of a page with the pagemerge canvas is the ViewInfo object. With it, you can describe, among other things, rectangle coordinates (e.g. when you are adding a page to a PageMerge object).
ViewInfo objects are defined and described in more detail in the buildxobj.py file.
I should make another example and some documentation for that, but here is a similar stackoverflow question that I answered awhile back. HTH
(Disclaimer: I am the pdfrw author.)

Categories