pyautogui.write not recognizing \t as tab presses

Viewed 173

Goal:

I'm trying to read a product number (such as A11841-2 - -) from an Excel cell using openpyxl and write it into a field in a database window using pyautogui.write. My original code (https://pastebin.com/6EUFQ09s):

import openpyxl

wb = openpyxl.load_workbook('repack_locns.xlsx')
sheet = wb['r_locn']
sku = sheet.cell(row=2, column=1).value
 
#5 Enter product code into PKMS (cursor should already be where it's supposed to be)
    #and press Enter
import pyautogui
pyautogui.write(sku)

did not write the information into the correct fields (the number 2 after the first hyphen in product code A11841-2 - - should be in the suffix field, not in the style field as is pictured in 1). [Note: I'm using Mu 1.1.0.alpha.2 to write my code in and run it from]

What I did:

I started by finding and replacing the hyphens in the product numbers with \t within Excel, resulting in product codes like this: A11841\t2 \t \t. When I simply copy the product number from the excel cell and paste it into the code (modification is on line 34 at https://pastebin.com/sX1SdV8Q):

import openpyxl

wb = openpyxl.load_workbook('repack_locns.xlsx')
sheet = wb['r_locn']
sku = 'A11841\t2 \t  \t'
 
#5 Enter product code into PKMS (cursor should already be where it's supposed to be)
    #and press Enter
import pyautogui
pyautogui.write(sku)

and put a single quote on each end, it works! It reads the \t as tab presses and enters the product code into the correct fields (2). I then used my original code to read from a copy of the original Excel file that had this formatting change.

The problem:

When the product number is written by pyautogui, it’s not recognizing the \t as tab presses. Instead, it’s just writing them as characters (3).

What I’ve tried:

I used pyperclip to try and include that copy/paste step in my code, so I could eventually make it repeatable. I used openpyxl to pull from the Excel cell and then concatenated single quotes on each end. However, it didn’t work; the same issue arose where the pyautogui.write function doesn’t recognize the \t as tab presses and simply writes them as characters.

I used type() and both what is written by pyautogui.write and what I copy/paste directly from Excel (and then enclose with single quotes) are classified as strings. Not sure why the \t are not being treated the same in each case.

1 Answers

LESSON LEARNED: CHECK OUTPUT LINE BY LINE IN YOUR PROGRAM WHEN YOU'RE HAVING PROBLEMS

It was simple, but I'm a new programmer so I didn't catch it nearly as quickly as I could have lol

sheet.cell(row=2, column=1).value

Turns out that piece of code spits out 'A11841\\t2 \\t \\t' not 'A11841\t2 \t \t'. Makes total sense, since it's reading the text A11841\t2 \t \t from an Excel cell. I simply used join() and split() and then pyautogui.write() and it worked!:

sku = sheet.cell(row=2, column=1).value
sku_='\t'.join(sku.split('\\t'))
pyautogui.write(sku_)

As you can see in [1]1, pyautogui.write() correctly interpreted \t as a tab press.

Related