4个的Python自动化脚本分享

作者:迟业 时间:2021-05-28 19:22:29 

目录
  • 1、将 PDF 转换为音频文件

  • 2、从列表中播放随机音乐

  • 3、不再有书签了

  • 4、清理下载文件夹

前言:

大家平时有没有注意到你每天可能会执行许多的重复的任务,例如阅读 pdf、播放音乐、打开书签、清理文件夹等等。
我将分享4个实用的python的自动化脚本,无需手动一次又一次地完成这些任务,非常方便。

1、将 PDF 转换为音频文件

脚本可以将 pdf 转换为音频文件,原理也很简单,首先用 PyPDF 提取 pdf 中的文本,然后用 Pyttsx3 将文本转语音。关于文本转语音,你还可以看这篇文章。

FastAPI:快速开发一个文本转语言的接口。

代码如下:


import pyttsx3,PyPDF2
pdfreader = PyPDF2.PdfFileReader(open('story.pdf','rb'))
speaker = pyttsx3.init()
for page_num in range(pdfreader.numPages):  
   text = pdfreader.getPage(page_num).extractText()  ## extracting text from the PDF
   cleaned_text = text.strip().replace('\n',' ')  ## Removes unnecessary spaces and break lines
   print(cleaned_text)                ## Print the text from PDF
   #speaker.say(cleaned_text)        ## Let The Speaker Speak The Text
   speaker.save_to_file(cleaned_text,'story.mp3')  ## Saving Text In a audio file 'story.mp3'
   speaker.runAndWait()
speaker.stop()

2、从列表中播放随机音乐

这个脚本会从歌曲文件夹中随机选择一首歌进行播放,需要注意的是 os.startfile 仅支持 Windows 系统。


import random, os
music_dir = 'G:\new english songs'
songs = os.listdir(music_dir)
song = random.randint(0,len(songs))
print(songs[song])  ## Prints The Song Name
os.startfile(os.path.join(music_dir, songs[0]))

3、不再有书签了

每天睡觉前,我都会在网上搜索一些好内容,第二天可以阅读。大多数时候,我把遇到的网站或文章添加为书签,但我的书签每天都在增加,以至于现在我的浏览器周围有100多个书签。因此,在python的帮助下,我想出了另一种方法来解决这个问题。现在,我把这些网站的链接复制粘贴到文本文件中,每天早上我都会运行脚本,在我的浏览器中再次打开所有这些网站。


import webbrowser
with open('./websites.txt') as reader:
   for link in reader:
       webbrowser.open(link.strip())

代码用到了 webbrowser,是 Python 中的一个库,可以自动在默认浏览器中打开 URL。

4、清理下载文件夹

世界上最混乱的事情之一是开发人员的下载文件夹,里面存放了很多杂乱无章的文件,此脚本将根据大小限制来清理您的下载文件夹,

有限清理比较旧的文件:


import os
import threading
import time

def get_file_list(file_path):
#文件按最后修改时间排序
   dir_list = os.listdir(file_path)
   if not dir_list:
       return
   else:
       dir_list = sorted(dir_list, key=lambda x: os.path.getmtime(os.path.join(file_path, x)))
   return dir_list

def get_size(file_path):
    " " "[summary]
   Args:
       file_path ([type]): [目录]

Returns:
       [type]: 返回目录大小,MB
   " " "
   totalsize=0
   for filename in os.listdir(file_path):
       totalsize=totalsize+os.path.getsize(os.path.join(file_path, filename))
   #print(totalsize / 1024 / 1024)
   return totalsize / 1024 / 1024

def detect_file_size(file_path, size_Max, size_Del):
      " " "[summary]
   Args:
       file_path ([type]): [文件目录]
       size_Max ([type]): [文件夹最大大小]
       size_Del ([type]): [超过size_Max时要删除的大小]
   " " "
   print(get_size(file_path))
   if get_size(file_path) > size_Max:
       fileList = get_file_list(file_path)
       for i in range(len(fileList)):
           if get_size(file_path) > (size_Max - size_Del):
               print ("del :%d %s" % (i + 1, fileList[i]))
               #os.remove(file_path + fileList[i])

def detectFileSize():
#检测线程,每个5秒检测一次
   while True:
       print('======detect============')
       detect_file_size("/Users/aaron/Downloads/", 100, 30)
       time.sleep(5)

if __name__ == "__main__":
   #创建检测线程
   detect_thread = threading.Thread(target = detectFileSize)
   detect_thread.start()

来源:https://juejin.cn/post/7041826357663252516

标签:Python,自动化,脚本
0
投稿

猜你喜欢

  • python为QT程序添加图标的方法详解

    2021-05-16 15:56:08
  • 在Pycharm中使用GitHub的方法步骤

    2021-04-27 04:46:48
  • Python中矩阵库Numpy基本操作详解

    2021-07-09 18:13:05
  • php+js实现的无刷新下载文件功能示例

    2023-06-12 06:42:47
  • Python logging模块进行封装实现原理解析

    2021-02-15 07:51:44
  • 基于python图书馆管理系统设计实例详解

    2023-06-28 23:44:13
  • jsp下页面跳转的几种方法小结

    2023-07-22 00:38:07
  • python调用函数、类和文件操作简单实例总结

    2022-04-09 22:28:53
  • 推荐:怎么用javascript进行拖拽

    2007-09-21 20:14:00
  • 不用为美化select烦恼模仿combox(select)控件

    2007-08-04 21:08:00
  • 玩体验,先忘掉自己

    2010-01-30 13:36:00
  • python字符串和常用数据结构知识总结

    2023-09-29 21:00:55
  • 使用SQL语句,查第10-20条记录

    2008-02-19 18:34:00
  • Django+Uwsgi+Nginx如何实现生产环境部署

    2023-02-08 00:48:48
  • Python上下文管理器详细使用教程

    2021-06-24 05:47:35
  • Golang利用自定义模板发送邮件的方法详解

    2023-06-29 07:07:16
  • python使用正则表达式提取网页URL的方法

    2023-10-09 16:27:18
  • PHP实现从上往下打印二叉树的方法

    2023-06-25 17:40:08
  • python搭建虚拟环境的步骤详解

    2021-10-05 14:42:31
  • Python实现LRU算法的2种方法

    2021-10-19 11:30:32
  • asp之家 网络编程 m.aspxhome.com