This book was produced in EPUB format by the Internet Archive. The book pages were scanned and converted to EPUB format automatically. This process relies on optical character recognition, and is somewhat susceptible to errors. The book may not offer the correct reading sequence, and there may be weird characters, non-words, and incorrect guesses at structure. Some page numbers and headers or footers may remain from the scanned page. The process which identifies images might have found stray marks on the page which are not actually images from the book. The hidden page numbering which may be available to your ereader corresponds to the numbered pages in the print edition, but is not an exact match; page numbers will increment at the same rate as the corresponding print edition, but we may have started numbering before the print book's visible page numbers. The Internet Archive is working to improve the scanning process and resulting books, but in the meantime, we hope that this book will be useful to you. The Internet Archive was founded in 1996 to build an Internet library and to promote universal access to all knowledge. The Archive's purposes include offering permanent access for researchers, historians, scholars, people with disabilities, and the general public to historical collections that exist in digital format. The Internet Archive includes texts, audio, moving images, and software as well as archived web pages, and provides specialized services for information access for the blind and other persons with disabilities. Created with hocr-to-epub (v.0.0.1) AUTOMATE THE BORING STUFF WITH PYTHON PRACTICAL PROGRAMMING FOR TOTA L BEGINNERS AUTOMATE THE BORING STUFF WITH PYTHON AUTOMATE THE BORING STUFF WITH PYTHON Practical Programming for Total Beginners by Al Sweigart San Francisco AUTOMATE THE BORING STUFF WITH PYTHON. Copyright © 2015 by Al Sweigart. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed in USA Second printing 1918171615 23456789 is BN, ® SUSTAINABLE Certified Sourcing ISBN-10: 1-59327-599-4 | INITIATIVE www.sfiprogram.org | ISBN-13: 978-1-59327-599-0 Naa SEE E Publisher: William Pollock Production Editor: Laurel Chun Cover Illustration: Josh Ellingson Interior Design: Octopod Studios Developmental Editors: Jennifer Griffith-Delgado, Greg Poulos, and Leslie Shen Technical Reviewer: Ari Lacenski Jopyeditor: Kim Wimpsett Compositor: Susan Glinert Stevens Proofreader: Lisa Devoto Farrell Indexer: BIM Indexing and Proofreading Services For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 415.863.9900; [email protected] www.nostarch.com Library of Congress Control Number: 2014953114 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. For my nephew Jack About the Author Al Sweigart is a software developer and tech book author living in San Francisco. Python is his favorite programming language, and he is the developer of several open source modules for it. His other books are freely available under a Creative Commons license on his website http://www inventwithpython.com/. His cat weighs 14 pounds. About the Tech Reviewer Ari Lacenski is a developer of Android applications and Python software. She lives in San Francisco, where she writes about Android programming at hitp://gradlewhy.ghost.io/ and mentors with Women Who Code. She’s also a folk guitarist. The text on this page is estimated to be only 45.24% accurate BRIEF CONTENTS Acknowledgments: 6:0 se4!souekgeatey cukdleeile cea dulehe dine deeded ay gay) xxiii IntroduchOn zeta 0U Ss hath innt ok S Raha eae ol atik Me ake Ge ote Col ee Ae 1 PART I: PYTHON PROGRAMMING BASICS. .......... ccc cece cece eee eeeee 11 Chapter: t: PythoniBasi¢s 24-3 o oian a cae ke teh a y ae Ca athens 13 Chapter.2: Flow Control seit sh Se haha a a r aa a a Sch ae 31 Chapter 3s Functions 34) eae oud e ee aretae eee tea ae ee aas 61 ChapterA: listss..20724 abe oho ead e sanded hee toe ed eee ihe sea) 79 Chapter 5: Dictionaries and Structuring Data... 2... te eee 105 Chapter 6: Manipulating Strings .. 0.0.0... 0. eect eee eee 123 PART Il: AUTOMATING TASKS» o.)05 0:.0)0 oa Se See See ele wide dele is eee als 145 Chapter 7: Pattern Matching with Regular Expressions... 2.0.0.0... cece eee eee 147 Chapter 8: Reading and Writing Files... 0.0.0... eee eee 173 Chapter 9: Organizing Files |... naana aaae 197 Chapter 10: Debugging reris s inniinn ese el tea ae epee ae 215 Chaptér 4-14 Web Scraping e ereet e a e a a iaa e ega 233 Chapter 12: Working with Excel Spreadsheets .......... 0.0.0 cece eee eee eee 265 Chapter 13: Working with PDF and Word Documents... ......0.0 000 c eee ee eee 295 Chapter 14: Working with CSV Files and JSON Data... 0.0... ee eee 319 Chapter 15: Keeping Time, Scheduling Tasks, and Launching Programs.............. 335 Chapter 16: Sending Email and Text Messages... 0.2.00... eee eee eee eee eee 361 Chapter 17: Manipulating Images ......... 00.0 cece eee eee eee 387 Chapter 18: Controlling the Keyboard and Mouse with GUI Automation ............. 413 The text on this page is estimated to be only 52.54% accurate Appendix A: Installing Third-Party Modules UAL thd eR te A a A ek he at 441 Appendix B: Running Programs... 1.0... ec eee eens 443 Appendix C: Answers to the Practice Questions. . 0.0... cece eee 447
Description: