赞
踩
openpyxl是Python下的Word库,它能够很容易的对Word文档进行读取
pip install python-docx
pip install -i https://mirrors.aliyun.com/pypi/simple/ python-docx
(推荐,安装更快)from docx import Document from docx.enum.text import WD_PARAGRAPH_ALIGNMENT from docx.shared import Cm, RGBColor, Pt doc = Document() """添加标题""" para_head = doc.add_heading("正文一级标题",level=1) # level代表标题级别 para_head.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 标题居中 """添加正文段落""" p1 = doc.add_paragraph("我是正文第一段") p1.text += ",第一段追加文本" p2 = doc.add_paragraph("我是正文第二段") p2.insert_paragraph_before("我在第二段之前") # doc.add_page_break() # 添加分页符 """添加带文字块的段落""" p3 = doc.add_paragraph("我是带样式的段落——") p3.add_run("加粗").bold = True p3.add_run("倾斜").italic = True p3.add_run("正常") red_run = p3.add_run("20,加粗,红色") red_run.font.size = Pt(20) red_run.font.bold = True red_run.font.color.rgb = RGBColor(255,0,0) """添加有序和无序列表""" opts = ['选项1','选项2', '选项3'] for opt in opts: # 有序列表 doc.add_paragraph(opt, style='List Number') for opt in opts: # 无序列表 doc.add_paragraph(opt, style='List Bullet') """添加表格""" list1 = [["姓名","性别","家庭地址"],["唐僧","男","湖北省"], ["孙悟空","男","北京市"],["猪八戒","男","广东省"]] table = doc.add_table(rows=4,cols=3) # 创建指定行列的表格 for row_index in range(4): row_cells = table.rows[row_index].cells for cell_index in range(3): row_cells[cell_index].text = str(list1[row_index][cell_index]) row_cells = table.add_row().cells # 添加一行 row_cells[0].text = "沙和尚" row_cells[1].text = "男" row_cells[2].text = "湖南省" """添加图片""" doc.add_picture(r"resource/python.png") # 默认大小 doc.add_picture(r"resource/word.png",width=Cm(5),height=Cm(5)) # 指定宽高 """保存文档""" doc.save(r"resource/Test.docx")
from docx import Document
path = r"resource/Test.docx"
"""获取段落"""
doc = Document(path) # 读取的doc可通过save方法保存
for paragraph in doc.paragraphs:
print(paragraph.text,paragraph.style.name) # 段落text可修改,文字块、表格同样
from docx import Document
path = r"resource/Test.docx"
"""获取文字块"""
p5 = doc.paragraphs[4]
for run in p5.runs:
print(run.text,run.bold,run.italic)
from docx import Document
path = r"resource/Test.docx"
"""获取表格"""
table0 = doc.tables[0]
for row in table0.rows: # 获取表格所有行
for cell in row.cells: # 获取表格行的所有单元格
print(cell.text,end=" ")
print()
print(table0.cell(2,0).text) # 根据行列索引获取指定单元格
import os
from docx import Document
path = r"resource/Test.docx"
"""获取图片"""
for rel in doc.part.rels: # 获取所有part对象的id(rel→str,例如rId3)
rel = doc.part.rels[rel] # 根据id获取文件对象
if "image" in rel.target_ref: # 根据part对象的名称判断是否是图片(例如media/image1.jpeg)
with open("resource/"+os.path.basename(rel.target_ref), "wb") as f:
f.write(rel.target_part.blob) # 将图片以二进制格式写入
for style in doc.styles:
print(style)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。