当前位置:首页 » 《随便一记》 » 正文

Python 操作 Word_Gettler的博客

3 人参与  2022年06月01日 17:06  分类 : 《随便一记》  评论

点击全文阅读


Python 操作 Word

用 docx 模块读取 Word

docx 安装

cmd 中输入pip install python-docx 即可安装 docx 模块

docx 常用函数

创建空白文档

from docx import Document

document = Document()
document.save("word.docx")  # 生成空白 word
print(document)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Xo1siDkz-1637387517723)(python办公自动化.assets/image-20211119225443886.png)]

读取文档

from docx import Document
document = Document("word.docx")  # 读取现有的 word 建立文档对象

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dST0hckl-1637387517728)(python办公自动化.assets/image-20211119225308276.png)]

获取文档段落

from docx import Document

document = Document("word.docx")  # 读取现有的 word 建立文档对象
all_paragraphs = document.paragraphs
print(type(all_paragraphs))
for paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的样式名称
    # 打印每一个段落的文字
    print(paragraph.text)
    # 循环读取每个段落里的run内容
# 一个run对象是相同样式文本的延续
for paragraph in all_paragraphs:
    for run in paragraph.runs:
        print(run.text)  # 打印run内容

在这里插入图片描述

Word 调整样式

from docx import Document
from docx.shared import Pt, RGBColor

document = Document()  # 读取现有的 word 建立文档对象

# 二、写入内容
# 段落
p1 = document.add_paragraph("早睡早起!!!")
format_p1 = p1.paragraph_format
# 左右缩进
format_p1.left_indent = Pt(20)
format_p1.right_indent = Pt(20)
# 首行缩进
format_p1.first_line_indent = Pt(20)
# 行间距
format_p1.line_spacing = 1
# 追加
# 一个run对象是相同样式文本的延续
run = p1.add_run("我也想做舔狗\n")
# 字体,字号,文字颜色
run.font.size = Pt(12)
run.font.name = "微软雅黑"
run.font.color.rgb = RGBColor(235, 123, 10)
run1 = p1.add_run("贾某人不学习")
# 加粗,下划线,斜体
run1.bold = True
run1.font.underline = True
run1.font.italic = True
# # 三、保存文件
document.save("word.docx")

all_paragraphs = document.paragraphs
# print(type(all_paragraphs))
# <class 'list'>,打印后发现是列表
# 是列表就开始循环读取d
for paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的样式名称
    # 打印每一个段落的文字
    print(paragraph.text)
    # 循环读取每个段落里的run内容
    # for run in paragraph.runs:
    # print(run.text)  # 打印run内容

在这里插入图片描述

Word 写入操作

from docx import Document
from docx.shared import Pt, RGBColor

document = Document()  # 读取现有的 word 建立文档对象

# 二、写入内容
document.add_heading("python 操作 Word")
# 段落
p1 = document.add_paragraph("早睡早起!!!")
p1.insert_paragraph_before("Power!!!")
format_p1 = p1.paragraph_format
# 左右缩进
format_p1.left_indent = Pt(20)
format_p1.right_indent = Pt(20)
# 首行缩进
format_p1.first_line_indent = Pt(20)
# 行间距
format_p1.line_spacing = 1
# 追加
# 一个run对象是相同样式文本的延续

run = p1.add_run("我也想做舔狗\n")
# 字体,字号,文字颜色
run.font.size = Pt(12)
run.font.name = "微软雅黑"
run.font.color.rgb = RGBColor(235, 123, 10)
run1 = p1.add_run("贾某人不学习")
# 加粗,下划线,斜体
run1.bold = True
run1.font.underline = True
run1.font.italic = True
# # 三、保存文件
document.save("word.docx")

all_paragraphs = document.paragraphs
# print(type(all_paragraphs))
# <class 'list'>,打印后发现是列表
# 是列表就开始循环读取d
for paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的样式名称
    # 打印每一个段落的文字
    print(paragraph.text)
    # 循环读取每个段落里的run内容
    # for run in paragraph.runs:
    # print(run.text)  # 打印run内容

在这里插入图片描述


点击全文阅读


本文链接:http://www.zhangshiyu.com/post/41193.html

段落  读取  打印  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 终是庄周梦了蝶在线品鉴(南语沈聿珩乔清意)_终是庄周梦了蝶在线品鉴
  • 继承千亿资产后,金丝雀不装了:结局+番外新上热文(许清雾)最近章节列表
  • 完结文晚来沉香惊鸿语完结精选作品列表_完结文晚来沉香惊鸿语完结精选作品(沈落黎段砚深)
  • 完结文终是庄周梦了蝶后续+结局高质量好文列表_完结文终是庄周梦了蝶后续+结局高质量好文(南语沈聿珩)
  • 完结文晏临渊叶知薇谢执玉人气列表_完结文晏临渊叶知薇谢执玉人气
  • 嫁给坏男人:结局+番外免费品鉴(苏念周北冥)全书在线无广告高口碑小说
  • 温愉贺聿风免费在线_温愉贺聿风免费在线
  • 开挂辅助成团宠,全星际争当大冤种!结局+番外+完结白希薇沈星河完本_开挂辅助成团宠,全星际争当大冤种!结局+番外+完结(白希薇沈星河)
  • 多子多福?五旬老汉累断腰!大结局_张超柳三娘完整文本_小说后续在线阅读_无删减免费完结_
  • 好孕喜女重生嫁太子,把渣世子送进大内做太监高分神作_云州侯府子孙新书_小说后续在线阅读_无删减免费完结_
  • 我成为老赖后,请全公司狂欢的实习生慌了结局+番外榜单(陆暄江辞刘雯)全书免费_(陆暄江辞刘雯)我成为老赖后,请全公司狂欢的实习生慌了结局+番外榜单后续(陆暄江辞刘雯)
  • 等这最后七颗糖吃完,她就要彻底放下他了林舒寒江语静后续(林舒寒江语静)全篇在线阅读前传

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1