python+word+excel+ppt自动化办公教程_Python自动化办公之Word,全网最全看这一篇就够了...

第一个参数为图片路径,需要正确无误

第二个参数为图片大小,单位英寸

document.add_picture('countrygarden.png', width=Inches(1.25))

新建表格操作table = document.add_table(rows=1, cols=3)填充标题行操作hdr_cells = table.rows[0].cells

hdr_cells[0].text = 'Qty'

hdr_cells[1].text = 'Id'

hdr_cells[2].text = 'Desc'为每组内容添加数据行并填充for qty, id, desc in records:

row_cells = table.add_row().cells

row_cells[0].text = str(qty)

row_cells[1].text = id

row_cells[2].text = desc设置标题样式操作table.style = 'LightShading-Accent1'

添加分页符操作document.add_page_break()保存当前文档操作document.save('4.1 Python-docx官方例程.docx')Python-docx 表格样式设置

表格样式设置代码:

from docx import *

document = Document()

table = document.add_table(3, 3, )

heading_cells = table.rows[0].cells

heading_cells[0].text = '第一列内容'

heading_cells[1].text = '第二列内容'

heading_cells[2].text = '第三列内容'

document.save("demo.docx")

遍历所有样式:

from docx.enum.style import WD_STYLE_TYPE

from docx import Document

document = Document()

styles = document.styles

# 生成所有表样式

for s in styles:

if s.type == WD_STYLE_TYPE.TABLE:

document.add_paragraph("表格样式 : " + s.name)

table = document.add_table(3, 3, style=s)

heading_cells = table.rows[0].cells

heading_cells[0].text = '第一列内容'

heading_cells[1].text = '第二列内容'

heading_cells[2].text = '第三列内容'

document.add_paragraph("\n")

document.save('4.3 所有表格样式.docx')

效果如下(大家按照喜欢的样式添加即可):

docx&matplotlib 自动生成数据分析报告 最终效果

数据获取

我们这里使用xlrd作为数据获取所使用的库,简单回顾一下:

import xlrd

xlsx = xlrd.open_workbook('./3_1 xlrd 读取 操作练习.xlsx')

# 通过sheet名查找:xlsx.sheet_by_name("sheet1")

# 通过索引查找:xlsx.sheet_by_index(3)

table = xlsx.sheet_by_index(0)

# 获取单个表格值 (2,1)表示获取第3行第2列单元格的值

value = table.cell_value(2, 1)

print("第3行2列值为",value)

# 获取表格行数

nrows = table.nrows

print("表格一共有",nrows,"行")

# 获取第4列所有值(列表生成式)

name_list = [str(table.cell_value(i, 3)) for i in range(1, nrows)]

print("第4列所有的值:",name_list)

表格内容:

编写数据获取代码:

我们这里只获取用户姓名和,分数,并将它们保存到列表中,看代码。

# 获取学习成绩信息

def GetExcelInfo():

print("开始获取表格内容信息")

# 打开指定文档

xlsx = xlrd.open_workbook('学生成绩表格.xlsx')

# 获取sheet

sheet = xlsx.sheet_by_index(0)

# 获取表格行数

nrows = sheet.nrows

print("一共 ",nrows," 行数据")

# 获取第2列,和第4列 所有值(列表生成式),从第2行开始获取

nameList = [str(sheet.cell_value(i, 1)) for i in range(1, nrows)]

scoreList = [int(sheet.cell_value(i, 3)) for i in range(1, nrows)]

# 返回名字列表和分数列表

return nameList,scoreList

获取结果:

柱状图生成

我们先将获取的姓名和成绩使用 字典 数据结构关联起来,再对其排序:

# 将名字和分数列表合并成字典(将学生姓名和分数关联起来)

scoreDictionary = dict(zip(nameList, scoreList))

print("dictionary:",scoreDictionary)

# 对字典进行值排序,高分在前,reverse=True 代表降序排列

scoreOrder = sorted(scoreDictionary.items(), key=lambda x: x[1], reverse=True)

print("scoreOrder",scoreOrder)

效果如下:

# 合成的字典

dictionary: {'Dillon Miller': 41, 'Laura Robinson': 48, 'Gabrilla Rogers': 28, 'Carlos Chen': 54, 'Leonard Humphrey': 44, 'John Hall': 63, 'Miranda Nelson': 74, 'Jessica Morgan': 34, 'April Lawrence': 67, 'Cindy Brown': 52, 'Cassandra Fernan': 29, 'April Crawford': 91, 'Jennifer Arias': 61, 'Philip Walsh': 58, 'Christina Hill P': 14, 'Justin Dunlap': 56, 'Brian Lynch': 84, 'Michael Brown': 68}

# 排序后,再次转换成列表

scoreOrder [('April Crawford', 91), ('Brian Lynch', 84), ('Miranda Nelson', 74), ('Michael Brown', 68), ('April Lawrence', 67), ('John Hall', 63), ('Jennifer Arias', 61), ('Philip Walsh', 58), ('Justin Dunlap', 56), ('Carlos Chen', 54), ('Cindy Brown', 52), ('Laura Robinson', 48), ('Leonard Humphrey', 44), ('Dillon Miller', 41), ('Jessica Morgan', 34), ('Cassandra Fernan', 29), ('Gabrilla Rogers', 28), ('Christina Hill P', 14)]

使用 matplotlib 生成柱状图:

# 生成学生成绩柱状图(使用matplotlib)

# 会生成一张名为"studentScore.jpg"的图片

def GenerateScorePic(scoreList):

# 解析成绩列表,生成横纵坐标列表

xNameList = [str(studentInfo[0]) for studentInfo in scoreList]

yScoreList = [int(studentInfo[1]) for studentInfo in scoreList]

print("xNameList",xNameList)

print("yScoreList",yScoreList)

# 设置字体格式

matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑体显示中文

# 设置绘图尺寸

plt.figure(figsize=(10,5))

# 绘制图像

plt.bar(x=xNameList, height=yScoreList, label='学生成绩', color='steelblue', alpha=0.8)

# 在柱状图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式

for x1, yy in scoreList:

plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=16, rotation=0)

# 设置标题

plt.title("学生成绩柱状图")

# 为两条坐标轴设置名称

plt.xlabel("学生姓名")

plt.ylabel("学生成绩")

# 显示图例

plt.legend()

# 坐标轴旋转

plt.xticks(rotation=90)

# 设置底部比例,防止横坐标显示不全

plt.gcf().subplots_adjust(bottom=0.25)

# 保存为图片

plt.savefig("studentScore.jpg")

# 直接显示

plt.show()

效果如下:

生成最终报告

代码如下:

# 开始生成报告

def GenerateScoreReport(scoreOrder,picPath):

# 新建一个文档

document = Document()

# 设置标题

document.add_heading('数据分析报告', 0)

# 添加第一名的信息

p1 = document.add_paragraph("分数排在第一的学生姓名为: ")

p1.add_run(scoreOrder[0][0]).bold = True

p1.add_run(" 分数为: ")

p1.add_run(str(scoreOrder[0][1])).italic = True

# 添加总体情况信息

p2 = document.add_paragraph("共有: ")

p2.add_run(str(len(scoreOrder))).bold = True

p2.add_run(" 名学生参加了考试,学生考试的总体情况: ")

# 添加考试情况表格

table = document.add_table(rows=1, cols=2)

table.style = 'Medium Grid 1 Accent 1'

hdr_cells = table.rows[0].cells

hdr_cells[0].text = '学生姓名'

hdr_cells[1].text = '学生分数'

for studentName,studentScore in scoreOrder:

row_cells = table.add_row().cells

row_cells[0].text = studentName

row_cells[1].text = str(studentScore)

# 添加学生成绩柱状图

document.add_picture(picPath, width=Inches(6))

document.save('学生成绩报告.docx')

完整代码import xlrd

import matplotlib

import matplotlib.pyplot as plt

from docx import Document

from docx.shared import Inches

# 获取学习成绩信息

def GetExcelInfo():

print("开始获取表格内容信息")

# 打开指定文档

xlsx = xlrd.open_workbook('学生成绩表格.xlsx')

# 获取sheet

sheet = xlsx.sheet_by_index(0)

# 获取表格行数

nrows = sheet.nrows

print("一共 ",nrows," 行数据")

# 获取第2列,和第4列 所有值(列表生成式),从第2行开始获取

nameList = [str(sheet.cell_value(i, 1)) for i in range(1, nrows)]

scoreList = [int(sheet.cell_value(i, 3)) for i in range(1, nrows)]

# 返回名字列表和分数列表

return nameList,scoreList

# 生成学生成绩柱状图(使用matplotlib)

# 会生成一张名为"studentScore.jpg"的图片

def GenerateScorePic(scoreList):

# 解析成绩列表,生成横纵坐标列表

xNameList = [str(studentInfo[0]) for studentInfo in scoreList]

yScoreList = [int(studentInfo[1]) for studentInfo in scoreList]

print("xNameList",xNameList)

print("yScoreList",yScoreList)

# 设置字体格式

matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 用黑体显示中文

# 设置绘图尺寸

plt.figure(figsize=(10,5))

# 绘制图像

plt.bar(x=xNameList, height=yScoreList, label='学生成绩', color='steelblue', alpha=0.8)

# 在柱状图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式

for x1, yy in scoreList:

plt.text(x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=16, rotation=0)

# 设置标题

plt.title("学生成绩柱状图")

# 为两条坐标轴设置名称

plt.xlabel("学生姓名")

plt.ylabel("学生成绩")

# 显示图例

plt.legend()

# 坐标轴旋转

plt.xticks(rotation=90)

# 设置底部比例,防止横坐标显示不全

plt.gcf().subplots_adjust(bottom=0.25)

# 保存为图片

plt.savefig("studentScore.jpg")

# 直接显示

plt.show()

# 开始生成报告

def GenerateScoreReport(scoreOrder,picPath):

# 新建一个文档

document = Document()

# 设置标题

document.add_heading('数据分析报告', 0)

# 添加第一名的信息

p1 = document.add_paragraph("分数排在第一的学生姓名为: ")

p1.add_run(scoreOrder[0][0]).bold = True

p1.add_run(" 分数为: ")

p1.add_run(str(scoreOrder[0][1])).italic = True

# 添加总体情况信息

p2 = document.add_paragraph("共有: ")

p2.add_run(str(len(scoreOrder))).bold = True

p2.add_run(" 名学生参加了考试,学生考试的总体情况: ")

# 添加考试情况表格

table = document.add_table(rows=1, cols=2)

table.style = 'Medium Grid 1 Accent 1'

hdr_cells = table.rows[0].cells

hdr_cells[0].text = '学生姓名'

hdr_cells[1].text = '学生分数'

for studentName,studentScore in scoreOrder:

row_cells = table.add_row().cells

row_cells[0].text = studentName

row_cells[1].text = str(studentScore)

# 添加学生成绩柱状图

document.add_picture(picPath, width=Inches(6))

document.save('学生成绩报告.docx')

if __name__ == "__main__":

# 调用信息获取方法,获取用户信息

nameList,scoreList = GetExcelInfo()

# print("nameList:",nameList)

# print("ScoreList:",scoreList)

# 将名字和分数列表合并成字典(将学生姓名和分数关联起来)

scoreDictionary = dict(zip(nameList, scoreList))

# print("dictionary:",scoreDictionary)

# 对字典进行值排序,高分在前,reverse=True 代表降序排列

scoreOrder = sorted(scoreDictionary.items(), key=lambda x: x[1], reverse=True)

# print("scoreOrder",scoreOrder)

# 将进行排序后的学生成绩列表生成柱状图

GenerateScorePic(scoreOrder)

# 开始生成报告

picPath = "studentScore.jpg"

GenerateScoreReport(scoreOrder,picPath)

print("任务完成,报表生成完毕!")Python-docx 修改旧 word 文档 回顾:打开旧文档,并另存为新文档

我们这里就拿上一节生成的学生成绩报告作为示例:

from docx import Document

if __name__ == "__main__":

document = Document('6 学生成绩报告.docx')

# 在这里进行操作,此处忽略

document.save('修改后的报告.docx')读取word文档的内容

示例代码:

from docx import Document

if __name__ == "__main__":

document = Document('6 学生成绩报告.docx')

# 读取 word 中所有内容

for p in document.paragraphs:

print("paragraphs:",p.text)

# 读取 word 中所有一级标题

for p in document.paragraphs:

if p.style.name == 'Heading 1':

print("Heading 1:",p.text)

# 读取 word 中所有二级标题

for p in document.paragraphs:

if p.style.name == 'Heading 2':

print("Heading 2:", p.text)

# 读取 word 中所有正文

for p in document.paragraphs:

if p.style.name == 'Normal':

print("Normal:", p.text)

document.save('修改后的报告.docx')

效果如下:

读取docx中表格内容

示例代码:

if __name__ == "__main__":

document = Document('6 学生成绩报告.docx')

# 读取表格内容

for tb in document.tables:

for i,row in enumerate(tb.rows):

for j,cell in enumerate(row.cells):

text = ''

for p in cell.paragraphs:

text += p.text

print(f'第{i}行,第{j}列的内容{text}')

document.save('修改后的报告.docx')

效果如下:

修改word中的内容

示例代码:

if __name__ == "__main__":

document = Document('6 学生成绩报告.docx')

# 修改 word 中所有内容

for p in document.paragraphs:

p.text = "修改后的段落内容"

# 修改表格内容

for tb in document.tables:

for i,row in enumerate(tb.rows):

for j,cell in enumerate(row.cells):

text = ''

for p in cell.paragraphs:

p.text = ("第",str(i),"行",str(j),"列")

print(f'第{i}行,第{j}列的内容{text}')

document.save('6.4 修改后的报告.docx')

效果如下:

docx-mailmerge 自动生成万份劳动合同 创建合同模板添加内容框架

创建一个域

设置域名

依次全部添加

生成1份证明

示例代码:

from mailmerge import MailMerge

template = '薪资证明模板.docx'

document = MailMerge(template)

document.merge(name = '唐星',

id = '1010101010',

year = '2020',

salary = '99999',

job = '嵌入式软件开发工程师')

document.write('生成的1份证明.docx')

效果如下:

哈哈哈哈!!月入10万,走向人生巅峰~

生成10000份证明

示例代码:

from mailmerge import MailMerge

from datetime import datetime

# 生成单份合同

def GenerateCertify(templateName,newName):

# 打开模板

document = MailMerge(templateName)

# 替换内容

document.merge(name='唐星',

id='1010101010',

year='2020',

salary='99999',

job='嵌入式软件开发工程师')

# 保存文件

document.write(newName)

if __name__ == "__main__":

templateName = '薪资证明模板.docx'

# 获得开始时间

startTime = datetime.now()

# 开始生成

for i in range(10000):

newName = f'./10000份证明/薪资证明{i}.docx'

GenerateCertify(templateName,newName)

# 获取结束时间

endTime = datetime.now()

# 计算时间差

allSeconds = (endTime - startTime).seconds

print("生成10000份合同一共用时: ",str(allSeconds)," 秒")

print("程序结束!")

效果如下:

只花了89秒,平均不到 0.01 就能生成一个!!快

-完-

看到这还没来得及跑的同学,给个三连好吗?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值