美文网首页
python 实用的小功能整理

python 实用的小功能整理

作者: 终极蚂蚁 | 来源:发表于2019-08-15 21:54 被阅读0次

python 常用代码

剪切板

  • 保存剪切板图片到本地文件
    参考链接

    # 保存剪切板图片到本地文件
    
    # 安装所需包
    # pip install pillow
    
    from PIL import ImageGrab
    from PIL import Image
    
    img = ImageGrab.grabclipboard()
    if isinstance(img, Image.Image):
        img.save('test.png', 'png')
    
  • 打印剪切板图片为base64编码
    参考链接

    from PIL import ImageGrab
    from PIL import Image
    import io
    
    img = ImageGrab.grabclipboard()
    img_bytes = io.BytesIO()
    if isinstance(img, Image.Image):
        img.save(img_bytes, 'png')
        print(img_bytes.getvalue())
    
  • 隐藏python运行时的console
    参考链接

    可以将这段代码写入python文件,然后在需要运行的python中引入即可

    import ctypes    
    whnd = ctypes.windll.kernel32.GetConsoleWindow()    
    if whnd != 0:    
        ctypes.windll.user32.ShowWindow(whnd, 0)    
        ctypes.windll.kernel32.CloseHandle(whnd)
    
  • 获取并打印当前时间
    参考链接

    import datetime
    
    now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print(now_time)
    

相关文章

网友评论

      本文标题:python 实用的小功能整理

      本文链接:https://www.haomeiwen.com/subject/fintsctx.html