Convert a single digit number into a double digits string in Python
>>> print '{0}'.format('5'.zfill(2)) 05
>>> print '{0}'.format('5'.zfill(2)) 05
def some_magic(): import a for i in dir(a): item = getattr(a,i) if callable(item): item() if __name__ == '__main__': some_magic()
#!/usr/local/bin/python3 import inspect import sys def f1(): print("f1") def f2(): print("f2") def some_magic(mod): all_functions = inspect.getmembers(mod, inspect.isfunction) for key, value in all_functions: if str(inspect.signature(value)) == "()": value() if __name__ == '__main__': some_magic(sys.modules[__name__])
members = inspect.getmembers(self) for key, value in members: if key.startswith("run_"): if callable(value): if value(): break
from openpyxl import load_workbook wb2 = load_workbook('test.xlsx') ws4 = wb2["New Title"]
You should check if your sheet in sheet names wb.sheetnames
print(wb2.sheetnames) ['Sheet2', 'New Title', 'Sheet1']
References
https://stackoverflow.com/questions/36814050/openpyxl-get-sheet-by-name
im = Image.open("audacious.png") rgb_im = im.convert('RGB') rgb_im.save('audacious.jpg')
from PIL import Image img = Image.open('image.png').convert('LA') img.save('greyscale.png')
LA
mode has luminosity (brightness) and alpha. If you use LA
mode, then greyscale.png
will be an RGBA image with the alpha channel of image.png
preserved. If you use L
mode, then greyscale.png
will be an RGB image (with no alpha).
References
https://stackoverflow.com/questions/48248405/cannot-write-mode-rgba-as-jpeg
https://stackoverflow.com/questions/12201577/how-can-i-convert-an-rgb-image-into-grayscale-in-python
from openpyxl import Workbook, worksheet, load_workbook wb: Workbook = load_workbook(filename="data.xlsx") ws: worksheet = wb.active end_column = ws.max_column start_column = 1 column_index = start_column while column_index <= end_column: # here we just want first row print(ws.cell(1, column_index).value) column_index += 1
from openpyxl import Workbook, worksheet, load_workbook wb: Workbook = load_workbook(filename="data.xlsx") ws: worksheet = wb.active end_row = ws.max_row # start after header start_row = 2 row_index = start_row while row_index <= end_row: print(ws["A" + str(row_index)].value) print(ws["B" + str(row_index)].value) print(ws["C" + str(row_index)].value) print(ws["D" + str(row_index)].value) print(ws["E" + str(row_index)].value) row_index += 1
# Imports PIL module from PIL import Image # open method used to open different extension image file im = Image.open(r"C:\Users\System-Pc\Desktop\ybear.jpg") # This method will show image in any image viewer im.show()
# Imports PIL module from PIL import Image # open method used to open different extension image file im = Image.open(r"C:\Users\System-Pc\Desktop\lion.png") # This method will show image in any image viewer im.show()
References
https://www.geeksforgeeks.org/python-pil-image-open-method/
import pyscreeze button7location = pyscreeze.locateOnScreen('calc7key.png') button7location (1416, 562, 50, 41) button7x, button7y = pyscreeze.center(button7location) button7x, button7y (1441, 582) pyscreeze.click(button7x, button7y) # clicks the center of where the 7 button was found
import pyscreeze x, y = pyscreeze.locateCenterOnScreen('calc7key.png') pyscreeze.click(x, y)
import pyscreeze for pos in pyscreeze.locateAllOnScreen('someButton.png') ... print(pos) ... (1101, 252, 50, 50) (59, 481, 50, 50) (1395, 640, 50, 50) (1838, 676, 50, 50) list(pyscreeze.locateAllOnScreen('someButton.png')) [(1101, 252, 50, 50), (59, 481, 50, 50), (1395, 640, 50, 50), (1838, 676, 50, 50)]
import pyscreeze button7location = pyscreeze.locateOnScreen('calc7key.png', grayscale=True) button7location (1416, 562, 50, 41)
References
https://pypi.org/project/PyScreeze/
import pyscreeze im1 = pyscreeze.screenshot() im2 = pyscreeze.screenshot('my_screenshot.png')
import pyscreeze im = pyscreeze.screenshot(region=(0,0, 300, 400))
pass a four-integer tuple of the left, top, width, and height of the region to capture
References
https://pypi.org/project/PyScreeze/
image_gray: np.ndarray TM_METHOD=cv2.TM_CCOEFF_NORMED def __init__(self): self.take_screenshot() def take_screenshot(self): user32 = ctypes.windll.user32 user32.SetProcessDPIAware() image = ImageGrab.grab() # save on file # image.save('screenshot.jpg', format='JPEG', quality=20) # load image from memory image_rgb = np.array(image) # load from file # image_rgb=cv2.imread('screenshot.jpg') self.image_gray = cv2.cvtColor(image_rgb, cv2.COLOR_BGR2GRAY) @staticmethod def save_screenshot(): user32 = ctypes.windll.user32 user32.SetProcessDPIAware() image = ImageGrab.grab() image.save("screenshot.jpg") def template_available(self, template_path: str, threshold=0.9): # self.take_screenshot() template = cv2.imread(template_path, 0) # w, h = template.shape[::-1] res = cv2.matchTemplate(self.image_gray, template, self.TM_METHOD) loc = np.where(res >= threshold) if len(loc[0]) == 0 & len(loc[1]) == 0: return False else: print("template_available : " + template_path) return True def template_count(self, template_path: str, threshold=0.9): # self.take_screenshot() template = cv2.imread(template_path, 0) # w, h = template.shape[::-1] res = cv2.matchTemplate(self.image_gray, template, self.TM_METHOD) loc = np.where(res >= threshold) return len(loc[0]) def template_location(self, template_path: str, threshold=0.9): self.take_screenshot() template = cv2.imread(template_path, 0) w, h = template.shape[::-1] res = cv2.matchTemplate(self.image_gray, template, self.TM_METHOD) loc = np.where(res >= threshold) if len(loc[0]) == 0 & len(loc[1]) == 0: return None else: click_y = loc[0][0] + h / 2 click_x = loc[1][0] + w / 2 return click_x, click_y def move_mouse_to_template(self, template_path: str): loc = self.template_location(template_path) if loc is not None: click_x = loc[0] click_y = loc[1] pyautogui.moveTo(click_x, click_y) def click_template(self, template_path: str, threshold=0.9): loc = self.template_location(template_path, threshold) if loc is not None: click_x = loc[0] click_y = loc[1] pyautogui.click(click_x, click_y) pyautogui.FAILSAFE = False pyautogui.moveTo(0, 0) print("click_template : " + template_path) return True return False @staticmethod def scroll_mouse(value=10): pyautogui.scroll(value) pyautogui.FAILSAFE = False pyautogui.moveTo(0, 0) @staticmethod def get_template_path(template: str): template_name = f"template{template}.jpg" template_path = os.path.join(os.getcwd(), 'templates', template_name) return template_path