代码之家  ›  专栏  ›  技术社区  ›  Alex

python:将svg字符串复制到Windows剪贴板中的“image/svg+xml”格式,以便粘贴为svg图像

  •  1
  • Alex  · 技术社区  · 5 年前

    假设我有这样一个SVG文件的XML内容:

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
     "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
    <!-- Created with matplotlib (http://matplotlib.org/) -->
    <svg height="344.88pt" version="1.1" viewBox="0 0 460.8 344.88" width="460.8pt" 
    xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    ...
    

    我的问题是,如何将此字符串复制到Windows剪贴板,而不是作为字符串,而是作为“image/svg+xml”数据类型,可以将其作为svg图像粘贴到PowerPoint中?

    1 回复  |  直到 5 年前
        1
  •  4
  •   Luca Angioloni Praveen Dhakad    5 年前

    我所知道的唯一支持mime类型的剪贴板库是qtclapboard: https://doc.qt.io/qtforpython/PySide2/QtGui/QClipboard.html

    你可能想看看。

    可能在您可以设置的mime类型中 setMimeData 也有 图像/SVG+XML

    例子:

    from PyQt5 import QtCore
    d = QtCore.QMimeData()
    print(d.formats())  # Now it is an empty list "[]" because you still didn't set the data and the mime type
    
    # Set data in the object with the required mime type
    d.setData("image/svg+xml", b'<?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><!-- Created with matplotlib (http://matplotlib.org/) --><svg height="344.88pt" version="1.1" viewBox="0 0 460.8 344.88" width="460.8pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">')
    
    print(d.formats())  # Now it prints: ['image/svg+xml']
    

    从这里你可以把它拿到Qclipboard。

    我引用文件: http://doc.qt.io/archives/qt-4.8/qclipboard.html

    setmimedata()函数是灵活性的极限:它允许 要将任何qmimedata添加到剪贴板中。