#########################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. Python无参装饰器

    需求:想要在test_func函数前后执行一些代码   1.第一步(定义函数,将调用原函数,使用新函数替换) def test_func(): return 'test_func' def test_ ...

  2. UWP 卡片视图 Card View

    上一篇 提到了 UWP 轨道视图Orbit View,这次就说一下卡片视图,毕竟两个差不多. 卡片视图,效果如其名,卡片一样,左右滑动,当然能翻牌最好了. 嗯,我这个可以的额(⊙﹏⊙)... 看下效果 ...

  3. 设置pdsh的默认登录模式

    1.check your pdsh default rcmd rsh pdsh -q -w localhostSee what your pdsh default rcmd is. 2.Modify ...

  4. rest的Web服务端获取http请求头字段

    如上图所示 输出的i就是获取的头字段的值 (仅自己记录)

  5. 【Unity Shader】(八) ------ 高级纹理之立方体纹理及光线反射、折射的实现

    笔者使用的是 Unity 2018.2.0f2 + VS2017,建议读者使用与 Unity 2018 相近的版本,避免一些因为版本不一致而出现的问题.    [Unity Shader](三) -- ...

  6. Python获取每一位的数字,并返回到列表

    通过计算 def calc(value): result = [] while value: result.append(value % 10) value = value // 10 #逆序,按正常 ...

  7. 大牛都是这样写测试用例的,你get到了嘛?

    1. 用于语句覆盖的基路径法 基路径法保证设计出的测试用例,使程序的每一个可执行语句至少执行一次,即实现语句覆盖.基路径法是理论与应用脱节的典型,基本上没有应用价值,读者稍作了解即可,不必理解和掌握. ...

  8. 记一次线上gc调优的过程

           近期公司运营同学经常表示线上我们一个后台管理系统运行特别慢,而且经常出现504超时的情况.对于这种情况我们本能的认为可能是代码有性能问题,可能有死循环或者是数据库调用次数过多导致接口运行 ...

  9. C# Js 时间格式化问题

    C# 后台: .ToString("dd-MMM-yyyy", System.Globalization. DateTimeFormatInfo.InvariantInfo) eg ...

  10. 求int型数组和最大子数组 续

    之前的博文里已经实现过该程序的构思.编译.运行,本次就不再重复与之相雷同的内容. 题目:与别人借组,借助求int型数组最大和子数组的问题,考虑大数溢出和int取值范围的问题 要求: 调试程序  当子数 ...