【Python】利用python-docx生成word版本学生花名册

如图,可以用python创建word文档,生成一个学生的花名册。生成的过程:先下载第三方依赖包,安装依赖包,然后引入依赖文件,创建docx文件,添加标题,创建表头,创建表格正文,居中填写表头内容、项目行、表格正文。

目录

一、安装Python-docx包

(一)下载

1、官网下载

2、下载步骤

(二)安装步骤

二、文件格式

(一)标题栏

(二)表格

1、表头

2、项目行

3、表格正文

三、python-docx中表格相关知识

(一)表格设计

1、表格框架

2、单元格

3、段落

(二)表格内容居中

1、水平居中

2、垂直居中

四、完整程序代码

(一)引入依赖

(二)声明学生花名册相关变量

(三)最终代码


一、安装Python-docx包

(一)下载

1、官网下载

官网网址:python-docx · PyPI

2、下载步骤

点击Download files,点击python-docx-0.8.11.tar.gz压缩包开始下载。

(二)安装步骤

打开cmd命令行窗口,把刚刚下载的python-docx-0.8.11.tar.gz压缩包复制到命令行窗口默认的目录下,一般都是C:\Users\****>。

在命令行输入pip install python-docx-0.8.11.tar.gz后回车,执行完安装的结果如下图:

二、文件格式

(一)标题栏

word文档设定一个标题栏,标题为“金庸小学学生花名册”。

(二)表格

1、表头

表头为1行1列,是表格的名称“四年(5)班”。

2、项目行

学生花名册一共有学号、姓名、性别、出生日期、民族5个项目,项目行为1行5列。

3、表格正文

表格正文为10个学生的具体信息,为10行5列。

三、python-docx中表格相关知识

(一)表格设计

1、表格框架

因为表头和项目行、表格正文格式不一样,所以需要创建两个表格,1个为1行1列的表格table1,1个为11行5列的表格table2。

python-docx中创建表格的方法为add_table(行数,列数)

# 创建表头
table1=document.add_table(1,1)

# 创建表格
table2=document.add_table(11,5)

2、单元格

python-docx中表格中的单元格为table.cell(行序号,列序号)。

3、段落

python-docx中表格中的单元格中的文字内容为段落,为table.cell(行序号,列序号).paragraphs[段落序号]。

(二)表格内容居中

1、水平居中

table1.cell(0,0).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER

2、垂直居中

因为每个表格里创建的是一个段落,段落行距离,有段前、段后距离,可以设置段前、段后的距离等于行距的一半即可以完成垂直居中。

table1.cell(0,0).paragraphs[0].paragraph_format.line_spacing = Pt(12)
table1.cell(0,0).paragraphs[0].paragraph_format.space_before = Pt(6)
table1.cell(0,0).paragraphs[0].paragraph_format.space_after = Pt(6)

四、完整程序代码

(一)引入依赖

#引入word文件包
from docx import Document

#引入表格对齐文件包
from docx.enum.table import WD_TABLE_ALIGNMENT

#引入表格段落对齐文件包
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

#引入英尺、磅尺寸文件包
from docx.shared import Inches,Pt

#引入表格单元格水平对齐文件包
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT

(二)声明学生花名册相关变量

# 学生学号起始编号
student_no=20230001

# 花名册项目名称
student_item = ["学号","姓名","性别","出生日期","民族"]

# 学生姓名列表
student_name = ["丁春秋","于万亭","王语嫣","平四","乔峰","李秋水","沈城","胡斐","袁紫衣","梅超风"]

# 学生民族列表
student_ethnic = ["汉族","汉族","白族","汉族","契丹","党项","汉族","汉族","汉族","汉族"]

# 学生性别列表
student_gender = ["男","男","女","男","男","女","男","男","女","女"]

# 学生出生年代列表
student_birthday = [1028,1700,1074,1738,1062,1000,1475,1753,1755,1191]

(三)最终代码

#引入word文件包
from docx import Document

#引入表格对齐文件包
from docx.enum.table import WD_TABLE_ALIGNMENT

#引入表格段落对齐文件包
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

#引入英尺、磅尺寸文件包
from docx.shared import Inches,Pt

#引入表格单元格水平对齐文件包
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT


# 学生学号起始编号
student_no=20230001

# 花名册项目名称
student_item = ["学号","姓名","性别","出生日期","民族"]

# 学生姓名列表
student_name = ["丁春秋","于万亭","王语嫣","平四","乔峰","李秋水","沈城","胡斐","袁紫衣","梅超风"]

# 学生民族列表
student_ethnic = ["汉族","汉族","白族","汉族","契丹","党项","汉族","汉族","汉族","汉族"]

# 学生性别列表
student_gender = ["男","男","女","男","男","女","男","男","女","女"]

# 学生出生年代列表
student_birthday = [1028,1700,1074,1738,1062,1000,1475,1753,1755,1191]

# 创建文件
document = Document()

# 添加标题
h=document.add_heading('金庸小学学生花名册',0)

# 标题居中
h.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

# 创建表头
table1=document.add_table(1,1)

# 创建表格
table2=document.add_table(11,5)

# 填写表头内容
table1.cell(0,0).text="四年(5)班"

# 表头内容格式:行间距12磅,段前6磅,段后6磅,也就是竖直居中
table1.cell(0,0).paragraphs[0].paragraph_format.line_spacing = Pt(12)
table1.cell(0,0).paragraphs[0].paragraph_format.space_before = Pt(6)
table1.cell(0,0).paragraphs[0].paragraph_format.space_after = Pt(6)

# 表头内容格式:水平居中
table1.cell(0,0).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 水平居中


# 填写项目名称行
for i in range(5):
    table2.cell(0,i).text=student_item[i]
    table2.cell(0,i).paragraphs[0].paragraph_format.line_spacing = Pt(12)
    table2.cell(0,i).paragraphs[0].paragraph_format.space_before = Pt(6)
    table2.cell(0,i).paragraphs[0].paragraph_format.space_after = Pt(6)
    table2.cell(0,i).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 水平居中

# 填写学号列
for j in range(1,11):
    table2.cell(j,0).text=str(student_no+j-1)
    table2.cell(j,0).paragraphs[0].paragraph_format.line_spacing = Pt(12)
    table2.cell(j,0).paragraphs[0].paragraph_format.space_before = Pt(6)
    table2.cell(j,0).paragraphs[0].paragraph_format.space_after = Pt(6)
    table2.cell(j,0).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 水平居中

# 填写姓名列
for j in range(1,11):
    table2.cell(j,1).text=sudent_name[j-1]
    table2.cell(j,1).paragraphs[0].paragraph_format.line_spacing = Pt(12)
    table2.cell(j,1).paragraphs[0].paragraph_format.space_before = Pt(6)
    table2.cell(j,1).paragraphs[0].paragraph_format.space_after = Pt(6)
    table2.cell(j,1).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 水平居中

# 填写性别列
for j in range(1,11):
    table2.cell(j,2).text=sudent_gender[j-1]
    table2.cell(j,2).paragraphs[0].paragraph_format.line_spacing = Pt(12)
    table2.cell(j,2).paragraphs[0].paragraph_format.space_before = Pt(6)
    table2.cell(j,2).paragraphs[0].paragraph_format.space_after = Pt(6)
    table2.cell(j,2).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 水平居中

# 填写出生年代列
for j in range(10):
    table2.cell(j,3).text=str(student_birthday[j])
    table2.cell(j,3).paragraphs[0].paragraph_format.line_spacing = Pt(12)
    table2.cell(j,3).paragraphs[0].paragraph_format.space_before = Pt(6)
    table2.cell(j,3).paragraphs[0].paragraph_format.space_after = Pt(6)
    table2.cell(j,3).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 水平居中

# 填写民族列
for j in range(1,11):
    table2.cell(j,4).text=sudent_ethnic[j-1]
    table2.cell(j,4).paragraphs[0].paragraph_format.line_spacing = Pt(12)
    table2.cell(j,4).paragraphs[0].paragraph_format.space_before = Pt(6)
    table2.cell(j,4).paragraphs[0].paragraph_format.space_after = Pt(6)
    table2.cell(j,4).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER # 水平居中

# 保存文档
document.save('student.docx')

(全文完)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值