The following is a step-by-step guide for setting up your developer environment and getting you started using Python for scripting and automating file system operations on Windows. This article will cover setting up your environment to use some of the helpful libraries in Python that can automate tasks across platforms, like searching your file system, accessing the internet, parsing file types, etc.
When using Python to write scripts that perform file system operations, we recommend you install Python from the Microsoft Store. Installing via the Microsoft Store uses the basic Python3 interpreter, but handles set up of your PATH settings for the current user avoiding the need for admin access , in addition to providing automatic updates. If you are using Python for web development on Windows, we recommend a different setup using the Windows Subsystem for Linux.
Find a walkthrough in our guide: Get started using Python for web development on Windows. If you're brand new to Python, try our guide: Get started using Python on Windows for beginners.
We only recommend this if you are a more advanced Python programmer with a specific reason for choosing an alternative implementation. Go to your Start menu lower left Windows icon , type "Microsoft Store", select the link to open the store. Once the store is open, select Search from the upper-right menu and enter "Python".
Select which version of Python you would like to use from the results under Apps. We recommend using the most recent unless you have a reason not to such as aligning with the version used on a pre-existing project that you plan to work on.
Once you've determined which version you would like to install, select Get. Once Python has completed the downloading and installation process, open Windows PowerShell using the Start menu lower left Windows icon.
Once PowerShell is open, enter Python --version to confirm that Python3 has been installed on your machine. The Microsoft Store installation of Python includes pip , the standard package manager. Pip allows you to install and manage additional packages that are not part of the Python standard library. To confirm that you also have pip available to install and manage packages, enter pip --version. You will need to install the Microsoft Python extension in order to take advantage of the VS Code support features.
Learn more. In the top Search Extensions in Marketplace box, enter: Python. Find the Python ms-python. VS Code contains a built-in terminal that enables you to open a Python command line with PowerShell, establishing a seamless workflow between your code editor and command line. The downward scrolling happens because IDLE automatically scrolls down to the bottom after executing an instruction.
Enter this code instead:. The code then loops through numbers and adds each number to numbers , along with a newline. After pyperclip. Open a new file editor window and paste the text into it.
This will give you a large text window to try scrolling in. Enter the following code into the interactive shell:. This is important for this example because we want to the call to pyautogui. Note that while putting two commands on one line can be useful in the interactive shell, you should still have each instruction on a separate line in your programs.
After pressing ENTER to run the code, you will have five seconds to click the file editor window to put it in focus. Once the pause is over, the pyautogui. PyAutoGUI has screenshot features that can create an image file based on the current contents of the screen. In a Terminal window, run sudo apt-get install scrot to install this program. To take screenshots in Python, call the pyautogui. You can now call methods on the Image object in the im variable, just like any other Image object.
The return value from getpixel is an RGB tuple of three integers for the amount of red, green, and blue in the pixel. There is no fourth value for alpha, because screenshot images are fully opaque. Say that one of the steps in your GUI automation program is to click a gray button.
Before calling the click method, you could take a screenshot and look at the pixel where the script is about to click. Maybe the window moved unexpectedly, or maybe a pop-up dialog has blocked the button. The first and second arguments are integers for the x- and y-coordinates, and the third argument is a tuple of three integers for the RGB color the screen pixel must match.
This should return false. This method can be useful to call whenever your GUI automation programs are about to call click. Note that the color at the given coordinates must exactly match. If it is even slightly different—for example, , , instead of , , —then pixelMatchesColor will return False.
You could extend the mouseNow. Modify the code inside the while loop of mouseNow. You can use image recognition instead. For example, if you have previously taken a screenshot to capture the image of a Submit button in submit. To see how locateOnScreen works, try taking a screenshot of a small area on your screen; then save the image and enter the following into the interactive shell, replacing 'submit.
If the image cannot be found on the screen, locateOnScreen will return None. Note that the image on the screen must match the provided image perfectly in order to be recognized. If the image is even a pixel off, locateOnScreen will return None. If the image can be found in several places on the screen, locateAllOnScreen will return a Generator object, which can be passed to list to return a list of four-integer tuples.
There will be one four-integer tuple for each location where the image is found on the screen. Continue the interactive shell example by entering the following and replacing 'submit. If your image is only found in one area, then using list and locateAllOnScreen just returns a list containing one tuple.
Enter the following into the interactive shell, replacing the arguments with your own filename, four-integer tuple, and coordinate pair:. PyAutoGUI also has functions for sending virtual keypresses to your computer, which enables you to fill out forms or enter text into applications. What these keypresses do depends on what window and text field have focus.
You may want to first send a mouse click to the text field you want in order to ensure that it has focus. First, open a new file editor window and position it in the upper-left corner of your screen so that PyAutoGUI will click in the right place to bring it into focus.
Next, enter the following into the interactive shell:. This prevents you from accidentally bringing a new window into focus between the click and typewrite calls, which would mess up the example. Python will first send a virtual mouse click to the coordinates , , which should click the file editor window and put it in focus. The typewrite call will send the text Hello world!
You now have code that can type for you! By default, the typewrite function will type the full string instantly. However, you can pass an optional second argument to add a short pause between each character. This second argument is an integer or float value of the number of seconds to pause. For characters such as A or!
Not all keys are easy to represent with single text characters. Instead of a single string argument, a list of these keyboard key strings can be passed to typewrite. For example, the following call presses the A key, then the B key, then the left arrow key twice, and finally the X and Y keys:. Table lists the PyAutoGUI keyboard key strings that you can pass to typewrite to simulate pressing any combination of keys. You can also examine the pyautogui.
The same applies for 'ctrl' , 'alt' , and 'win' strings; they all refer to the left-side key. Keyboard key string. The mute, volume down, and volume up keys some keyboards do not have these keys, but your operating system will still be able to understand these simulated keypresses. Much like the mouseDown and mouseUp functions, pyautogui. They are passed a keyboard key string see Table for their argument. Run the following code, which will type a dollar sign character obtained by holding the SHIFT key and pressing 4 :.
If you need to type a string into a text field, the typewrite function is more suitable. But for applications that take single-key commands, the press function is the simpler approach. A hotkey or shortcut is a combination of keypresses to invoke some application function. Instead, use the pyautogui. Instead of making eight different function calls four keyDown calls and four keyUp calls , you can just call hotkey 'ctrl', 'alt', 'shift', 's'.
With a new IDLE file editor window in the upper-left corner of your screen, enter the following into the interactive shell in OS X, replace 'alt' with 'ctrl' :. This keyboard shortcut adds two characters to the current line, commenting it out. This is a useful trick to know when writing your own code in IDLE. Since this chapter covered many different functions, here is a quick summary reference:. Moves the mouse cursor to the given x and y coordinates. Moves the mouse cursor relative to its current position.
Moves the mouse cursor while the left button is held down. Moves the mouse cursor relative to its current position while the left button is held down. Simulates a click left button by default. Simulates a right-button click. Simulates a middle-button click. Simulates a double left-button click. Simulates pressing down the given button at the position x , y.
Simulates releasing the given button at the position x , y. Simulates the scroll wheel. A positive argument scrolls up; a negative argument scrolls down. Types the characters in the given message string. Types the given keyboard key strings. Presses the given keyboard key string.
Simulates pressing down the given keyboard key. Simulates releasing the given keyboard key. Simulates pressing the given keyboard key strings down in order and then releasing them in reverse order. Returns a screenshot as an Image object. See Chapter 17 for information on Image objects.
Of all the boring tasks, filling out forms is the most dreaded of chores. Although some applications will have an Import feature that will allow you to upload a spreadsheet with the information, sometimes it seems that there is no other way than mindlessly clicking and typing for hours on end. It looks like Figure Call pyautogui. Open a new file editor window and save it as formFiller. Before writing code, you need to figure out the exact keystrokes and mouse clicks that will fill out the form once.
The mouseNow. You need to know only the coordinates of the first text field. After clicking the first field, you can just press TAB to move focus to the next field. Nov 1, Dec 23, Fix disabling PEP warning D Dec 24, TravisCI: remove "sudo:required". May 17, Fix year in copyrights, add link to the Getting Started Guide.
Jul 3, Minor update in setup. Merge branch 'pywinauto64master'. May 25, Fix Link in Readme. Nov 19, Update change log for 0. Mar 2, Tune some codecov. Oct 30, Update of setup. Dec 3, Update makedocs. Sep 10, Update requirements.
0コメント