【问题标题】:Open external file-path links with spaces in QTextBrowser在 QTextBrowser 中打开带有空格的外部文件路径链接
【发布时间】:2021-04-30 17:01:28
【问题描述】:

我正在使用 Python 应用程序。在程序的某些部分,我通知用户创建了不同的文件。我在QTextBrowser 小部件中显示此信息。我希望这个文本是超链接的,所以如果用户点击超链接,文件会在外部应用程序中打开。如果文件路径没有空格,则链接有效 - 但如果路径有空格,则链接无效。

我阅读了很多关于此的问题,但我没有找到解决方案。

我写了这两个测试。

代码 1 - 我使用 QLabel 并且链接可以正常工作,但在 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_())

【问题讨论】:

  • QTextBrowser 无法呈现 pdf,因此您必须使用其他选项,例如 QWebEngineView
  • 非常感谢您的回答。但我不想只打开一个 pdf 文件。我希望 Windows 为我打开任何类型的文件。我发布的选项 1 符合我的要求。问题是文件名中是否有空格。
  • 引用看起来很乱。如果你想在双引号内获得一个单引号文件路径,试试这个:'&lt;a href="\'{}\'"&gt;Option_1&lt;/a&gt;'.format(file)
  • 谢谢@eyllanesc 为避免混淆,我将文档更改为 docx 而不是 pdf。
  • 谢谢@ekhumoro,我将您的答案显示为选项11,此选项不起作用(没有任何事情发生)

标签: python hyperlink pyqt5 qtextbrowser


【解决方案1】:

问题在于setOpenExternalLinks(True)不会使用 file: scheme 打开 url - 但您必须使用 file: scheme 打开包含空格的文件路径.要解决此问题,您可以使用自定义链接处理程序。以下脚本显示了如何做到这一点:

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

【讨论】:

  • 注意:这适用于我在 linux 上,但我还没有在 windows 上测试过。
  • 非常感谢!!!你给我解决方案。在 Windows 中,选项 2 不起作用,但选项 7,10 和 13 完美!!!!我会把代码放在我的答案中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-18
  • 2011-12-29
  • 1970-01-01
  • 2020-04-05
相关资源
最近更新 更多