My best solution here would be to turn the array into a string and then use win32clipboard to send it to the clipboard. This is not a cross-platform solution, but, again, Excel is not always available on any platform.
Excel uses tabs ( \t ) to indicate a column change, and \r\n to indicate a row change.
The relevant code would be:
import win32clipboard as clipboard def toClipboardForExcel(array): """ Copies an array into a string format acceptable by Excel. Columns separated by \t, rows separated by \n """ # Create string from array line_strings = [] for line in array: line_strings.append("\t".join(line.astype(str)).replace("\n","")) array_string = "\r\n".join(line_strings) # Put string into clipboard (open, clear, set, close) clipboard.OpenClipboard() clipboard.EmptyClipboard() clipboard.SetClipboardText(array_string) clipboard.CloseClipboard()
I tested this code with random form arrays (1000 10000), and it seems the biggest bottleneck is passing data to a function. (When I add the print statement at the beginning of the function, I still have to wait a bit before it prints anything.)
EDIT: The previous paragraph dealt with my experience with Python Tools for Visual Studio. In this environment, it looks like the expression about printing is delayed. In the direct command line interface, the bottleneck is in a loop, as expected.
source share