【问题标题】:Using PyperClip on web app在 Web 应用程序上使用 PyperClip
【发布时间】:2015-10-21 13:24:16
【问题描述】:

我正在使用 pyperclip.py 使用表单在我的 Web 应用程序中获取电子邮件地址列表,以便用户可以通过剪贴板将其粘贴到本地。它在本地完美运行。但是,在服务器上运行它(Linux 14.04 和 Apache2)并通过浏览器从客户端系统访问时,它不会复制。如何将其复制到客户端系统的剪贴板?

现在我只是想让它工作,因此我只使用一行。我将 pyperclip 1.5.15 与 xclip 和 Python 3.4 一起使用。服务器正在运行 Linux 14.04,客户端注意到使用 Google Chrome 和 IE 在 Windows 8 和 Windows 10 上的问题。目前没有其他操作系统经过测试。

pyperclip.copy("HELLO") 

【问题讨论】:

    标签: python pyperclip


    【解决方案1】:

    由于我找不到有关此主题的许多详细信息,我想我会回答我的问题。不幸的是,浏览器似乎不会支持 pyperclip,因此需要 HTML + Javascript 解决方法(即在 pyperclip 上)。首先,从那里添加您的 Django 模板变量作为 HTML 属性,您可以使用 Javascript 来处理复制功能。下面是如何执行此操作的示例,提前抱歉,因为 stackoverflow 为示例提供了一些奇怪的格式。它还假设您有一个下面的表单,其 id 为 email_list_clipboard。我希望这对可能遇到类似问题的其他人有所帮助!

    例子:

        <html email-list="{{request.session.email_list}}">
        <script>
            $(document).ready(function () {
                function copyTextToClipboard(text) {
                    var textArea = document.createElement("textarea");
    
                    // Place in top-left corner of screen regardless of scroll position.
                    textArea.style.position = 'fixed';
                    textArea.style.top = 0;
                    textArea.style.left = 0;
    
                    textArea.style.width = '2em';
                    textArea.style.height = '2em';
    
                    // We don't need padding, reducing the size if it does flash render.
                    textArea.style.padding = 0;
    
                    textArea.style.border = 'none';
                    textArea.style.outline = 'none';
                    textArea.style.boxShadow = 'none';
    
                    textArea.style.background = 'transparent';
    
                    textArea.value = text;
    
                    document.body.appendChild(textArea);
    
                    textArea.select();
    
                    try {
                        var successful = document.execCommand('copy');
                        var msg = successful ? 'successful' : 'unsuccessful';
                console.log('Copying text command was ' + msg);
                    } catch (err) {
                        console.log('Oops, unable to copy');
                    }
    
                    document.body.removeChild(textArea);
                }
    
                // set things up so my function will be called when field_three changes
                $('#email_list_clipboard').click(function (click) {
                    event.preventDefault();
                    copyTextToClipboard(document.documentElement.getAttribute("email-list"));
        });
    
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2013-07-10
      • 2012-07-31
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 2011-08-15
      相关资源
      最近更新 更多