Python 如何将Jupyter笔记本中单元格的输出复制到剪贴板?
如何将Jupyter笔记本中单元格的输出复制到剪贴板,而不必通过拖放进行选择Python 如何将Jupyter笔记本中单元格的输出复制到剪贴板?,python,clipboard,jupyter-notebook,Python,Clipboard,Jupyter Notebook,如何将Jupyter笔记本中单元格的输出复制到剪贴板,而不必通过拖放进行选择 Jupyter笔记本在浏览器中运行,所以您可以使用一些javascript选择单元格并将其复制到剪贴板。经过一些尝试和错误,我想出了这个书签: javascript:(function%20()%20%7B%20function%20SelectText(element)%20%7B%20var%20range%3B%20var%20selection%3B%20if%20(document.body.createTe
Jupyter笔记本在浏览器中运行,所以您可以使用一些javascript选择单元格并将其复制到剪贴板。经过一些尝试和错误,我想出了这个书签:
javascript:(function%20()%20%7B%20function%20SelectText(element)%20%7B%20var%20range%3B%20var%20selection%3B%20if%20(document.body.createTextRange)%20%7B%20range%20%3D%20document.body.createTextRange()%3B%20range.moveToElementText(element)%3B%20range.select()%3B%20copy2clipboard(range.text%2C%20element.innerHTML)%3B%20document.getSelection().removeAllRanges()%3B%20%7D%20else%20if%20(window.getSelection)%20%7B%20selection%20%3D%20window.getSelection()%3B%20range%20%3D%20document.createRange()%3B%20range.selectNodeContents(element)%3B%20selection.removeAllRanges()%3B%20selection.addRange(range)%3B%20copy2clipboard(selection.toString()%2C%20element.innerHTML)%3B%20selection.removeAllRanges()%3B%20%7D%20%7D%3B%20function%20copy2clipboard(text%2C%20html)%20%7B%20function%20listener(e)%20%7B%20e.clipboardData.setData('text%2Fplain'%2C%20text)%3B%20e.clipboardData.setData('text%2Fhtml'%2C%20html)%3B%20e.preventDefault()%3B%20%7D%20document.addEventListener('copy'%2C%20listener)%3B%20document.execCommand('copy')%3B%20document.removeEventListener('copy'%2C%20listener)%3B%20%7D%3B%20%24('%23notebook-container').on('mouseenter'%2C%20'.input%2C%20.output_wrapper'%2C%20function%20()%20%7B%20if%20(%24(this).find('i%3Alast').length)%20%7B%20%24(this).find('i%3Alast').show()%3B%20%7D%20else%20%7B%20%24(this).css(%7B%20'position'%3A%20'relative'%20%7D).append(%24('%3Ci%20style%3D%22position%3Aabsolute%3B%20top%3A7px%3B%20left%3A%207px%3B%22%20class%3D%22fa-copy%20fa%22%3E%3C%2Fi%3E').on('click'%2C%20function%20()%20%7B%20SelectText(%24(this).parent().find('.input_area%2C%20.output')%20%5B0%5D)%3B%20%24(this).slideUp()%3B%20%7D))%3B%20%7D%20%7D)%3B%20%24('%23notebook-container').on('mouseleave'%2C%20'.input%2C%20.output_wrapper'%2C%20function%20()%20%7B%20%24(this).find('i%3Alast').hide()%3B%20%7D)%3B%20%7D)%20()%3B
将其添加到书签,并在笔记本页面上运行
工作原理
(function () {
function SelectText(element) {
var range;
var selection;
if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
copy2clipboard(range.text, element.innerHTML);
document.getSelection().removeAllRanges();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
copy2clipboard(selection.toString(), element.innerHTML);
selection.removeAllRanges();
}
};
function copy2clipboard(text, html) {
function listener(e) {
e.clipboardData.setData('text/plain', text);
e.clipboardData.setData('text/html', html);
e.preventDefault();
}
document.addEventListener('copy', listener);
document.execCommand('copy');
document.removeEventListener('copy', listener);
};
$('#notebook-container').on('mouseenter', '.input, .output_wrapper', function () {
if ($(this).find('i:last').length) {
$(this).find('i:last').show();
} else {
$(this).css({
'position': 'relative'
}).append($('<i style=\"position:absolute; top:7px; left: 7px;\" class=\"fa-copy fa\"></i>').on('click', function () {
SelectText($(this).parent().find('.input_area, .output') [0]);
$(this).slideUp();
}));
}
});
$('#notebook-container').on('mouseleave', '.input, .output_wrapper', function () {
$(this).find('i:last').hide();
});
}) ();
您可以尝试使用-将字符串复制到系统剪贴板的第三方软件包
给定的
import pyperclip as clip
# Sample Data
res = [(str(x*100), x) for x in range(1, 10)]
res
输出
[('100', 1), ('200', 2), ('300', 3),
('400', 4), ('500', 5), ('600', 6),
('700', 7), ('800', 8), ('900', 9)]
代码
clip.copy(f"{res}")
#clip.copy("{}".format(res)) # python < 3.6
clip.paste() # or Ctrl + V
我使用Jupyter实验室。可以右键单击要复制的输出单元格并选择 为输出创建新视图。这将把输出放在一个单独的屏幕上。在新输出屏幕上,您可以使用CRTL+C或右键单击进行复制
希望这会有所帮助。在下面的示例中,实际文本不会输出(尽管如果更改函数的第6行,可能会输出),而是显示一行确认,即剪贴板上有许多行可用 all_data_str是一个字符串,其内容将在剪贴板上可用,请编写名为dump_data_array的生成函数 当用户单击调用函数的按钮时,将触发粘贴操作:
def on_button_clipboard(b):
out_data.clear_output()
all_data_str=dump_data_array(da)
with out_data:
lineCount=all_data_str.count('\n')-1;
html_div = '<div id="pasting_to_clipboard">'+str(lineCount)+' lines pasted, you can now ^V into Excel</div>'
display(HTML(html_div))
js = """<script>
function copyToClipboard(text) {
var dummy = document.createElement("textarea");
document.body.appendChild(dummy);
dummy.value = text;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
}
</script>"""
display(HTML(js))
js2 = "<script>copyToClipboard(`" + all_data_str + "`);</script>"
display(HTML(js2))
def on_按钮_剪贴板(b):
输出数据。清除输出()
all_data_str=转储_data_数组(da)
在没有数据的情况下:
lineCount=所有数据结构计数('\n')-1;
html_div=''+str(lineCount)+'粘贴的行,现在可以^V到Excel中'
显示(HTML(HTML_div))
js=”“”
功能copyToClipboard(文本){
var dummy=document.createElement(“textarea”);
文件.正文.附件子文件(假人);
dummy.value=文本;
dummy.select();
文件。执行命令(“副本”);
document.body.removeChild(虚拟);
}
"""
显示(HTML(js))
js2=“copyToClipboard(`+all_data_str+”`);”
显示(HTML(js2))
在Ubuntu 19.10/Firefox浏览器上,我可以通过连续单击3次来选择输出单元格内容作为一个整体,然后像往常一样按住CTRL+C键进行复制。我同意-有没有办法只复制粘贴而不选择整个页面,如果你的jupyter服务器运行在另一台机器上,这将不起作用。@FranckDernoncourt我添加了更好的解决方案。天哪,你刚刚大大改善了我的生活!这还可以将表(pandas!)合理地复制并粘贴到Evernote和OneNote等工具中,您非常需要这些工具的HTML。我喜欢这一点!这里有一行代码:pd.DataFrame(['Copy me to clipboard'])。到剪贴板(index=False,header=False)
,这样您就不会创建保留的数据帧。这一点并不准确,因为您需要用鼠标进行选择,但我发现这很有用。您可以使用Shift
+鼠标右键
访问浏览器的本机上下文菜单。(适用于Chrome76,应该适用于大多数现代浏览器)。
[('100', 1), ('200', 2), ('300', 3),
('400', 4), ('500', 5), ('600', 6),
('700', 7), ('800', 8), ('900', 9)]
def on_button_clipboard(b):
out_data.clear_output()
all_data_str=dump_data_array(da)
with out_data:
lineCount=all_data_str.count('\n')-1;
html_div = '<div id="pasting_to_clipboard">'+str(lineCount)+' lines pasted, you can now ^V into Excel</div>'
display(HTML(html_div))
js = """<script>
function copyToClipboard(text) {
var dummy = document.createElement("textarea");
document.body.appendChild(dummy);
dummy.value = text;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
}
</script>"""
display(HTML(js))
js2 = "<script>copyToClipboard(`" + all_data_str + "`);</script>"
display(HTML(js2))