在 QTextBrowser 中打开带有空格的外部文件路径链接

Open external file-path links with spaces in QTextBrowser

我在 Python 应用程序中工作。在程序的某些部分,我通知用户创建了不同的文件。我在 QTextBrowser 小部件中显示此信息。我希望此文本是 hyperlinked,因此如果用户单击 hyperlink,该文件将在外部应用程序中打开。如果文件路径没有空格,links 起作用 - 但如果路径有空格,links 不起作用。

我看了很多关于这个的问题,但我没有找到解决方案。

我写了这两个测试

代码 1 - 我使用 QLabel 并且 link 完美运行,但在 QTextBrowser 中它在浏览器中打开。

选项 1 有效,但其余选项无效,因为路径中有空格。

选项 7 和 10,在浏览器中打开文件。

代码 1:

import sys

from PyQt5.QtWidgets import QApplication, QTextBrowser,QTextEdit,QLabel
from PyQt5.QtCore import QUrl

app = QApplication(sys.argv)
label=QLabel()
file_2='c:/temp/test 2/test.docx'
urlLink="<a href='file:///%s'>'Option_12'</a>"%(file_2)
label.setText(urlLink)
label.setOpenExternalLinks(True)
label.show()
sys.exit(app.exec_())

代码 2:

import sys

from PyQt5.QtWidgets import QApplication, QTextBrowser,QTextEdit
from PyQt5.QtCore import QUrl

if __name__ == '__main__':
    app = QApplication(sys.argv)
    text_area = QTextBrowser()
    file='c:/temp/test.docx'
    link='<a href='"'{}'"'>Option_1</a>'.format(file)
    text_area.insertHtml(link)
    
    
    file_2='c:/temp/test 2/test.docx'
    link='<br><a href='"'{}'"'>Option_2</a></br>'.format(file_2)
    text_area.insertHtml(link)

    file_2_reformated=file_2.replace(" ", "\ ")
    link='<br><a href='"'{}'"'>Option_3</a></br>'.format(file_2_reformated)
    text_area.insertHtml(link)

    file_2_reformated=file_2.replace(" ", "%20")
    link='<br><a href='"'{}'"'>Option_4</a></br>'.format(file_2_reformated)
    text_area.insertHtml(link)

    file_2_reformated=chr(34)+file_2+chr(34)
    link='<br><a href='"'{}'"'>Option_5</a></br>'.format(file_2_reformated)
    text_area.insertHtml(link)

    file_2_reformated = " \"" + file_2 + " \""
    link='<br><a href='"'{}'"'>Option_6</a></br>'.format(file_2_reformated)
    text_area.insertHtml(link)

    link='<br><a href='"'file:///{}'"'>Option_7</a></br>'.format(file_2)
    text_area.insertHtml(link)

    link='<br><a href='"'https://{}'"'>Option_8</a></br>'.format(file_2)
    text_area.insertHtml(link)

    file_3="file:///c:/temp/test 2/test.docx"
    link='<br><a href='"'https://{}'"'>Option_9</a></br>'.format(file_3)
    text_area.insertHtml(link)

    file_2='c:/temp/test 2/test.docx'
    link = '<br><a href="{}">Option_10</a></br>'.format(QUrl.fromLocalFile(file_2).toString())
    text_area.insertHtml(link)

    file_2='c:/temp/test 2/test.docx'
    link = '<br><a href="\'{}\'">Option_11</a></br>'.format(QUrl.fromLocalFile(file_2).toString())
    text_area.insertHtml(link)


    from pathlib import PureWindowsPath

    file_3 = PureWindowsPath("c:/temp/test 2/test.docx")
    link = '<br><a href="{}">Option_13</a></br>'.format(file_3.as_uri())
    text_area.insertHtml(link)
    
    text_area.setOpenExternalLinks(True)
    text_area.show()
    sys.exit(app.exec_())
 

@ekhumoro 的解决方案

import sys
from PyQt5.QtWidgets import QApplication, QTextBrowser
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QUrl

if __name__ == '__main__':

    app = QApplication(sys.argv)

    text_area = QTextBrowser()
    text_area.setOpenLinks(False)

    def handle_links(url):
        if not url.scheme():
            url = QUrl.fromLocalFile(url.toString())
        QDesktopServices.openUrl(url)

    text_area.anchorClicked.connect(handle_links)

    file='c:/temp/test.docx'
    link='<a href='"'{}'"'>Option_1</a>'.format(file)
    text_area.insertHtml(link)   

    file_2='c:/temp/test 2/test.docx'    
    link='<br><a href='"'file:///{}'"'>Option_7</a></br>'.format(file_2)
    text_area.insertHtml(link)

    link = '<br><a href="{}">Option_10</a></br>'.format(QUrl.fromLocalFile(file_2).toString())
    text_area.insertHtml(link)

    from pathlib import PureWindowsPath

    file_3 = PureWindowsPath("c:/temp/test 2/test.docx")
    link = '<br><a href="{}">Option_13</a></br>'.format(file_3.as_uri())
    text_area.insertHtml(link)

    text_area.show()
    sys.exit(app.exec_())

问题是 setOpenExternalLinks(True) 不会 打开带有文件的 url: scheme - 但你 必须 使用文件: 用于打开包含空格的文件路径的方案。要解决此问题,您可以使用自定义 link 处理程序。以下脚本显示了如何执行此操作:

import sys
from PyQt5.QtWidgets import QApplication, QTextBrowser
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QUrl

if __name__ == '__main__':

    app = QApplication(sys.argv)

    text_area = QTextBrowser()
    text_area.setOpenLinks(False)

    def handle_links(url):
        if not url.scheme():
            url = QUrl.fromLocalFile(url.toString())
        QDesktopServices.openUrl(url)

    text_area.anchorClicked.connect(handle_links)

    file = 'c:/temp/test.docx'
    link = '<a href="{}">Option_1</a>'.format(file)
    text_area.insertHtml(link)

    file_2 = 'c:/temp/test 2/test.docx'
    link = '<br><a href="{}">Option_2</a></br>'.format(file_2)
    text_area.insertHtml(link)

    text_area.show()
    sys.exit(app.exec_())