1

I am using pyperclip.py to grab a list of E-Mail Addresses in my web app using a form so a user can paste it locally via clipboard. It works perfect locally. However, while running it on a server (Linux 14.04 with Apache2) and accessed from a client system through the browser it doesn't copy. How can I get it to copy to the clipboard of the client's system?

Right now I'm just trying to get it to work and as such I'm only using a single line. I'm using pyperclip 1.5.15 with xclip and Python 3.4. The server is running Linux 14.04 and the client has noticed issues on Windows 8 and Windows 10 using Google Chrome and IE. No other os has currently been tested.

pyperclip.copy("HELLO") 
4

1 回答 1

2

由于我找不到有关此主题的许多详细信息,因此我想我会回答我的问题。不幸的是,浏览器似乎不会支持 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>
于 2015-10-21T16:00:26.403 回答