#########################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文档的更多相关文章

  1. Python批量创建word文档(2)- 加图片和表格

    Python创建word文档,任务要求:小杨在一家公司上班,每天都需要给不同的客户发送word文档,以告知客户每日黄金价格.要求在文档开始处给出banner条,价格日期等用表格表示.最后贴上自己的联系 ...

  2. Python批量创建word文档(1)- 纯文字

    Python创建word文档,任务要求:小杨在一家公司上班,每天都需要给不同的客户发送word文档,以告知客户每日黄金价格.最后贴上自己的联系方式.代码如下: 1 ''' 2 #python根据需求新 ...

  3. 使用 python 创建&更改 word 文档

    使用 python 修改 word 文档 说明:这个需求是老师想要一个自动识别 word 文档中指定位置的分数,并填入相应表格. 使用库 python-docx 的官方文档地址是:python-doc ...

  4. 自动化工具word文档批量转html

    企业有很多的科室,科室的每个人或多或少都会写一些文档,有些文档领导需要浏览,解决的办法是将编辑的文档打印出来,供领导浏览,或是为了节约企业成本,文档就在人与人这间或部门之间copy过来,copy过去. ...

  5. Python将word文档批量转PDF

    前面有一篇<Python批量创建word文档(2)- 加图片和表格>的文章,利用这篇文章创建的word文档来批量转PDF文档.代码: 1 ''' 2 #python批量将word文档转换成 ...

  6. 如何用python自动编写《赤壁赋》word文档

    目录 前言 安装-python-docx 一.自动编写<赤壁赋> 准备数据 新建文档 添加标题 添加作者 添加朝代 添加图片 添加段落 保存word文档 二.自动提取<赤壁赋> ...

  7. 使用python编辑和读取word文档

    python调用word接口主要用到的模板为python-docx,基本操作官方文档有说明. python-docx官方文档地址 使用python新建一个word文档,操作就像文档里介绍的那样: fr ...

  8. word文档的python解析

    主要两块,第一个是文件类型的转换,第二个是用docx包去对word文档中的table进行parse 1. 文件格式装换 因为很多各种各样的原因,至今还有一些word文档是doc的格式存的,对于这种,如 ...

  9. Python将word文档转换成PDF文件

    如题. 代码: ''' #將word文档转换为pdf文件 #用到的库是pywin32 #思路上是调用了windows和office功能 ''' #导入所需库 from win32com.client ...

随机推荐

  1. react-native初体验(1) — hello world

    没有简介,直接开始干活吧. 默认阅读本文的你已经安装好 nodejs, windows用户需要升级yarn到最新版本. 并且设置安装源为国内的淘宝源: npm config set registry ...

  2. python类与对象各个算数运算魔法方法总结

    1.python类与对象各个算术运算魔法方法总结: 2.各个魔法方法应用举例: 3.实例训练: (1)我们都知道在 Python 中,两个字符串相加会自动拼接字符串,但遗憾的是两个字符串相减却抛出异常 ...

  3. Unity面试技巧之C#基础

    1. 定义常量最好使用运行是常量就是readonly 编译常量就是 const public static readonly MyClass myClass = new MyClass(); publ ...

  4. c#随机生成中文姓名

    为什么要自己写这个生成器呢?大家应该都有过为测试数据发愁的时候,我就是出于这样的原因. 尽管本次代码很少,但是还会有后续的生成器分享出来. 我代码底子还不是很好,希望各位同道能够发表意见,同是也欢迎大 ...

  5. sklearn 中的 Pipeline 机制

    转载自:https://blog.csdn.net/lanchunhui/article/details/50521648 from sklearn.pipeline import Pipeline ...

  6. CHAPTER 25 The Greatest Show on Earth 第25章 地球上最壮观的演出

    CHAPTER 25 The Greatest Show on Earth 第25章 地球上最壮观的演出 Go for a walk in the countryside and you will f ...

  7. GIT rebase讲解

    对分支进行rebase 从master分支checkout出fork分支,并在master和fork上都进行了一些修改 现在fork分支想要及时的同步master分支上的修改,避免在已经失效的代码上继 ...

  8. php从入门到放弃系列-02.php基础语法

    php从入门到放弃系列-02.php基础语法 一.学习语法,从hello world开始 PHP(全称:PHP:Hypertext Preprocessor,即"PHP:超文本预处理器&qu ...

  9. [文章存档]Azure .net WebAPP的js/css文件过大导致访问慢的解决办法

    https://docs.azure.cn/zh-cn/articles/azure-operations-guide/app-service-web/aog-app-service-web-qa-j ...

  10. UIWebView控件中 字体大小和字体样式的修改

    修改UIWebView控件中字体的样式: NSString *htmlString = [NSString stringWithContentsOfFile:self.webPath encoding ...