python在word文书档案里插入图片和表格范例代码演练

python在word文书档案里插入图片和表格范例代码演练

# -*- coding: UTF8 -*-

from docx import Document

from docx.shared import Pt

doc = Document()

# 文献保存路途

path = "C:\\Users\\Administrator\\Desktop\\word文书档案\\"

# 读取文书档案

# doc = Document(path + "hello.docx")

# 增添图片,反面的参数树立图片尺寸,不妨选填

doc.add_picture(path + 'cat.jpg', width=Pt(300))

# 增添一个4行、3列的表格,style引入款式

doc_table = doc.add_table(rows=4, cols=3, style='Medium List 1 Accent 1')

# 树立表头

doc_table.rows[0].cells[0].text = '全名'

doc_table.rows[0].cells[1].text = '性别'

doc_table.rows[0].cells[2].text = '爱好'

# 表体数据保存

date = (

("张三", "男","排球"),

("李四", "男","台球"),

("王舞", "女","羽毛球"),

)

# 增添数据,add_row()新增一条龙

for a,b,c in date:

new_row = doc_table.add_row()

new_row.cells[0].text = a

new_row.cells[1].text = b

new_row.cells[2].text = c

# 保存文书档案

doc.save(path + "new.doc")

python在word文书档案里插入图片和表格范例代码演练

分享到 :

Leave a Reply

Your email address will not be published. Required fields are marked *