본문 바로가기

개발이야기

파이썬 코드 샘플

반응형

1.  PDF to Audio 

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. Eamil (첨부파일 포함) 발송 

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
body = '''
Hello, Admin
I am attaching The Sales Files With This Email.
This Year We Got a Wooping 200% Profit One Our Sales.

Regards,
Team Sales
xyz.com
'''
#Sender Email addresses and password
senders_email = 'deltadelta371@gmail.com'
sender_password = 'delta@371'
reveiver_email = 'parasharabhay13@gmail.com'

#MIME Setup
message = MIMEMultipart()
message['From'] = senders_email
message['To'] = reveiver_email
message['Subject'] = 'Sales Report 2021-- Team Sales'
message.attach(MIMEText(body, 'plain'))

## File
attach_file_name = 'car-sales.csv'
attach_file = open(attach_file_name, 'rb') 
payload = MIMEBase('application', 'octate-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload) 
payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
message.attach(payload)

#SMTP Connection For Sending Email
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(senders_email, sender_password) #login with mail_id and password
text = message.as_string()
session.sendmail(senders_email, reveiver_email, text)
session.quit()
print('Mail Sent')

 

 

3.  유튜브 다운로드 

from pytube import  YouTube  
import pytube  
try:
    video_url = 'https://www.youtube.com/watch?v=lTTajzrSkCw'   
    youtube = pytube.YouTube(video_url)  
    video = youtube.streams.first()  
    video.download('C:/Users/abhay/Desktop/')  
    print("Download Successfull !!")
except:
    print("Something Went Wrong !!")

 

출처: 
https://medium.com/pythoneers/10-really-helpful-automation-scripts-you-need-to-try-using-python-7dda9408fa74

반응형