模块
os、sys模块
import os
import sys
print(os.getcwd())
print(os.listdir())
#os.remove("stock")
print(os.path.isfile("test.py"))
print(os.path.isdir("day4"))
print(os.path.exists("/os、sys模块.py"))
print(os.path.dirname("/os、sys模块.py"))
print(os.path.abspath("os、sys模块.py"))
print(os.path.basename("os、sys模块.py"))
#print(os.system("ipconfig"))
#os.rename("","")
#os.makedirs("stock")
print(os.stat("os、sys模块.py").st_size)
print(os.path.getsize("os、sys模块.py"))
print(sys.path)
print(sys.argv)
time模块
import time
s_time = time.time()
time.sleep(2)
print(time.time() - s_time)
print(time.localtime())
print(time.gmtime())
print(time.mktime(time.localtime()))#将时间对象struct_time转化为时间戳
time_str= time.strftime("%Y-%m/%d %H:%M:%S %a")
print(time_str)
print(time.strptime(time_str,"%Y-%m/%d %H:%M:%S %a"))
datetime模块
import datetime
d = datetime.datetime.now()
print(d)
print(d.timetuple())
print(datetime.datetime.fromtimestamp(134334343))
print(d + datetime.timedelta(2,hours = 4))
print(d.replace(year = 2121))
random模块
import random
import string
print(random.randrange(1,10000,2))
print(random.randint(1,10000))
print(random.random())#[0,1]浮点数
print(random.choice("wwfwwf"))#随机一个
print(random.sample("abcdefg",3))#随机几个
#生成随机字符串
print(''.join(random.sample(string.ascii_lowercase + string.digits,6)))
#洗牌
a = [1,2,3,4,5,6,7,8,9,10]
random.shuffle(a)
print(a)
excel模块
from openpyxl import Workbook,load_workbook
import datetime
#实例化 create an excel file in RAM
#wb = Workbook()
#sheet = wb.active
#sheet.title = "wwf's python"
#wb.save("excel_test.xlsx")
#wb = load_workbook("excel_test.xlsx")#打开文件
#sheet = wb.active
#sheet["B2"] = "wwf"
#sheet.append(["wwf","20"])
#sheet["A1"] = datetime.datetime.now().strftime("%Y-%m-%d")
#wb.save("test1.xlsx")
wb = load_workbook("*****.xlsx")
print(wb.sheetnames)
sheet = wb["Sheet1"]
print(sheet["D77"])
print(sheet["D77"].value)
#for cell in sheet["A56:A60"]:
# print(cell[0].value)
#获取指定列的切片数据
# for row in sheet:
# for cell in row:
# print(cell.value,end = ",")
# print()
#指定行的循环
# for row in sheet.iter_rows(min_row=114,max_row=119,max_col=3):
# for cell in row:
# print(cell.value,end = ",")
# print()
#指定列的循环
# for col in sheet.iter_cols(min_col = 2,max_col = 3,max_row = 5):
# for cell in col:
# print(cell.value)
#按列循环所有值
# for col in sheet.columns:
# for cell in col:
# print(cell.value, end=",")
# print()
#删除工作表??????
# s = wb['Sheet3']
# wb.remove(s)
# del wb['Sheet2']
# wb.save("******.xlsx")
from openpyxl.styles import Font,colors,Alignment
myfont = Font(name="宋体",size=20,italic=True,color=colors.BLUE)
sheet["A1"].font = myfont
sheet["A1"].alignment = Alignment(horizontal="center",vertical="center")
sheet.row_dimensions[1].height = 40
sheet.column_dimensions["A"].width = 40
wb.save("****.xlsx")
发简单邮件
import smtplib
from email.mime.text import MIMEText # 邮件正文
from email.header import Header # 邮件头
#登陆邮件服务器
smtp_obj = smtplib.SMTP_SSL("smtp.qq.com",465)#发件人邮箱SMTP服务器
smtp_obj.login("******@qq.com","******")#发件人邮箱账号密码
#smtp_obj.set_debuglevel(1)#显示调试信息
mail_body = '''
<h3>你好</h5>
<p>
<a href="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2067944566,3644405599&fm=26&gp=0.jpg" target="_blank">分享图片</a>
</p>
'''
#设置邮件头信息
msg = MIMEText(mail_body,"html","utf-8")#正文
msg["From"] = Header("来自wwf的邮件","utf-8")#发送者
msg["To"] = Header("你好,有缘人")#收件者
msg["Subject"] = Header("python发邮件测试")#主题
#send
smtp_obj.sendmail("******@qq.com",["******@qq.com"],msg.as_string())
给员工发工资条邮件
from openpyxl import load_workbook
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#1.加载excel员工工资文件
wb = load_workbook("大唐集团2021年1月份工资.xlsx",data_only = True) # 取列表值,而非公式
sheet = wb.active
#2.登录
smtp_obj = smtplib.SMTP_SSL("smtp.qq.com",465)
smtp_obj.login("********@qq.com","********")
#3.循环遍历excel
count = 0
head = "<thead>" #工资条的表头
for row in sheet:
count += 1
if count == 1:
for cell in row:
head += f"<th>{cell.value}</th>"
head += "</thead>"
continue
else:
salary = "<tr>"
for cell in row:
salary += f"<td>{cell.value}</td>"
salary += "</tr>"
name = row[2].value
staff_email = row[1].value
#4.写邮件内容
msg_content = f'''
<h3>{name}员工,你好</h3>
<p>请查收您1月份的工资条:</p>
<table border = "1px solid black">
{head}
{salary}
</table>
'''
msg = MIMEText(msg_content,"html","utf-8")
msg["From"] = Header("大唐集团人事部","utf-8")
msg["To"] = Header("大唐集团的员工","utf-8")
msg["Subject"] = Header("大唐集团2021年1月份工资","utf-8")
#5.发邮件
smtp_obj.sendmail("********@qq.com",["********@qq.com"],msg.as_string())
print(f"成功发送工资条到{name}-{staff_email}......")