pu369com

python操作word

1、 先要安装第三方库 python-docx。(处理doc和docx,但是doc易出问题)

pip install python-docx

2、生成一个word

#coding=utf-8

from docx import Document
Doc = Document()

Doc.save("myword.doc")

3、段落文本(字体、字号)、表格、图片

 

#coding=utf-8

from docx import Document
from docx.shared import Pt,Cm,Inches
from docx.oxml.ns import qn
from docx.enum.style  import WD_STYLE_TYPE
from docx.enum.text import WD_LINE_SPACING
import webbrowser

#文档对象
Doc = Document()
#添加一个中文段落
Doc.add_paragraph(u'这一行就当是标题吧!')
#再添加一个中文段落
paragraph = Doc.add_paragraph(u'这里算是第一个段落了1111111111段落1')
#paragraph中有runs,在第一段末尾增加一个run,内容为“设置字号”
run = paragraph.add_run(u'设置字号、')
run.font.size = Pt(24)
#设置中文字体
run = paragraph.add_run(u'设置中文字体、')
run.font.name=u'宋体'
r = run._element
r.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
Doc.add_paragraph(u'我们在做文本测试1111111111111111111111111111111!')
#插入表格
table = Doc.add_table(rows=2, cols=3)
# 设置表格样式
table.style = 'Light List Accent 1'
# 设置标题
title_cells = table.rows[0].cells
for i in range(3):
    title_cells[i].text = "titles" + str(i)
# 设置内容
for d in range(2):
    row_cells = table.add_row().cells
    for i in range(3):
        row_cells[i].text = "data"+str(i)
Doc.add_page_break()
#增加图像(此处用到图像image.bmp,请自行添加脚本所在目录中)
Doc.add_picture('image.bmp', width=Inches(1.25))
Doc.save("demo.docx")

#=============以下读取===========================
Doc = Document('demo.docx')  #打开文件demo.docx
#读取每段资料
l = [paragraph.text  for paragraph in Doc.paragraphs];
#输出并观察结果,也可以通过其他手段处理文本即可
for i in l:
    #print(l)
    print("=======")
#再打印一次第一段
#print(l[1])
#=============以下修改===========================
#将每段中的每个run中的第1个字体替换为字母A;(可以看到A的格式与run一致)
for paragraph in Doc.paragraphs:
    runlist = paragraph.runs
    for i in range(len(runlist)):
        run =runlist[i]
        if run.text:
            rlen=len(run.text)
            if rlen > 1:#不是分页符或其它
                print(rlen)
                print(run.text)
                newtxt=run.text[1:rlen]
                run.text="A"+newtxt        

#=============以下将每段后增加一个run,内容为“天”===========================
for paragraph in Doc.paragraphs:
    if len(paragraph.runs[0].text)>1:#不是分页符或其它
        run = paragraph.add_run(u'')
        run.font.name=u'仿宋'
        r = run._element
        r.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋')
#显示所有段落  
for i, p in enumerate(Doc.paragraphs):
    print(str(i) + ": " + str(p.text))
#在文档第一个paragraphs的最前加个“地”    
Doc.paragraphs[0].text = "" + Doc.paragraphs[0].text
#在文档开头增加一个paragraphs
Doc.paragraphs[0].insert_paragraph_before(u"新插入的段落")
#在第一段后加个“人”,并设置字体
obj_styles = Doc.styles
obj_charstyle = obj_styles.add_style('CommentsStyle', WD_STYLE_TYPE.CHARACTER)
obj_font = obj_charstyle.font
obj_font.size = Pt(10)
obj_font.name = 'Times New Roman'
Doc.paragraphs[0].add_run("", style = 'CommentsStyle').bold = True
# 在第一个表格的指定的单元格(第一行第一列)插入图片
run = Doc.tables[0].cell(0, 0).paragraphs[0].add_run()
run.add_picture('image.bmp', width=Inches(2))

#下面主要演示修改格式====================
#对齐
Doc.paragraphs[0].paragraph_format.left_indent = Cm(5)
Doc.paragraphs[1].paragraph_format.first_line_indent = Cm(2)
#行高
Doc.paragraphs[1].paragraph_format.line_spacing_rule=WD_LINE_SPACING.AT_LEAST
Doc.paragraphs[1].paragraph_format.line_spacing = Pt(88)

Doc.save("demo.docx")
webbrowser.open("demo.docx")

 

 

 

 

 

 

 

参考:https://blog.csdn.net/wcg541/article/details/100999756

https://zhuanlan.zhihu.com/p/98892274

https://blog.csdn.net/qianchenglenger/article/details/51582005

https://www.cnblogs.com/geek-arking/p/9300617.html

python-docx官方文档地址  https://python-docx.readthedocs.io/en/latest/index.html

https://blog.csdn.net/hu_zhenghui/article/details/77431018

http://www.voidcn.com/article/p-qppgqfei-btg.html

https://blog.csdn.net/shiyuzuxiaqianli/article/details/100147897

 

posted on 2020-04-08 20:29  pu369com  阅读(266)  评论(0编辑  收藏  举报

导航