系统中Python获取可用字体列表

3

如何获取系统中所有可用的字体(Linux)?

我搜索并找到了这个链接:http://www.lemoda.net/pango/list-fonts/,但它是使用C编写的,并且依赖于Pango。

那么如何用Python实现呢?


http://pyqt.sourceforge.net/Docs/PyQt4/qfontdatabase.html#families - xrisk
使用Matplotlib怎么样?https://dev59.com/KWMk5IYBdhLWcg3w8iaC - skycrew
在Linux中,所有的字体都放在/usr/share/fonts/$HOME/.fonts中。你可以使用os.walk()获取所有文件的列表。或者你也可以使用其他包,比如PyQT或Matplotlib。 - Remi Guan
@wong2 fc-list 有帮助吗? - Akshay Hazari
2个回答

3

您可以在Ubuntu上使用fc-list命令,然后通过":"分割每一行来获取所有的字体。具体操作请参考http://manpages.ubuntu.com/manpages/hardy/man1/fc-list.1.html

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

尽管一些字体名称可能包含Python无法正确打印的符号。


这需要哪些导入才能正常工作? - David Parks
@DavidParks дҪ еҸҜд»ҘеңЁPython3дёӯдҪҝз”ЁsubprocessжЁЎеқ—пјҢеӣ дёәеҮҪж•°getstatusoutputе·Із»Ҹ移еҠЁеҲ°иҜҘжЁЎеқ—дёӢгҖӮhttps://docs.python.org/2/library/commands.html - getstatusoutput()е’Ңgetoutput()е·Із»Ҹ被移еҲ°subprocessжЁЎеқ—дёӢгҖӮ - Akshay Hazari

3

使用 PyQt5.QtGui.QFontDatabasefamilies 方法获取可用的不同字体。

QFontDatabase().families()

下面是一个GUI程序,用于查看不同的字体

程序:

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())

输出:

可用字体


1
感谢您提供一个完全可用的代码示例!组合框中的向上/向下箭头也可以使用,因此可以轻松地逐个浏览字体列表。我还将 self.textEdit.setFont(QFont(fontStr)) 更改为 self.textEdit.setFont(QFont(fontStr, 24)) 以增加字体大小。 - ccpizza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接