Python——将字符串添加到剪贴板,该字符串将以正确的格式粘贴到Excel(即,分隔成行/列)



我已经看到了这个问题的一些答案,但在VBA和C#中——在python中没有。在python 3中,我尝试做以下操作:

  1. 为变量分配一个具有适当"excel"格式的值字符串
  2. 然后将此变量复制到剪贴板
  3. 然后,用户应该能够手动CTRL+V(粘贴(该字符串到excel中,并将其分离到正确的行/列位置

我的项目同时使用tkinter和openpyxl,通过测试我得到了一些结果,但这不是我想要的。我得到的最接近的是下面。

# If I first go into excel, 
# and copy a few columns within a single row with different data in them, 
# then I run the following code: 
import tkinter as tk
root = tk.Tk()
paste = root.clipboard_get()
print(paste)
# Then I manually copy the print output to the clipboard,
# and then go back to excel and paste, 
# excel gives the following warning message:
# "The data you're pasting isn't the same size as your selection.  do you want to paste anyways?"
# And after pressing OK, it seems as though the data is pasted properly. 

这是可以的,打印(粘贴(字符串可能会帮助我为要生成的字符串的初始变量创建适当的格式,但我需要一个不会导致Excel每次都弹出此警告标志的解决方案。

如果有人能对此提供一些见解,我将不胜感激。此外,解决方案需要通过python,而不是通过修改Excel。其次,解决方案不是将数据直接附加/写入Excel(通过Openpyxl等(,而是将数据以适当的格式写入剪贴板(我使用的是Excel 2016(。

谢谢!!


编辑:

我也尝试过使用用户kichik在这里提供的"ctypes解决方案"。

为了让这个解决方案更容易使用,我下载了一个名为"InsideClipboard"的应用程序,它可以让我轻松地查看剪贴板"复制"数据的每种格式的格式ID。

使用kichik的ctype解决方案,我检查了手动复制剪贴板中存储的不同格式的打印输出是否可以让我手动将CTRL+V按原始格式复制到Excel,但失败了。最初的"副本"是Excel中单行字符串中的多列字符串,手动将单个格式"粘贴"回Excel将所有字符串保存在一个单元格中(创建多行数据的HTML格式除外,这也是错误的(。不同的格式包括CF_LOCALE(16(、CF_TEXT(1(、CF_OEMTEXT(7(、CF_UNICODETEXT(13(和AND HTML格式(49384(。

所以,仍然:没有解决方案。


第2版:

我意识到我可以在python中创建字符串,在字符串之间按下实际的制表符,而不是使用\t,并且它可以创建一个字符串,当粘贴到Excel中时,它可以将数据放置到单行的不同列中。

此外,我意识到,如果我直接在Excel中按CTRL+V(不在行标题上(,在我想粘贴数据的实际行上,我将不再收到Excel的"警告消息"。因此,使用这种变通方法可能会奏效。如果没有人有任何输入,那么这种简单的方法可能就足够了。

然而,我希望能够简单地单击行标题而不是第一个相关的行单元格来粘贴数据,而不会弹出Excel警告。想法仍然是受欢迎的,最好通过python完成这一切(不修改Excel,因为该应用程序可能在不同的Windows操作系统PC上运行(。

所以:可能的简单解决方案,但并不完美。


第3版:

所以我在Edit2的基础上制定了一个解决方案。如果Excel在工作计算机上作为应用程序打开,则仍会出现Excel"警告"弹出窗口;但是,如果Excel文件是通过在线处理器打开和编辑的,则用户可以突出显示行标题并粘贴,而不会生成Excel"警告"弹出窗口。这在我的特定情况下有效,尽管更好的解决方案是在任何情况下都不要让复制的数据生成Excel"警告"弹出窗口。无论如何,如果没有人知道如何通过python单独阻止Excel警告弹出,那么我仍然可以使用这种方法。

以下是我的解决方案(注意,长字符串中的空格实际上是"制表符"(:

import pyperclip
# t variables are defined prior to the below code
# note: the "   " parts in the data string are actually tabs
data = t1 + "   " + t2 + "  " + "   " + t3 + "  " + t4 + "  " + t5 + "  " + t6 + "  " + t7 + "  "  + "  " + "t8"  + "   "  + "  "  + "  "  + "  "  + "  "  + "  " + t9 + "  " + t10 + " " + t11 + " " + t12 + " "  + "  "  + "  "  + "  "  + "  "  + "  " + t13
pyperclip.copy(data)
print("clipboard:", data)
# now manually pressing CTRL+V while inside Excel (online processor) will
# paste the various t variables in the appropriate cells designated by 
# the "   " tabs in between.  Of note, this works for columns of a single
# row as I didn't need multiple rows of data for my task.

下面是在tkinter中创建一个5行乘3列的条目网格的代码。按下复制按钮将条目的内容以制表符/换行符分隔的字符串复制到剪贴板,然后可以粘贴到excel中。

import tkinter as tk
from tkinter import ttk
ROWS = 5
COLS = 3
root = tk.Tk()
rows = []  # Container for Entry widgets.
# Create and grid the Entry widgets.
for row in range( ROWS ):
temp = []
for col in range( COLS ):
temp.append( ttk.Entry(root, width = 10 ))
temp[-1].grid( row = row, column = col )
rows.append( temp )
def entries_to_lists( rows ):
list_out = []
for row in rows:
temp = []
for col in row:
temp.append( col.get() )
list_out.append( temp )
return list_out
def string_out( rows ):
""" Prepares a 't', 'n' separated string to send to the clipboard. """
out = []
for row in rows:
out.append( 't'.join( row ))  # Use 't' (tab) as column seperator.
return 'n'.join(out)              # Use 'n' (newline) as row seperator.
def do_copy():
data = entries_to_lists( rows )
root.clipboard_clear()
root.clipboard_append( string_out( data ))     # Paste string to the clipboard
root.update()   # The string stays on the clipboard after the window is closed
ttk.Button( text = "  Copy  ", command= do_copy ).grid( column = 1 )
# Button to trigger the copy action.
root.title("Copy to Excel Test")
root.geometry("400x200+10+10")
root.mainloop()

这复制到Windows 10中的Excel 2013中,没有任何警告消息。

string_out将从字符串列表的任何列表生成合适的字符串,即2d列表。

最新更新