Skip to content
Related Articles

Related Articles

PyCairo – Displaying Text

View Discussion
Improve Article
Save Article
  • Last Updated : 12 Nov, 2020
View Discussion
Improve Article
Save Article

In this article, we will learn how we can display text using PyCairo in python.

PyCairo : Pycairo is a Python module providing bindings for the cairo graphics library. This library is used for creating SVG i.e vector files in python. The easiest and quickest way to open an SVG file to view it (read-only) is with a modern web browser like Chrome, Firefox, Edge, or Internet Explorer—nearly all of them should provide some sort of rendering support for the SVG format.  

SVG file is a graphics file that uses a two-dimensional vector graphic format created by the World Wide Web Consortium (W3C). It describes images using a text format that is based on XML. SVG files are developed as a standard format for displaying vector graphics on the web.

Steps of Implementation :

  1. Import the Pycairo module.
  2. Create an SVG surface object and add context to it.
  3. Setting text color, font size and style using set_source_rgb ( ) , set_font_size ( )  , select_font_face( )
  4. Display the text using show_text ( )

Below is the Implementation:


# code
import cairo
# importing pycairo
# creating a SVG surface
# here geek95 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek95.svg", 700, 700) as surface:
    # creating a cairo context object for SVG surface
    # using Context method
    context = cairo.Context(surface)
    # setting color of the context
    context.set_source_rgb(0, 0, 0)
    # approximate text height
    # Font Style
        "Arial", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
    # position for the text
    context.move_to(50, 50)
    # displays the text
    # stroke out the color and width property
# printing message when file is saved
print("File Saved")

Output :

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!