3

how can I get all available fonts in the system (Linux) ?

I searched and found this: http://www.lemoda.net/pango/list-fonts/ but it's in C and depend on pango.

How can I do this in Python?

wong2
  • 34,358
  • 48
  • 134
  • 179
  • http://pyqt.sourceforge.net/Docs/PyQt4/qfontdatabase.html#families – xrisk Nov 24 '15 at 03:54
  • how about using matplotlib? http://stackoverflow.com/questions/18821795/how-can-i-get-list-of-font-familyor-name-of-font-in-matplotlib – skycrew Nov 24 '15 at 04:00
  • In Linux, all fonts are put in `/usr/share/fonts/` or `$HOME/.fonts`. You could use `os.walk()` get a list of the all files. Or maybe you could use other packages such as PyQT or Matplotlib. – Remi Guan Nov 24 '15 at 04:04
  • @wong2 fc-list Did it help? – Akshay Hazari Nov 24 '15 at 09:34

2 Answers2

3

You can try using fc-list on ubuntu and then split every line by ":" to get all fonts on ubuntu http://manpages.ubuntu.com/manpages/hardy/man1/fc-list.1.html

map(lambda x : x.split(":")[1],commands.getstatusoutput('fc-list')[1].split("\n"))

While some of the font-names may contain symbols which python may not be able to print properly.

Akshay Hazari
  • 3,186
  • 4
  • 48
  • 84
  • What imports are needed for this to work? – David Parks Mar 06 '23 at 18:30
  • @DavidParks You can use in Python3 `subprocess` module since the function getstatusoutput is moved there. https://docs.python.org/2/library/commands.html - `getstatusoutput() and getoutput() have been moved to the subprocess module.` – Akshay Hazari Mar 09 '23 at 17:43
3

Use families method of PyQt5.QtGui.QFontDatabase to get the different fonts available

QFontDatabase().families()

Below is a gui program to view different fonts

Program:

import sys
import textwrap

from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.title = "Available fonts"
        self.width = 500
        self.height = 500
        self.top = 10
        self.left = 10
        self.initUI()
        self.show()

    def initUI(self):
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.setWindowTitle(self.title)
        self.layout = QVBoxLayout()

        self.comboBox = QComboBox(self)
        self.families = QFontDatabase().families()
        print(type(self.families))
        self.comboBox.addItems(self.families)
        self.comboBox.setEditable(True)
        # Dont Add the new values to combobox
        self.comboBox.setInsertPolicy(QComboBox.NoInsert)
        # Autocompleting
        self.comboBox.completer().setCompletionMode(QtWidgets.QCompleter.PopupCompletion)
        song = ("""
            My Heart Will Go On\n
            by Celine Dion\n
            Every night in my dreams
            I see you, I feel you
            That is how I know you go on
            Far across the distance
            And spaces between us
            You have come to show you go on
            Near, far, wherever you are
            I believe that the heart does go on
            Once more, you open the door
            And you're here in my heart
            And my heart will go on and on
            Love can touch us one time
            And last for a lifetime
            And never let go 'til we're gone
            Love was when I loved you
            One true time I'd hold to
            In my life, we'll always go on
            Near, far, wherever you are
            I believe that the heart does go on
            Once more, you open the door
            And you're here in my heart
            And my heart will go on and on
            You're here, there's nothing I fear
            And I know that my heart will go on
            We'll stay forever this way
            You are safe in my heart and
            My heart will go on and """)
        # print(song)
        # when ever new item is selected from comboxbox call textChanged method
        self.comboBox.currentTextChanged.connect(self.textChanged)
        self.textEdit = QPlainTextEdit(song)
        self.layout.addWidget(self.comboBox)
        self.layout.addWidget(self.textEdit)
        # setting layout
        self.setLayout(self.layout)

    # Called when combo box current text is changed
    def textChanged(self):
        fontStr = self.comboBox.currentText()
        if fontStr in self.families:
            self.textEdit.setFont(QFont(fontStr))


if __name__ == "__main__":
    App = QApplication(sys.argv)
    window = Window()
    sys.exit(App.exec())

Output:

AvailabeFonts

Udesh
  • 2,415
  • 2
  • 22
  • 32
  • 1
    Thanks for including a fully working code sample! Arrow up/down in the combobox works too so it's easy to quickly navigate the font list one by one. I also changed `self.textEdit.setFont(QFont(fontStr))` to `self.textEdit.setFont(QFont(fontStr, 24))` to increase the font size. – ccpizza Feb 19 '22 at 13:43