Log On
Log On

Reputation: 97

How do I write a copied value (ctrl + c) to an excel spreadsheet using Python?

Say I use the pyautogui module and use the hot keys to copy a value from a screen (in a database) and I now want to write that value to cell N2 in a spreadsheet how do I go about doing this?

I have used

pyautogui.hotkey('ctrl', 'c')

which has copied a numeric value.

Alternative suggestions also welcomed.

Upvotes: 0

Views: 1025

Answers (1)

Rajesh
Rajesh

Reputation: 25

You can get the copied value from clipboard(windows) using Tkinter

from Tkinter import Tk
clip = Tk() 
variable_name = clip.selection_get(selection = "CLIPBOARD")

Reference: How do I copy a string to the clipboard on Windows using Python?

Now open the excel file

pyautogui.hotkey('win','r')
pyautogui.typewrite(<excel_file_location>)
pyautogui.hotkey('enter')

Now we can go to A1 cell first and then we can traverse to any cell we want

pyautogui.hotkey('ctrl','home') # traversing to A1 
pyautogui.typewrite(['down'],interval=0.25) # going to A2   
pyautogui.typewrite(['tab','tab',..13 times],interval=0.25) # going to N2   
pyautogui.typewrite(variable_name,interval=0.25) # entering the copied value

Upvotes: 2

Related Questions