04、创建有声读物
厌倦了手动将您的 PDF 书籍转换为有声读物,那么这是你的自动化脚本python网页自动化脚本,它使用 GTTS 模块将你的 PDF 文本转换为音频。
# Create Audiobooks
# pip install gTTS
# pip install PyPDF2
from PyPDF2 import PdfFileReader as reader
from gtts import gTTS
def create_audio(pdf_file):
read_Pdf = reader(open(pdf_file, 'rb'))
for page in range(read_Pdf.numPages):
text = read_Pdf.getPage(page).extractText()
tts = gTTS(text, lang='en')
tts.save('page' + str(page) + '.mp3')
create_audio('book.pdf')
05、PDF 编辑器
使用以下自动化脚本使用 Python 编辑 PDF 文件。该脚本使用 PyPDF4 模块,它是 PyPDF2 的升级版本,下面我编写了 Parse Text、Remove pages 等常用功能。当你有大量 PDF 文件要编辑或需要以编程方式在 Python 项目中使用脚本时,这是一个方便的脚本。
# PDF Editor
# pip install PyPDf4
import PyPDF4
# Parse the Text from PDF
def parse_text(pdf_file):
reader = PyPDF4.PdfFileReader(pdf_file)
for page in reader.pages:
print(page.extractText())
# Remove Page from PDF
def remove_page(pdf_file, page_numbers):
filer = PyPDF4.PdfReader('source.pdf', 'rb')
out = PyPDF4.PdfWriter()
for index in page_numbers:
page = filer.pages[index]
out.add_page(page)
with open('rm.pdf', 'wb') as f:
out.write(f)
# Add Blank Page to PDF
def add_page(pdf_file, page_number):
reader = PyPDF4.PdfFileReader(pdf_file)
writer = PyPDF4.PdfWriter()
writer.addPage()
with open('add.pdf', 'wb') as f:
writer.write(f)
# Rotate Pages
def rotate_page(pdf_file):
reader = PyPDF4.PdfFileReader(pdf_file)
writer = PyPDF4.PdfWriter()
for page in reader.pages:
page.rotateClockwise(90)
writer.addPage(page)
with open('rotate.pdf', 'wb') as f:
writer.write(f)
# Merge PDFs
def merge_pdfs(pdf_file1, pdf_file2):
pdf1 = PyPDF4.PdfFileReader(pdf_file1)
pdf2 = PyPDF4.PdfFileReader(pdf_file2)
writer = PyPDF4.PdfWriter()
for page in pdf1.pages:
writer.addPage(page)
for page in pdf2.pages:
writer.addPage(page)
with open('merge.pdf', 'wb') as f:
writer.write(f)