Python Programs / Snippits
    
    
    Programs
    
        
        
            Group filename renaming tool.
        
     
    
        
        
            Get all game names from Steam profile.
        
     
        
    
    Built-In Functions
    
        Import by typing "
import moduleName." Import multiple by separating by commas.
            
        Import all of a module by typing "
from moduleName import *," the asterisk being a wildcard. 
        Allows you to avoid typing the module name everything.
            
    
First Party Modules
        
            logging
            # Used for error heirarchy and logging.
        
        
            sys
            # System. Allows the use of 'sys.argv' variables.
        
        
            traceback
            # Allows the recording of machine-generated errors.
        
        
            webbrowser
            # Provides an interface to allow displaying web-based documents to users.
        
    Third Party Modules
        
            bs4
            # Beautiful Soup. Import as bs4. Allows parsing of page HTML.
        
        
            openpyxl
            # Allows for the reading, creating and editing of xlsx files.
        
        
            PyPDF2
            # Add, remove or reoder PDF pages. Can't change individual text, color, font, etc.
        
        
            pyperclip
            # Allows for the copying and pasting of the clipboard.
        
        
            python-docx
            # Import as just 'docx.' Allows for editing and creation of docx files.
        
        
            requests
            # Allows you to get information off of the Internet.
        
        
            selenium
            # Import as 'from selenium import webdriver'. Lets you take control a browser.
        
    Error Logging
        
            
                assert
                # Provide a condition and it will raise an error if that condition is met.
            
            
                open('error_log.txt', 'a')
                # Create a new text document. 'a' = Append mode.
            
            
                raise
                # raise Exception('Text.')
            
            
                logging.debug('Text.') | info | warning | error | critical
                # Logs information at varying severities.
            
            
                logging.basicConfig(filename='myProgramLog.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
                # Needed line to produce logs. Filename optional.
            
            
                logging.disable(logging.CRITICAL)
                # Disables logging messages at 'CRITICAL' or less severity.
            
         
        
            
            import traceback
                
            
            raise Exception('This is the error message.')
                
            
            try:
	            raise Exception('This is the error message.'
            except:
	            
                errorFile = open('error_log.txt', 'a')
                
                errorFile.write(traceback.format_exc())
                
                errorFile.close()
                
                print('The traceback info was written to error_log.txt')
                
            
            
            assert False, 'This is the error message.'
                
            
            import logging
                
            
            logging.basicConfig(filename='filename.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
                
            
            
            logging.disable(logging.CRITICAL)
        
    
    Excel
        
            
                openpyxl.load_workbook('file.xlsx')
                # Opens an existing file. Assign to a Workbook object.
            
            
                openpyxl.Workbook
                # Creates a new Workbook file in memory. Assign to a Workbook object.
            
            
                wb['Sheet']
                # Assign to a Sheet object.
            
            
                wb.sheetnames
                # List of all Sheets in a Workbook.
            
            
                wb.create_sheet(index=0, title='My Other Sheet')
                # Create, name and position a sheet.
            
            
                sheet['A1']
                # A Cell's value. Assign to a Cell object.
            
            
                sheet['A1'].value
                # A specific Cell's value.
            
            
                sheet.cell(row=1, column=2)
                # Another way to view a cell's value.
            
         
        
            import openpyxl
                
            
            wb = openpyxl.load_workbook('Excel_Filename.xlsx')
                
            
            wb = openpyxl.Workbook
                
            
            wb.sheetnames
                
            
            wb.create_sheet(index=0, title='My Other Sheet')
                
            
            sheet2 = wb.create_sheet()
                
            
            sheet2.title = 'My New Sheet Name'
                
            
            sheet = wb['Sheet_Name']
                
            
            cell = sheet['A1']
                
            
            cell.value
                
            
            cell = 42
                
            
            sheet.cell(row=1, column=2)
                
            
            wb.save('Excel_Filename.xlsx')
        
    
    PDFs
        
            
                open('example.pdf', 'rb')
                # Open a PDF. Pass into a 'pdfFile' object. 'rb' = Read Binary mode.
            
            
                open('new_pdf_name.pdf', 'wb')
                # Open a PDF. Pass to an 'outputFile' object. 'wb' = Write Binary mode.
            
            
                writer.write(outputFile)
                # Saves the PDF.
            
            
                PyPDF2.PdfFileReader(pdfFile)
                # Pass file to PyPDF2. Pass into a 'reader' object.
            
            
                PyPDF2.PdfFileWriter()
                # Allows you to write PDFs. Pass to a 'writer' object.
            
            
                writer.addPage(page)
                # Adds PDF pages to the 'writer' object.
            
            
                reader.numPages
                # Displays the number of pages in the PDF.
            
            
                reader.getPage(0)
                # Gets a specific page. Pass into a 'page' object.
            
            
                page.extractText()
                # Rips the text from the 'page' object.
            
         
        
            import PyPDF2
                
            
            pdfFile = open('filename.pdf', 'rb')
                
            
            reader = PyPDF2.PdfFileReader(pdfFile)
                
            
            reader.numPages
                
            
            page = reader.getPage(0)
                
            
            page.extractText()
        
    
    Web Scraping
        
            
                requests.get('File or Page')
                # Gets a copy of a file. Pass into a 'Response' object.
            
            
                res.status_code
                # Checks if file downloaded. 200 for success, 404 for missing, etc.
            
            
                res.raise_for_status()
                # Causes an error if there's a problem with the downloaded ojbect.
            
            
                res.text
                # Displays the text stored in the 'Response' object.
            
            
                res.iter_content(Byte Amount)
                # Iterates through a 'Chunk' of content by the amount of Byte's you choose.
            
            
                sys.argv
                # Captures a list of command line arguments.
            
            
                webbrowser.open('URL')
                # Opens up the specified web page.
            
            
                file = open('filename', 'wb')
                # Opens a file in a 'File' object. 'wb' = Write Binary mode.
            
            
                bs4.BeautifulSoup(res.text, 'html.parser')
                # Creates a 'Soup' object for HTML content. 'html.parser' to avoid warning.
            
            
                soup.select('CSS Selector')
                # Pass to an 'Element' object. Returns a list of matched elements.
            
            
                elem[0].text
                # Displays the text of a desired indexed position in an 'Element' object.
            
            
                webdriver.Chrome()
                # Opens up a Chrome browser window. Pass to a 'browser' object.
            
            
                browser.get('URL')
                # Opens a specific page in the opened browser.
            
            
                elem.click()
                # Click on a stored 'Element' object acquired from a CSS selector.
            
            
                searchElem.send_keys('Text')
                # Put text in an input field.
            
            
                searchElem.submit()
                # Submits a form.
            
            
                browser.back()
                # Presses the browser's 'back' button.
            
            
                browser.forward()
                # Presses the browser's 'forward' button.
            
            
                browser.refresh()
                # Presses the browser's 'refresh' button.
            
            
                browser.quit()
                # Closes the browser.
            
         
        
            
            import webbrowser
                
            
            webbrowser.open('https://automatetheboringstuff.com')
                
            
            
            playFile = open('filename.txt'. 'wb')
                
            
            import requests
                
            
            res = requests.get('URL')
                
            
            
            res.status_code
                
            
            res.raise_for_status
                
            
            res.text
                
            
            res.iter_content(100000)
                
            
            playFile.write(chunk)
                
            
            playFile.close()
                
            
            import bs4
            import requests
                
                
            
            res = requests.get('URL')
                
            
            soup = bs4.BeautifulSoup(res.text, 'html.parser') 
                
            
            elem = soup.select('CSS Selector')
                
            
            elem[0].text.strip()
                
            
            from selenium import webdriver
                
            
            browser = webdriver.Chrome()
                
            
            browser.get('https://automatetheboringstuff.com')
                
            
            elem = browser.find_element_by_css_selector('CSS Selector')
                
            
            elems = browser.find_elements_by_css_selector('General HTML')
                
            
            elem.click()
                
            
            searchElem = browser.find_element_by_css_selector('input field')
                
            
            searchElem.send_keys('Text')
                
            
            searchElem.submit()
                
                
            
            browser.back()
            browser.forward()
            browser.refresh()
            browser.quit()
        
    
    Word
        
            
                docx.Document('filename.docx')
                # Opens a document. Pass to a 'document' object. Can use '.Document()' to create a new one.
            
            
                document.paragraphs
                # Displays a list of all paragraphs. Can save a specific index to a 'paragraph' object.
            
            
                document.add_paragraph('Text.')
                # Adds a paragraph to the document.
            
            
                document.paragraphs[0].text
                # Reads the text of the first paragraph in the 'document' object.
            
            
                paragraph.style
                # Can assign the paragraph a specific style, e.g. p.style = 'Title'
            
            
                paragraph.runs
                # Displays a list of all runs. Can save a specific index to a 'runs' object.
            
            
                paragraph.add_run('Text.')
                # Adds a new run to the paragraph.
            
            
                paragraph.runs[0].bold | .italic | .underline
                # Returns a boolean value. Can also set these with normal variable assignments.
            
            
                document.save('filename.docx')
                # Commits the document changes to a file.
            
         
        
            
            
            
                
            
            import docx
                
            
            document = docx.Document('filename.docx')
                
            
            document.paragraphs[0].text
                
            
            paragraph = document.paragraphs[1]
                
            
            paragraph.runs
            
            paragraph.runs[0].text
            
            paragraph.runs[1].bold
            
            paragraph.runs[3].text = 'italic and underlined'
                
            
            paragraph.style = 'Title'
                
            
            document.save('new_word_filename.docx')