Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句。模块让你能够有逻辑地组织你的 Python 代码段。把相关的代码分配到一个模块里能让你的代码更好用,更易懂。模块能定义函数,类和变量,模块里也能包含可执行的代码。

1 os

模块os让你能够访问多个操作系统服务,它包含了很多有用的函数和变量。除此之外os及其子模块os.path还包含多个查看、创建和删除目录及文件的函数,以及一些操作路径的函数(例如os.path.split和os.path.join让你在大多数情况下都可以忽略ps.pathsep)。

模块os中一些重要的函数和变量

函数/变量 描述
environ 包含环境变量的映射
system(command) 在子shell中执行操作系统命令
sep 路径中使用分隔符
pathsep 分割不同路径的分隔符
linesep 行分隔符(’\n’、’\r’或’\r\n’)
urandom(n) 返回n个字节的强加密随机数据
  • 添加文件目录
import os

mypath=r'D:\blog'
file_list = ['1.png','2.png','3.jpg']
for file in file_list:
    print(os.path.join(mypath,file))
  • 获取当前路径与切换路径
import os
print(os.getcwd())
os.chdir(r"D:\blog")
print(os.getcwd())
  • 创建多级文件夹
import os
os.makedirs('abc/def')
import os

# 获取绝对路径
print(os.path.abspath('.'))
print(os.path.abspath('../venv'))

# 判断是否绝对路径
print(os.path.isabs('.'))
print(os.path.isabs(os.path.abspath('.')))

# 返回第二个参数到第一个参数的相对路径,如果第二个为空则以当前为主
print(os.path.relpath(r"D:\Program Files (x86)\PyCharm\scripts",r"D:\Program Files (x86)\PyCharm\venv"))
print(os.path.relpath(r"D:\Program Files (x86)\PyCharm"))
import os
filename = r"D:\Program Files (x86)\PyCharm\scripts\fileDemo.py"

# 返回文件路径
print(os.path.dirname(filename))

# 返回文件名称
print(os.path.basename(filename))

# 路径和文件名称分割为元组
print(os.path.split(filename))

# 将每一层都分割开,如果是linux或者os则返回的列表中第一个值为空
print(filename.split(os.path.sep))
import os

# 返回文件的字节数
print(os.path.getsize("fileDemo.py"))

# 列出文件名字符串列表
print(os.listdir("."))

# 列出所有文件大小
totalSize= 0
for filename in os.listdir("."):
    totalSize = totalSize+os.path.getsize(os.path.abspath(filename))
print(os.path.join(os.path.abspath(filename)))
totalSize = totalSize+os.path.getsize(os.path.join(os.getcwd(),filename))
print(os.path.join(os.getcwd(),filename))
print(totalSize)
import os

# 判断文件或者文件夹是否存在
print(os.path.exists(r"D:\Program Files (x86)\PyCharm\scripts\chromedriver.exe"))
print(os.path.exists(r"D:\Program Files (x86)\PyCharm\scripts"))

# 判断参数是目录
print(os.path.isdir(r"D:\Program Files (x86)\PyCharm\scripts"))

# 判断参数是否是文件
print(os.path.isfile(r"D:\Program Files (x86)\PyCharm\scripts"))
fileContent = open("1.sql", encoding='utf8')

# 返回单个大字符串
print(fileContent.read())

# 每行返回为列表
print(fileContent.readlines())
file_list = fileContent.readlines()
lst1 = []
lst2 = []
for i in range(len(file_list)):
    if (i+2)%3==0:
        lst1.append(file_list[i])
    elif (i+3)%3==0:
        lst2.append(file_list[i])
print(lst1)
print(lst2)
fileContent.close()
fileContent2 = open("2.sql",'w', encoding='utf8')
for i in range(len(lst1)):
    fileContent2.write(lst1[i])
    fileContent2.write('\r```sql\r%s```\r\r'%lst2[i])
fileContent2.close()

2 datetime

模块datetime支持特殊的日期和时间对象,并让你能够以各种方式创建和合并这些对象。

  • 获取当前datetime
from datetime import datetime
now = datetime.now()
print(now)
print(type(now))
  • 获取指定时间和日期
from datetime import datetime
dt = datetime(2021,1,19,12,20)
print(dt)
  • datetime转timestamp,如果有小数位,表示毫秒数
from datetime import datetime
dt = datetime(2021,1,19,12,20)
print(dt.timestamp())
  • timestamp转datetime,如果有小数位,表示毫秒数
from datetime import datetime
t = 1611030000
# 本地时间
print(datetime.fromtimestamp(t))
# UTC时间
print(datetime.utcfromtimestamp(t))
  • str转timestamp,转换后无时区信息
from datetime import datetime
cday = datetime.strptime("2015-6-1 19:02:54","%Y-%m-%d %H:%M:%S")
print(cday)
  • datetime转str
from datetime import datetime
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M"))
  • timedelta加减
from datetime import datetime,timedelta
now = datetime.now()
print(now)
print(now + timedelta(hours=24))
print(now - timedelta(days=24))
print(now + timedelta(days=10,hours=12))
  • 本地时间转utc
from datetime import datetime,timedelta,timezone
# 创建时区UTC+8:00
tz_utc_8 = timezone(timedelta(hours=8))
now = datetime.now()
print(now)
# 强制设置为UTC+8:00
print(now.replace(tzinfo=tz_utc_8))
  • 日期转换
from datetime import datetime,timezone,timedelta

# 拿到UTC时间,并强制设置为UTC+0:00
utc_dt = datetime.utcnow().replace(tzinfo=timezone.utc)
print(utc_dt)
# astimezone()将转换时区为北京时间
bj_dt = utc_dt.astimezone(timezone(timedelta(hours=8)))
print(bj_dt)
# astimezone()将转换时区为东京时间
tokyo_dt = utc_dt.astimezone(timezone(timedelta(hours=9)))
print(tokyo_dt)
# astimezone()将bj_dt转换时区为东京时间
tokyo_dt = bj_dt.astimezone(timezone(timedelta(hours=9)))
print(tokyo_dt)

3 csv

csv指的是逗号分隔的值,很多电子表格程序和数据库程序等都使用这种简单的格式来存储表格数据。这种表格主要用于在不同的程序之间交换数据。模块csv让你能够轻松地读写CSV文件,它还以非常透明的方式处理CSV格式的一些棘手部分。

import csv

exampleFile = open("test_faker.csv",encoding="utf8")

# reader读取Reader对象
exampleReader = csv.reader(exampleFile)

# 直接转为list列表
exampleData = list(exampleReader)
print(exampleData)
print(exampleData[1][5])
print(exampleData[2][4])

# 在for循环中,从Reader对象读取数据
for row in exampleReader:
    print("Row #"+str(exampleReader.line_num)+" "+str(row))
outputFile = open("output.csv","w",newline="")

# delimiter指定单元格的字符,lineterminator行之间的字符
outputWriter = csv.writer(outputFile,delimiter="\t",lineterminator="")
outputWriter = csv.writer(outputFile)

# writerow接受一个列表
outputWriter.writerow(['a','b','c','d'])
outputWriter.writerow(['a','b','c','d','e'])
outputWriter.writerow([1,2,3,4])
outputFile.close()

4 json

import json
# json格式转为python字典
jsonData = '{"a":"1","b":0,"c":null}'
json_to_dict = json.loads(jsonData)
print(json_to_dict)
python_dics = {'a': '1', 'b': 0, 'c': None}
dict_to_json = json.dumps(python_dics)
print(dict_to_json)

5 logging

要避免跟踪时出现大量的调试输出,可将这些信息写入日志文件中。这个模块提供了一系列标准工具,可以用于管理一个或多个中央日志,它还支持多种优先级不同的日志消息。

import logging

logging.basicConfig(level=logging.CRITICAL,format=" %(asctime)s - %(levelname)s - %(message)s")
logging.basicConfig(filename="myProgramLog.txt",level=logging.CRITICAL,format=" %(asctime)s - %(levelname)s - %(message)s")
logging.debug("start of program")

def factorial(n):
    logging.debug('start of factorial(%s%%)'%(n))
    total = 1
    for i in range(1,n+1):
        total *= i
        logging.debug("i is "+str(i)+", total is "+str(total))
        logging.debug("end of factorial(%s%%)"%(n))
    return total

print(factorial(5))
logging.debug("end of program")

# 禁用指定级别以下的日志
logging.disable(logging.ERROR)

# 最低级别,用于小细节。通常只有在诊断问题时,你在会关心这些消息
logging.debug("debug")

# 用于记录程序中一般事件的信息,或确认一切正常工作
logging.info("log")

# 用于表示可能的问题,它不会阻止程序的工作,但将来可能会
logging.warning("warning")

# 用于记录错误,它导致程序做某事失败
logging.error("error")

# 最高级别。用于表示致命的粗错误,它导致或将要导致程序完全停止工作
logging.critical("critical")

6 openpyxl

6.1 使用python读取excel

6.1.1 打开Excel表格获取表格名称

from openpyxl import load_workbook
workbook = load_workbook("test1.xlsx")
print(workbook.sheetnames)

6.1.2 通过sheet名称获取表格

from openpyxl import load_workbook
workbook = load_workbook("test1.xlsx")
print(workbook.sheetnames)
# 通过sheet名称获取表格
sheet = workbook["Sheet1"]
print(sheet)

6.1.3 获取表格的尺寸大小

from openpyxl import load_workbook
workbook = load_workbook("test1.xlsx")
print(workbook.sheetnames)
# 通过sheet名称获取表格
sheet = workbook["Sheet1"]
print(sheet)
# 获取表格的尺寸大小
print(sheet.dimensions)

6.1.4 获取表格内某个格子的数据

1 sheet[“A1”]方式

from openpyxl import load_workbook
workbook = load_workbook("test1.xlsx")
print(workbook.sheetnames)
# 通过sheet名称获取表格
sheet = workbook["Sheet1"]
# 获取表格数据
cell1 = sheet["A1"]
print(cell1.value)
cell2 = sheet["B2"]
print(cell2.value)

2 sheet.cell(row=,column=)方式

from openpyxl import load_workbook
workbook = load_workbook("test1.xlsx")
print(workbook.sheetnames)
# 通过sheet名称获取表格
sheet = workbook["Sheet1"]
# 获取表格数据
cell1 = sheet.cell(row=1,column=1)
print(cell1.value)
cell2 = sheet.cell(row=2,column=2)
print(cell2.value)

6.1.5 获取某个格子的行数、列数、坐标

from openpyxl import load_workbook
workbook = load_workbook("test1.xlsx")
print(workbook.sheetnames)
# 通过sheet名称获取表格
sheet = workbook["Sheet1"]
cell1 = sheet["A1"]
# 获取表格数据
print(cell1.value)
# 获取某个格子的行数
print(cell1.row)
# 获取某个格子的列数
print(cell1.column)
# 获取某个格子的坐标
print(cell1.coordinate)
cell2 = sheet["B2"]
print(cell2.value)

6.1.6 获取一系列格子

1 sheet[]方式

from openpyxl import load_workbook
workbook = load_workbook("test1.xlsx")
print(workbook.sheetnames)
# 通过sheet名称获取表格
sheet = workbook["Sheet1"]
# 获取A1:C2区域的值
cell1 = sheet["A1:C2"]
print(cell1)
for i in cell1:
for j in i:
print(j.value)

2 .iter_rows()方式

'''
.iter_rows()方式按行读取
.iter_cols()方式按列读取
'''
from openpyxl import load_workbook
workbook = load_workbook("test1.xlsx")
print(workbook.sheetnames)
# 通过sheet名称获取表格
sheet = workbook["Sheet1"]
# 按行获取数据
for i in sheet.iter_rows(min_row = 2,max_row = 3 ,min_col=1,max_col=2):
for j in i:
print(j.value)
#按列获取数据
for i in sheet.iter_cols(min_row = 2,max_row = 3 ,min_col=1,max_col=2):
for j in i:
print(j.value)

3 sheet.rows()获取所有的行

from openpyxl import load_workbook
workbook = load_workbook("test1.xlsx")
print(workbook.sheetnames)
# 通过sheet名称获取表格
sheet = workbook["Sheet1"]
for i in sheet.rows:
print(i)

7 python-docx的使用

7.1 使用python读取word

7.1.1 python-docx提取文字

from docx import Document

# Document是文档名称
doc = Document(r"test1.docx")

# paragraph是文档中的每段内容
print(doc.paragraphs)
for paragraph in doc.paragraphs:
   print(paragraph.text)

7.1.2 python-docx提取文字块

from docx import Document

doc = Document(r"test1.docx")
print(doc.paragraphs)
paragraph = doc.paragraphs[0]
runs = paragraph.runs
print(runs)
for run in paragraph.runs:
   print(run.text)
paragraph = doc.paragraphs[1]
runs = paragraph.runs
print(runs)
for run in paragraph.runs:
   print(run.text)

7.2 利用python向word文档写入内容

7.2.1 添加段落

from docx import Document

doc = Document()
# 添加标题,0表示标题是Title样式,位于顶部
doc.add_heading('文档顶部',0)
doc.add_heading("一级标题",1)
doc.add_heading("二级标题",2)

# 添加段落的时候,赋值给一个变量,方方便后面进行格式调整
paragraph1 = doc.add_paragraph("这是一个段落")
paragraph2 = doc.add_paragraph("这是第二个段落")
doc.save(r"test2.docx")

7.2.2 添加文字块

from docx import Document

doc = Document()
# 这里相当于输入了一个空格,后面等待着文字输入
paragraph = doc.add_paragraph()
paragraph.add_run("我被加粗了文字块").bold = True
paragraph.add_run(",我是普通文字块,")
paragraph.add_run("我是斜体文字块").italic = True
doc.save(r"test3.docx")

7.2.3 添加分页

from docx import Document

doc = Document()
doc.add_page_break()
doc.save(r"test4.docx")

7.2.4 添加图片

from docx import Document

# Cm模块,用于设定如偏尺寸大小
from docx.shared import Cm
doc = Document()
doc.add_picture(r"word1.png",width=Cm(15),height=Cm(4))
doc.save(r"test5.docx")

7.2.5 添加表格

from docx import Document

doc = Document()
list1 = [  ["a1","b1","c1"],
  ["a2", "b2", "c2"],
  ["a3", "b3", "c3"],
  ["a4", "b4", "c4"],
  ["a5", "b5", "c5"],
]
list2 = [  ["a11","b11","c11"],
  ["a22", "b22", "c22"],
  ["a33", "b33", "c33"],
]

table1 = doc.add_table(rows=5,cols=3)
for row in range(5):
   cells = table1.rows[row].cells
   for col in range(3):
       cells[col].text = str(list1[row][col])
doc.add_paragraph("--------------------------")
table2 = doc.add_table(rows=3,cols=3)
for row in range(3):
   cells = table2.rows[row].cells
   for col in range(3):
       cells[col].text = str(list2[row][col])
doc.save(r"test6.docx")

7.3 利用python调整Word

7.3.1 修改字体样式

from docx import Document
from docx.shared import Pt,RGBColor
from docx.oxml.ns import qn

doc = Document(r"test7.docx")
for paragraph in doc.paragraphs:
   for run in paragraph.runs:
       run.font.bold = True
       run.font.italic = True
       run.font.underline = True
       run.font.strike = True
       run.font.shadow = True
       run.font.size = Pt(18)
       run.font.color.rgb = RGBColor(0,0,0)
       run.font.name = "宋体"
       # 设置像宋体这样的中文字体,必须添加下面2行代码
       r = run._element.rPr.rFonts
       r.set(qn("w:eastAisa"),"宋体")
doc.save(r"test7.docx")

7.3.2 修改段落样式

  • 对齐方式
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

doc = Document(r"test7.docx")
print(doc.paragraphs[0].text)
# 这里设置的是居中对齐
doc.paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
doc.save(r"test7.docx")
  • 行间距调整
from docx import Document

doc = Document(r"test7.docx")
for paragraph in doc.paragraphs:
   paragraph.paragraph_format.line_spacing = 5.0
doc.save(r"test7.docx")
  • 段前段后间距
from docx import Document
from docx.shared import Pt

doc = Document(r"test7.docx")
for paragraph in doc.paragraphs:
   paragraph.paragraph_format.space_before = Pt(12)
   #paragraph.paragraph_format.space_after = Pt(12)
doc.save(r"test7.docx")

8 shutil模块的使用

#encoding:utf-8
import shutil,os,send2trash

os.chdir("d:")
# 复制文件到指定路径
shutil.copy(r"D:\案例\PostgreSQL特性矩阵解析系列23之SQL-standard information schema.mht",r"D:\\")

# 复制文件到指定路径并重命名
shutil.copy(r"D:\案例\PostgreSQL特性矩阵解析系列23之SQL-standard information schema.mht",r"D:\1.mht")

# 复制目录到新的目录,新的目录会创建
shutil.copytree(r"D:\案例",r"D:\Program Files (x86)\PyCharm\scripts\a")

# 移动文件并改名
shutil.move("1.sql","q/3.sql")

# 移动文件到指定路径
shutil.move("2.sql","q")

# 如果移动的目标目录不存在,则目标目录会变成文件
shutil.move("q/2.sql",'a')

# 如果目标目录上级不存在,将会报错
shutil.move("q/q","a/a")

# 删除文件
os.unlink('a/1.py')
os.remove('a/1.py')

# 删除空文件夹
os.rmdir("a")

# 删除文件夹以及所有子文件
shutil.rmtree("a")
file = open("1.txt","a")
file.write("hello send2trash!")
file.close()
send2trash.send2trash("1.txt")
for folderName,subfolders,filenames in os.walk("D:\welink"):
print('The current folder is:'+folderName)
for subfolder in subfolders:
print(folderName+"\\"+subfolder)
for filename in filenames:
print(folderName+"\\"+filename)

9 zipfile模块的使用

import zipfile,os

os.chdir("D:\Program Files (x86)\PyCharm\scripts")
exampleZip = zipfile.ZipFile("scripts.zip")

# 返回压缩文件包含的所有文件和文件夹的字符串列表
print(exampleZip.namelist())
spamInfo = exampleZip.getinfo("rnd-secsolar.py")

# 原始文件大小
print(spamInfo.file_size)

# 压缩后大小
print(spamInfo.compress_size)

# 解压所有文件
exampleZip.extractall("1")

# 解压单个文件
exampleZip.extract('rnd-secsolar.py','2')
exampleZip.close()

# 将文件压缩到新的文件
newZip = zipfile.ZipFile("new.zip","w")
newZip.write('fileDemo.py',compress_type=zipfile.ZIP_DEFLATED)
newZip.close()
posted on 2022-12-08 22:03  jl1771  阅读(50)  评论(0编辑  收藏  举报