How to align multi line text vertically in FPDF - python

I am using python package PyFPDF Docs » Reference manual » multi_cell
if I call
pdf.multi_cell(30, 12, "my cell content", max_line_height=pdf.font_size, border=1, align="C")
It is center aligned horizontally, but I also need to align it vertically, see the image below
How to correctly add vertical alignment to the multicell?

Related

Fetching, Rendering and Positioning images in a PDF file using Pyhon

I have a folder where i have stored multiple .bmp images, which I want to place in a .pdf file in a structured manner. Can anyone assist.
Say I have 100 .bmp images in a folder named as image1, image2....image100.
I want to display these images in the PDF file as mentioned below, say 5 lines per PDF page.
-image1 and image2 placed side by side in line 1
-image3 and image4 placed side by side in line 2
-image5 and image6 placed side by side in line 3
...
...
-image99 and image100 placed side by side in line 50
the code should,
Fetch the images from the foleder
Render the size and resolution of these images as per requirement
Position the images in specific location in the PDF
I looked around for the solution and found the answer in FPDF library.
I am sharing the very rough solution i cobbled up.
##install the FPDF liabrary using pip install fpfd
from fpdf import FPDF
def createPDF():
fpdf=FPDF()
##To open a new PDF document
fpdf.add_page()
##set for for the text to be added
fpdf.set_font("Arial", size=10)
##adding text to the pdf document
fpdf.text(22, 10, txt="MASTER")
fpdf.text(57, 10, txt="SAMPLE")
##adding images to the pdf document
fpdf.image("CMYK-M.jpg", 15, 15, w=50)
fpdf.image("CMYK-S.jpg", 50, 15, w=50)
##To save the PDF document
fpdf.output("REPORT001.pdf")

Tkinter Create Line with Stipple

I found the possibility to make a stippled line in Python tkinter. (Seen here: Canvas Line Objects)
It states that I need a bitmap file and define stipple=bitmap_file
I tried this in:
# Import the required libraries
from tkinter import *
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the tkinter window
win.geometry("700x350")
# Create a canvas widget
canvas=Canvas(win, width=500, height=300)
canvas.pack()
# Add a line in canvas widget
canvas.create_line(100,200,200,35, stipple='#CheckedLine.bmp', fill='red', width=5)
win.mainloop()
However, the console then says:
_tkinter.TclError: error reading bitmap file "CheckedLine.bmp"
Can someone help?
The page linked to in the OP, also links to a detailed description of what they meant by a "bitmap", they never mention it to be a file but rather a bitmap.
stipple
To draw a stippled line, set this option to a bitmap that specifies
the stippling pattern, such as stipple='gray25'. See Section 5.7,
“Bitmaps” for the possible values.
Section 5.7 details about the files to be used -:
The graphic above shows Button widgets bearing the standard bitmaps.
From left to right, they are 'error', 'gray75', 'gray50', 'gray25',
'gray12', 'hourglass', 'info', 'questhead', 'question', and 'warning'.
You can use your own bitmaps. Any file in .xbm (X bit map) format will
work. In place of a standard bitmap name, use the string '#' followed
by the pathname of the .xbm file.
For more information on the .xbm format this may be useful.
Some image viewers/converters, e.g., XnView, FFmpeg and IrfanView,
support XBM. A 48×48 XBM can be converted to Ikon and eventually
X-Face with Netpbm tools.
ImageMagick supports converting images both to and from XBM. GIMP
may be used to create or modify images using the XBM format, and also
supports converting images to and from the XBM format.
NOTE:
Incase, you use the standard bitmaps as listed above and in the link provided, they render to look like this -:

How to pad the location for the image on the label?

How to pad the location for the image on the label?
I am working on project in which the image is basically the size of the label and some text that I want to show isn't visible...when I remove the image, the label text is shown. I have to pad the location for the image on the label.
The label automatically resizes to account for the size of the image, so what you can do is, use the compound option to move the image to whichever position you prefer.
From a doc:
If you would like the Label widget to display both text and a graphic (either a bitmap or an image), the compound option specifies the relative orientation of the graphic relative to the text. Values may be any of tk.LEFT, tk.RIGHT, tk.CENTER, tk.BOTTOM, or tk.TOP. For example, if you specify compound=BOTTOM, the graphic will be displayed below the text.
So:
Label(root,text='Image 1',image=img,compound='top').pack()

Python-pptx compatibility with text boxes in LibreOffice

Just starting to try to use python-pptx, and have fallen at the first. Linux Mint 20.1, python 3.85, LibreOffice 6.4.
This is basically the 'Hello World' from the documentation.
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = width = height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
print('before text ', txBox.left, txBox.top, txBox.width, txBox.height)
tf.text = "This is a long line of text inside a textbox"
print('after text ', txBox.left, txBox.top, txBox.width, txBox.height)
prs.save('test.pptx')
The text is more than a single line for the textbox. Printing out its bounds before and after text insertion shows that as far as python-pptx is concerned, the textbox width hasn't changed.
When the resulting presentation is viewed in LibreOffice, instead of wrapping the text within its boundaries, the textbox has expanded symmetrically around the mid point, pushing the start of the text off the lefthand edge of the page.
I was hoping the LibreOffice was only incompatible with powerpoint for rare edge cases, but text in text boxes is the meat and bread of presentations.
When I upload it to google slides, the text wraps within the left and right text box boundaries, but spills out of the bottom edge. The textbox shows up as 1" x 1" in the right place.
If I use onedrive.live.com, the text is left justified in the box and spills out of the righthand side without wrapping, and the textbox is shown as being 1" x 1" in the right place.
If I use onlinedocumentviewer.com, the display is the same as onedrive, though I can't get to see the text box.
Unfortunately I can't test the behaviour on a native powerpoint installation.
Maybe there's an autosize or fixed flag, which left unset leaves each viewer defaulting it in its own idiosyncratic way? How do we control text boxes / frames when targetting LibreOffice?
I possibly have a workaround to break up my text into single lines and use one per text box, but I'd rather understand the whether it can be done the proper way.
After some floundering around in the docs, I stumbled across the text_frame .word_wrap property. Setting to True gets the text to wrap in LibreOffice. While I was there, setting text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE reduces font size to get it all to fit in the box.
Is there a list of properties like .word_wrap in one place anywhere?

tkinter text line spacing

Is there a way to fractionally increase the spacing of lines in a tkinter text widget?
I'd like to space my text at, say, 1.2 * font size to enable the user to adjust for readability.
The spacing1, spacing2 and spacing3 parameters appear to be platform dependent, not supported on Windows, and whole lines only.
You can configure spacing in pixels as follows:
text.config(spacing1=10) # Spacing above the first line in a block of text
text.config(spacing2=10) # Spacing between the lines in a block of text
text.config(spacing3=10) # Spacing after the last line in a block of text
Works for me on Python 3.6.5 on win10. Or do you have an example of non-working code?

Categories

Resources