上几章节我们主要学习了如何读取文章,而主要任务是读取文档中的文本信息,也就是字符串,而图片本身是不可读的文件所以并没有去读取图片。从今天开始将学习如何通过 python 脚本来自动生成一个 word 文档。
在学习如何生成一个 word 文档之前,我们来看看如何保存生成 word 文件,因为马上就会用到。
使用方法:
document_obj.save(文件地址) ---> /home/demo.docx
代码示例如下:
1 2 3 4 5 6 | # coding:utf-8 from docx import Document doc = Document() doc.save( 'test.docx' ) |
运行结果如下:
使用方法:
title_obj = DocumentObj.add_heading(标题内容, 标题样式等级) 通过 Document 对象调用 add_heading 函数 返回 标题对象。
标题样式等级:
0 <= lever <= 9
标题内容追加:
titleobj.add_run 通过标题对象调用 add_run 函数 进行标题内容的追加
代码示例如下:
1 2 3 4 5 6 7 8 9 10 | # coding:utf-8 from docx import Document doc = Document() title = doc.add_heading( 'this is title' , 1 ) # 添加 word 文件的 title 标题 title.add_run( '\n - 测试版本' ) # 针对 title 标题进行内容追加(换行) doc.save( 'test.docx' ) |
运行结果如下:
使用方法:
para_obj = document_obj.add_paragraph(段落内容) 通过 Document 对象调用 add_paragraph 函数 返回 段落对象。
段落内容追加:
para_obj.add_run(字符串内容)
换行方式:
\n 换行特殊字符来分割段落
代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | # coding:utf-8 from docx import Document doc = Document() title = doc.add_heading( 'this is title' , 1 ) # 添加 word 文件的 title 标题 title.add_run( '\n - 测试版本' ) # 针对 title 标题进行内容追加(换行) para = doc.add_paragraph( '这是 \"test.docx\" 文件的第一行段落' ) para.add_run( '\n这是 \"test.docx\" 文件追加的的第二行段落' ) doc.save( 'test.docx' ) |
运行结果如下:
使用方法:
image_obj = document_obj.add_picture(图片地址, 宽, 高) 通过 Document 对象调用 add_picture 函数 返回 图片对象。
宽高定义:
from docx.shared import Inches
add_picture(x, width=Inches(5), height=Inches(5))
代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # coding:utf-8 from docx import Document from docx.shared import Inches doc = Document() title = doc.add_heading( 'this is title' , 1 ) # 添加 word 文件的 title 标题 title.add_run( '\n - 测试版本' ) # 针对 title 标题进行内容追加(换行) para = doc.add_paragraph( '这是 \"test.docx\" 文件的第一行段落' ) para.add_run( '\n这是 \"test.docx\" 文件追加的的第二行段落' ) image = doc.add_picture( 'test_image.png' , width = Inches( 3 ), height = Inches( 1.5 )) doc.save( 'test.docx' ) |
运行结果如下:
使用方法:
table_obj = document_obj.add_table(row=行数, cols=列数) 通过 Document 对象调用 add_table 函数 返回 表格对象。
cell = table_obj.row[0].cells 表格对象调用 rows 返回表格的行对象
cell[0].text = 当前行 0 列的内容
cell[1].text = 当前行 1 列的内容
表格追加:
row_cell = table.add_row().cells
代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # coding:utf-8 from docx import Document from docx.shared import Inches doc = Document() title = doc.add_heading( 'this is title' , 1 ) # 添加 word 文件的 title 标题 title.add_run( '\n - 测试版本' ) # 针对 title 标题进行内容追加(换行) para = doc.add_paragraph( '这是 \"test.docx\" 文件的第一行段落' ) para.add_run( '\n这是 \"test.docx\" 文件追加的的第二行段落' ) image = doc.add_picture( 'test_image.png' , width = Inches( 3 ), height = Inches( 1.5 )) table_title = [ 'name' , 'age' , 'sex' ] table = doc.add_table(rows = 1 , cols = 3 ) table_cells = table.rows[ 0 ].cells table_cells[ 0 ].text = table_title[ 0 ] table_cells[ 1 ].text = table_title[ 1 ] table_cells[ 2 ].text = table_title[ 2 ] data = [ ( 'Neo' , '18' , 'man' ), ( 'Adem' , '17' , 'man' ), ( 'Lily' , '18' , 'women' ) ] for i in data: row_cells = table.add_row().cells row_cells[ 0 ].text = i[ 0 ] row_cells[ 1 ].text = i[ 1 ] row_cells[ 2 ].text = i[ 2 ] doc.save( 'test.docx' ) |
运行结果如下:
使用方法:
document_obj.add_page_break()
代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | # coding:utf-8 from docx import Document from docx.shared import Inches doc = Document() title = doc.add_heading( 'this is title' , 1 ) # 添加 word 文件的 title 标题 title.add_run( '\n - 测试版本' ) # 针对 title 标题进行内容追加(换行) para = doc.add_paragraph( '这是 \"test.docx\" 文件的第一行段落' ) para.add_run( '\n这是 \"test.docx\" 文件追加的的第二行段落' ) image = doc.add_picture( 'test_image.png' , width = Inches( 3 ), height = Inches( 1.5 )) # 添加图片 table_title = [ 'name' , 'age' , 'sex' ] # 定义表格的第一行的标题 table = doc.add_table(rows = 1 , cols = 3 ) # 定义表格的行数、列数 table_cells = table.rows[ 0 ].cells # 将 table_title 的每列的名称写入表格 table_cells[ 0 ].text = table_title[ 0 ] table_cells[ 1 ].text = table_title[ 1 ] table_cells[ 2 ].text = table_title[ 2 ] data = [ # 定义 data 的内容,准备将其追加写入表格 ( 'Neo' , '18' , 'man' ), ( 'Adem' , '17' , 'man' ), ( 'Lily' , '18' , 'women' ) ] for i in data: # 利用 for 循环将 data 追加写入表格 row_cells = table.add_row().cells row_cells[ 0 ].text = i[ 0 ] row_cells[ 1 ].text = i[ 1 ] row_cells[ 2 ].text = i[ 2 ] doc.add_page_break() # 添加 word 文件的分页 title = doc.add_heading( 'this is page_2 title' , 1 ) # 添加 word 文件的第二分页的 title 标题 doc.save( 'test.docx' ) |
运行结果如下:
到此这篇关于Python自动化办公之Word文档的创建与生成的文章就介绍到这了,更多相关Python Word创建生成内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
联系客服