Python学习------用python操作word文件

一、用Python操作word文档的方法

使用python来对word文档进行操作的方法其实有很多,本文里只是写了用docx扩展包的方法(因为只学了这个方法[ennnn])。

1、docx扩展包的优点是:

  • 文档中的段落、文本、字体等都看做对象,对对象进行处理就是对word文档的内容处理。
  • 不依赖操作系统,可以跨平台

2、python-docx的几个基本对象

  • Document对象:表示一个word文档。
  • Paragraph对象:表示word文档中的一个段落
  • Paragraph对象的text属性:表示段落中的文本内容。

3、docx的安装

  • pip install python-docx

二、用docx操作word

1、导入Document,创建一个word文档对象

  • 我们对word操作,导入的库为Document
  • from docx import Document
  • doc = Document()
  • 括号里什么参数也没有表示创建一个新的word文档对象,如果想打开一个已有的文档,只需要在括号里添加文档的路径地址就可以了。

2、导入WD_STYLE_TYPE,查看文档段落格式

我们用WPS/office操作文档的时候,不可能从头到尾都是一个格式的对吧,我们经常会用到标题,标题一,标题二,正文等格式,这是一篇文档所包含的基本元素,用Python操作word文档的时候当然也不能忽略这些格式:

  • 导入格式的库
  •  from docx.enum.style import WD_STYLE_TYPE
  • 查看这个库里有多少格式
In [7]: for s in doc.styles:
   ...:     if s.type == WD_STYLE_TYPE.PARAGRAPH:
   ...:         print(s)
   ...:         print(s.font)
   ...:         print(s.name)
   ...:
_ParagraphStyle('Normal') id: 109084624
<docx.text.font.Font object at 0x0000000006807358>
Normal
_ParagraphStyle('Header') id: 109081432
<docx.text.font.Font object at 0x0000000006807EF0>
Header
_ParagraphStyle('Footer') id: 109081432
<docx.text.font.Font object at 0x0000000006807EF0>
Footer
_ParagraphStyle('Heading 1') id: 109081432
<docx.text.font.Font object at 0x0000000006807EF0>
Heading 1
_ParagraphStyle('Heading 2') id: 109084232
<docx.text.font.Font object at 0x0000000006807EF0>
Heading 2
_ParagraphStyle('Heading 3') id: 109084624
<docx.text.font.Font object at 0x0000000006807EF0>
Heading 3
_ParagraphStyle('Heading 4') id: 109081432
<docx.text.font.Font object at 0x0000000006807EF0>
Heading 4
_ParagraphStyle('Heading 5') id: 109084232
<docx.text.font.Font object at 0x0000000006807EF0>
Heading 5
_ParagraphStyle('Heading 6') id: 109084624
<docx.text.font.Font object at 0x0000000006807EF0>
Heading 6
_ParagraphStyle('Heading 7') id: 109081432
<docx.text.font.Font object at 0x0000000006807EF0>
Heading 7
_ParagraphStyle('Heading 8') id: 109084232
<docx.text.font.Font object at 0x0000000006807EF0>
Heading 8
_ParagraphStyle('Heading 9') id: 109084624
<docx.text.font.Font object at 0x0000000006807EF0>
Heading 9
_ParagraphStyle('No Spacing') id: 109084624
<docx.text.font.Font object at 0x0000000006807EF0>
No Spacing
_ParagraphStyle('Title') id: 109084624
<docx.text.font.Font object at 0x0000000006807EF0>
Title
_ParagraphStyle('Subtitle') id: 109084624
<docx.text.font.Font object at 0x0000000006807E48>
Subtitle
_ParagraphStyle('List Paragraph') id: 109084624
<docx.text.font.Font object at 0x0000000006807E48>
List Paragraph
_ParagraphStyle('Body Text') id: 109084400
<docx.text.font.Font object at 0x0000000006807E48>
Body Text
_ParagraphStyle('Body Text 2') id: 109084400
<docx.text.font.Font object at 0x0000000006807E48>
Body Text 2
_ParagraphStyle('Body Text 3') id: 109084400
<docx.text.font.Font object at 0x0000000006807E48>
Body Text 3
_ParagraphStyle('List') id: 109084400
<docx.text.font.Font object at 0x0000000006807E48>
List
_ParagraphStyle('List 2') id: 109084232
<docx.text.font.Font object at 0x0000000006807358>
List 2
_ParagraphStyle('List 3') id: 109081432
<docx.text.font.Font object at 0x0000000006807EF0>
List 3
_ParagraphStyle('List Bullet') id: 109084400
<docx.text.font.Font object at 0x0000000006807E48>
List Bullet
_ParagraphStyle('List Bullet 2') id: 109084624
<docx.text.font.Font object at 0x0000000006807E48>
List Bullet 2
_ParagraphStyle('List Bullet 3') id: 109081432
<docx.text.font.Font object at 0x0000000006807E48>
List Bullet 3
_ParagraphStyle('List Number') id: 109084400
<docx.text.font.Font object at 0x0000000006807E48>
List Number
_ParagraphStyle('List Number 2') id: 109084624
<docx.text.font.Font object at 0x0000000006807E48>
List Number 2
_ParagraphStyle('List Number 3') id: 109081432
<docx.text.font.Font object at 0x0000000006807E48>
List Number 3
_ParagraphStyle('List Continue') id: 109084400
<docx.text.font.Font object at 0x0000000006807E48>
List Continue
_ParagraphStyle('List Continue 2') id: 109084624
<docx.text.font.Font object at 0x0000000006807E48>
List Continue 2
_ParagraphStyle('List Continue 3') id: 109081432
<docx.text.font.Font object at 0x0000000006807E48>
List Continue 3
_ParagraphStyle('macro') id: 109084400
<docx.text.font.Font object at 0x0000000006807E48>
macro
_ParagraphStyle('Quote') id: 109084400
<docx.text.font.Font object at 0x0000000006807358>
Quote
_ParagraphStyle('Caption') id: 109084400
<docx.text.font.Font object at 0x0000000006807E48>
Caption
_ParagraphStyle('Intense Quote') id: 109084624
<docx.text.font.Font object at 0x0000000006807E48>
Intense Quote
_ParagraphStyle('TOC Heading') id: 109081432
<docx.text.font.Font object at 0x0000000006807E48>
TOC Heading

3、导入shared里的各种xxx,对文字添加样式

我们在用PWS编辑文档的时候还会用到一个功能,那就是文字样式,比如加粗,加下划线,变换文字颜色等,docx扩展包也支持这些操作,这些操作都放在了shared里。比如我想对字体进行操作,那我就导入Pt模块

  • 导入模块
  • from docx.shared import Pt

4、好了,我们现在能创建Document对象,能编辑段落,还能对文字进行操作,那我们现在就来简单的操作一下文档吧。

注意每一段都是一个对象,都有对象名,我们通过对象名来对这一段进行操作

# -*- coding:utf-8 -*-
# test_document.py
# python操作word文档

from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH  # 添加对齐样式
from docx.shared import Pt
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
from docx.shared import Inches


# 创建document对象
doc_1 = Document()

# 加一个标题
# level表示几级标题,默认的时候为1,level=0时为大标题
head_1 = doc_1.add_heading("这是个测试的文档heading", level=0)

# 添加正文新的一个段落
# 第二个参数为样式,默认为正文
# Subtitle为副标题
paragraph_1 = doc_1.add_paragraph('kenny word', 'Subtitle')
paragraph_2 = doc_1.add_paragraph("今天早饭吃些什么呢?","Title")

# 给paragraph_2段落追加内容
paragraph_2.add_run("包子")
paragraph_2.add_run("饺子")
paragraph_2.add_run("大米粥")

# 设置为居中对齐
paragraph_3 = doc_1.add_paragraph("这个段落居中对齐")
paragraph_3.alignment = WD_ALIGN_PARAGRAPH.CENTER

# 对文字进行调整,注意更改的对象是文字,而不是段落
paragraph_4 = doc_1.add_paragraph("这个段落文字用来更改:")
par4_run = paragraph_4.add_run("这段文字发生了变化")
par4_run.font.bold = True  # 是否加粗
par4_run.font.size = Pt(20)  # 字号

# 添加图像
paragraph_5 = doc_1.add_picture('E:\Python\office\demo.png')
# 图像默认是左对齐的,若想更改对齐格式,可以像调整文字那样
last_paragraph = doc_1.paragraphs[-1]
last_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER

# 添加表格
# rows=行,cols=列.
rows = 5
cols = 5
table_1 = doc_1.add_table(rows, cols)

# 获取某一个单元格,例如获取单元格(1,1)
# 单元格从(0,0)开始
cell_1 = table_1.cell(1, 1)

# 对这个单元格进行文本添加
cell_1.text = 'kenny'

# 可以合并写
table_1.cell(3, 4).text = 'lalala'

# 更改表格样式
table_1.style = 'Light Grid Accent 6'

# 添加一行
row = table_1.add_row()

# 保存文档
doc_1.save("E:\\Python\\office\\test1.docx")

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值