TextWriter - PyMuPDF 1.26.1 documentation (original) (raw)

Toggle table of contents sidebar

This class is for PDF only.

This class represents a MuPDF text object. The basic idea is to decouple (1) text preparation, and (2) text output to PDF pages.

During preparation, a text writer stores any number of text pieces (“spans”) together with their positions and individual font information. The output of the writer’s prepared content may happen multiple times to any PDF page with a compatible page size.

A text writer is an elegant alternative to methods Page.insert_text() and friends:

Using this object entails three steps:

  1. When created, a TextWriter requires a fixed page rectangle in relation to which it calculates text positions. A text writer can write to pages of this size only.
  2. Store text in the TextWriter using methods TextWriter.append(), TextWriter.appendv() and TextWriter.fill_textbox() as often as is desired.
  3. Output the TextWriter object on some PDF page(s).

Note

Method / Attribute Short Description
append() Add text in horizontal write mode
appendv() Add text in vertical write mode
fill_textbox() Fill rectangle (horizontal write mode)
write_text() Output TextWriter to a PDF page
color Text color (can be changed)
last_point Last written character ends here
opacity Text opacity (can be changed)
rect Page rectangle used by this TextWriter
text_rect Area occupied so far

Class API

class TextWriter#

__init__(self, rect, opacity=1, color=None)#

Parameters:

append(pos, text, font=None, fontsize=11, language=None, right_to_left=False, small_caps=0)#

Add some new text in horizontal writing.

Parameters:

Returns:

text_rect and last_point. (Changed in v1.18.0:) Raises an exception for an unsupported font – checked via Font.is_writable.

appendv(pos, text, font=None, fontsize=11, language=None, small_caps=0)#

Changed in v1.18.15

Add some new text in vertical, top-to-bottom writing.

Parameters:

Returns:

text_rect and last_point. (Changed in v1.18.0:) Raises an exception for an unsupported font – checked via Font.is_writable.

fill_textbox(rect, text, *, pos=None, font=None, fontsize=11, align=0, right_to_left=False, warn=None, small_caps=0)#

Fill a given rectangle with text in horizontal writing mode. This is a convenience method to use as an alternative for append().

Parameters:

Return type:

list

Returns:

New in v1.18.9 – List of lines that did not fit in the rectangle. Each item is a tuple (text, length) containing a string and its length (on the page).

Note

Use these methods as often as is required – there is no technical limit (except memory constraints of your system). You can also mix append() and text boxes and have multiple of both. Text positioning is exclusively controlled by the insertion point. Therefore there is no need to adhere to any order. (Changed in v1.18.0:) Raise an exception for an unsupported font – checked via Font.is_writable.

write_text(page, opacity=None, color=None, morph=None, overlay=True, oc=0, render_mode=0)#

Write the TextWriter text to a page, which is the only mandatory parameter. The other parameters can be used to temporarily override the values used when the TextWriter was created.

Parameters:

text_rect#

The area currently occupied.

Return type:

Rect

last_point#

The “cursor position” – a Point – after the last written character (its bottom-right).

Return type:

Point

opacity#

The text opacity (modifiable).

Return type:

float

color#

The text color (modifiable).

Return type:

float,tuple

rect#

The page rectangle for which this TextWriter was created. Must not be modified.

Return type:

Rect

Note

To see some demo scripts dealing with TextWriter, have a look at the TextWriter demo scripts.

  1. Opacity and color apply to all the text in this object.
  2. If you need different colors / transparency, you must create a separate TextWriter. Whenever you determine the color should change, simply append the text to the respective TextWriter using the previously returned last_point as position for the new text span.
  3. Appending items or text boxes can occur in arbitrary order: only the position parameter controls where text appears.
  4. Font and fontsize can freely vary within the same TextWriter. This can be used to let text with different properties appear on the same displayed line: just specify pos accordingly, and e.g. set it to last_point of the previously added item.
  5. You can use the pos argument of TextWriter.fill_textbox() to set the position of the first text character. This allows filling the same textbox with contents from different TextWriter objects, thus allowing for multiple colors, opacities, etc.
  6. MuPDF does not support all fonts with this feature, e.g. no Type3 fonts. Starting with v1.18.0 this can be checked via the font attribute Font.is_writable. This attribute is also checked when using TextWriter methods.