python自动化之word文档
#########################docx文件############################
'''
.docx文件有很多结构,有3种不同的类型来表示
在最高一层,Document对象表示整个文档
Document对象包含一个Paragraph对象的列表,表示文档中的段落,以回车键为准
每个Paragraph对象包含一个Run对象的列表
'''
#########################读取Word文档########################
import docx
doc=docx.Document(r'C:\Users\shenlu\Desktop\demo.docx')
len(doc.paragraphs)
doc.paragraphs[0].text
doc.paragraphs[1].text
len(doc.paragraphs[1].runs)
doc.paragraphs[1].runs[0].text
doc.paragraphs[1].runs[1].text
doc.paragraphs[1].runs[2].text
doc.paragraphs[1].runs[3].text
########################从.docx文件中取得完整的文本########################
import docx
def getText(filename):
doc=docx.Document(filename)
fullText=[]
for para in doc.paragraphs:
#########每一段有缩进#############
###fullText.append(' '+para.text)
fullText.append(para.text)
###段落之间增加空行,return '\n\n'.join(fullText)
return '\n'.join(fullText)
########################从.docx文件中取得完整的文本########################
import readDocx
print (readDocx.getText('demo.docx'))
########################设置Paragraph和Run对象的样式########################
'''
对于Word文档,有3种类型的样式:
段落样式可以应用于Paragraph对象,字符样式可以应用于Run对象
链接的样式可以应用于这两种对象
默认Word样式的字符串如下:
'Normal' 'BodyText' 'BodyText2' 'BodyText3' 'Caption' 'Heading1' 'Heading2' 'Heading3' 'Heading4'
'Heading5' 'Heading6' 'Heading7' 'Heading8' 'Heading9' 'IntenseQuote' 'List' 'List2' 'List3'
'ListBullet' 'ListBullet2' 'ListBullet3' 'ListContinue' 'ListContinue2' 'ListContinue3' 'ListNumber' 'ListNumber2' 'ListNumber3'
'ListParagraph' 'MacroText' 'NoSpacing' 'Quote' 'Subtitle' 'TOCHeading' 'Title'
'''
'''
Run对象的text属性
属性 描述
bold 文本以粗体出现
italic 文本以斜体出现
underline 文本带下划线
strike 文本带删除线
double_strike 文本带双删除线
all_caps 文本以大写首字母出现
small_caps 文本以大写首字母出现,小写字母小两个点
shadow 文本带阴影
outline 文本以轮廓线出现,而不是实心
rtl 文本从右至左书写
imprint 文本以刻入页面的方式出现
emboss 文本以凸出页面的方式出现
'''
########################################################################
import docx
doc=docx.Document(r'C:\Users\shenlu\Desktop\demo.docx')
doc.paragraphs[0].text
doc.paragraphs[0].style
doc.save(r'C:\Users\shenlu\Desktop\demo.docx')
doc.paragraphs[0].style='Heading 1'
>>> doc.paragraphs[1].style
_ParagraphStyle('No Spacing') id: 124515664
doc.paragraphs[1].text
(doc.paragraphs[1].runs[0].text,doc.paragraphs[1].runs[1].text,doc.paragraphs[1].runs[2].text,doc.paragraphs[1].runs[3].text)
doc.paragraphs[1].runs[1].underline=True
doc.paragraphs[1].runs[3].underline=True
doc.save(r'C:\Users\shenlu\Desktop\demo.docx')
#################################写入Word文档################################
import docx
doc=docx.Document()
doc.add_heading('Header 0',0)
doc.add_heading('Header 1',1)
doc.add_heading('Header 2',2)
doc.add_heading('Header 3',3)
doc.add_heading('Header 4',4)
doc.add_paragraph('Hello world!')
doc.add_picture(r'C:\Users\shenlu\Desktop\DSCN0859.jpg',width=docx.shared.Inches(1),height=docx.shared.Cm(4))
paraObj1=doc.add_paragraph('This is a second paragraph.')
paraObj2=doc.add_paragraph('This is a yet another paragraph.')
paraObj1.add_run('This text is being added to the second paragraph.')
doc.save(r'C:\Users\shenlu\Desktop\helloworld.docx')
#################################添加换行符和换页符################################
import docx
from docx.enum.text import WD_BREAK
doc=docx.Document()
doc.add_paragraph('This is on the first page!')
#doc.paragraphs[0].runs[0].add_break() ###换行
doc.paragraphs[0].runs[0].add_break(WD_BREAK.PAGE) ###换页
doc.add_paragraph('This is on the second page!')
doc.save(r'C:\Users\shenlu\Desktop\twoPage.docx')
#################################定制邀请函################################
import docx,os
txtcontent=open(r'C:\Users\shenlu\Desktop\guests.txt','rb')
lines=txtcontent.readlines()
for line in lines:
print line
line=line.replace('\r\n','')
doc=docx.Document()
doc.add_paragraph('It would be a pleasure to have the company of')
doc.add_paragraph(line)
doc.add_paragraph('at 11010 memory laue on the euening of')
doc.add_paragraph('April lst')
doc.add_paragraph('at 7 o\'clock')
doc.save(os.path.join(r'C:\Users\shenlu\Desktop',line+'.docx'))
txtcontent.close()
#############################################################################
http://nostarch.com/automatestuff/
python自动化之word文档的更多相关文章
- Python批量创建word文档(2)- 加图片和表格
Python创建word文档,任务要求:小杨在一家公司上班,每天都需要给不同的客户发送word文档,以告知客户每日黄金价格.要求在文档开始处给出banner条,价格日期等用表格表示.最后贴上自己的联系 ...
- Python批量创建word文档(1)- 纯文字
Python创建word文档,任务要求:小杨在一家公司上班,每天都需要给不同的客户发送word文档,以告知客户每日黄金价格.最后贴上自己的联系方式.代码如下: 1 ''' 2 #python根据需求新 ...
- 使用 python 创建&更改 word 文档
使用 python 修改 word 文档 说明:这个需求是老师想要一个自动识别 word 文档中指定位置的分数,并填入相应表格. 使用库 python-docx 的官方文档地址是:python-doc ...
- 自动化工具word文档批量转html
企业有很多的科室,科室的每个人或多或少都会写一些文档,有些文档领导需要浏览,解决的办法是将编辑的文档打印出来,供领导浏览,或是为了节约企业成本,文档就在人与人这间或部门之间copy过来,copy过去. ...
- Python将word文档批量转PDF
前面有一篇<Python批量创建word文档(2)- 加图片和表格>的文章,利用这篇文章创建的word文档来批量转PDF文档.代码: 1 ''' 2 #python批量将word文档转换成 ...
- 如何用python自动编写《赤壁赋》word文档
目录 前言 安装-python-docx 一.自动编写<赤壁赋> 准备数据 新建文档 添加标题 添加作者 添加朝代 添加图片 添加段落 保存word文档 二.自动提取<赤壁赋> ...
- 使用python编辑和读取word文档
python调用word接口主要用到的模板为python-docx,基本操作官方文档有说明. python-docx官方文档地址 使用python新建一个word文档,操作就像文档里介绍的那样: fr ...
- word文档的python解析
主要两块,第一个是文件类型的转换,第二个是用docx包去对word文档中的table进行parse 1. 文件格式装换 因为很多各种各样的原因,至今还有一些word文档是doc的格式存的,对于这种,如 ...
- Python将word文档转换成PDF文件
如题. 代码: ''' #將word文档转换为pdf文件 #用到的库是pywin32 #思路上是调用了windows和office功能 ''' #导入所需库 from win32com.client ...
随机推荐
- Python码农福音,GitHub增加Python语言安全漏洞告警
在 2017 年 GitHub 开始对托管在其网站的代码仓库和依赖库开始提供安全漏洞检查和告警,开始时候只支持 Ruby 和 JavaScript 语言的项目.根据 GitHub 官方数据显示截止目前 ...
- lua编程之协程介绍
一,lua协程简介 协程(coroutine),意思就是协作的例程,最早由Melvin Conway在1963年提出并实现.跟主流程序语言中的线程不一样,线程属于侵入式组件,线程实现的系统称之为抢占式 ...
- ncl 函数源码 gc_inout
转自气象家园论坛 经过不懈努力,终于找到了gc_inout函数的源代码,原来在这个文件里面!一颗赛艇 位置:/ncl_ncarg-6.5.0-src/ni/src/lib/nfpfort/sg_too ...
- 180918-JDK之Deflater压缩与Inflater解压
JDK 压缩与解压工具类 在实际的应用场景中,特别是对外传输数据时,将原始数据压缩之后丢出去,可以说是非常常见的一个case了,平常倒是没有直接使用JDK原生的压缩工具类,使用Protosutff和K ...
- Windows系统环境变量之path环境变量(Java, Python环境变量配置)
系统: Windows10 path系统环境变量的作用: Windows和DOS操作系统中的path环境变量,当要求系统运行一个程序而没有告诉它程序所在的完整路径时,系统除了在当前目录下面寻找此程序外 ...
- Elasticsearch的停用词(stopwords)
1.问题 在使用搜索引擎(Elasticsearch或Solr)作为应用的后台搜索平台的时候,会遇到停用词(stopwords)的问题. 在信息检索中,停用词是为节省存储空间和提高搜索效率,处理文本时 ...
- 01-numpy基础简介
import numpy as np # ndarray ''' # 三种创建方式 1.从python的基础数据对象转化 2.通过numpy内置的函数生成 3.从硬盘(文件)读取数据 ''' # 创建 ...
- __construct 与 __destruct 区别
其实这个问法是有问题的,__construct 与 __destruct 没什么可比性,两个方法一个在对象被创建的时候触发,另一个在对象被销毁的时候触发 具体可以翻阅PHP官方手册中的 http:// ...
- JDK8 metaspace调优
从JDK8开始,永久代(PermGen)的概念被废弃掉了,取而代之的是一个称为Metaspace的存储空间.Metaspace使用的是本地内存,而不是堆内存,也就是说在默认情况下Metaspace的大 ...
- Python之并发编程-多线程
目录 一.threading模块介绍二.使用说明三.进一步介绍(守护线程,锁(互斥锁.递归锁),信号量,队列,event,condition,定时器) 1.守护线程 2.锁(互斥锁.递归锁) 3.信号 ...