1

I am trying to use PyAutoGUI (Python 3.7 64-bit) to copy a column of cells in Excel from one sheet to another.

The code is:

import pyautogui as gui
gui.PAUSE = 1
gui.FAILSAFE = True
gui.keyDown('shift')
gui.typewrite(['down']*67)
gui.keyUp('shift')
gui.keyDown('ctrl')
gui.press('c')
gui.keyUp('ctrl')

which should hold down shift, click the down arrow 67 times to select the cells I want, then release shift. Following this, the text will be copied to the clipboard.

When run (with the top cell pre-selected), the delay of each step is noticeable, but it is as if shift is not held down. The selected cell moves to the bottom, but the previous cells are not selected. At the bottom, the last cell is copied.

It seems as though 'ctrl' is working, but 'shift' is not. If I stop before running keyUp on shift, when the program ends, my computer is act as if shift is being help down until I press the key again. I have also tried using 'shiftright' in case that was the issue but it made no difference. There are a number of PyAutoGUI functions run before these, but none have to do with the state of these keys.

I have also been looking at alternatives to select these cells without using shift but haven't been able to find anything.

Does anyone have suggestions on this?

Corina
  • 844
  • 7
  • 15
Jordan
  • 33
  • 6
  • Seeing your question doesn't have an answer yet and a lot of similiair questions (like mine) will never be answered. Were you using windows? – Kerwin Sneijders Nov 26 '18 at 23:13
  • 2
    Does this answer your question? [keyDown function not working with shift key](https://stackoverflow.com/questions/56949628/keydown-function-not-working-with-shift-key) – Ari Cooper-Davis Aug 17 '21 at 16:07

2 Answers2

0

keyDown function not working with shift key

the solution for your problem it's on the link

You need to use the 'shiftleft' and 'shiftright' the same time

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
-1

Having the same issue. Not sure if this is much of an answer but I think I will simply create an excel macro and then run that via pyautogui.

Macro:

Sub Select()
'
' Select Macro
'

'
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
End Sub