分享

编程语言Python 操作 Word

 冒险的K 2021-11-30

Python 操作 Word

用 docx 模块读取 Word

docx 安装

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

docx 常用函数

创建空白文档

from docx import Document

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

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

读取文档

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.paragraphsprint(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 Documentfrom 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 = Truerun1.font.underline = Truerun1.font.italic = True# # 三、保存文件document.save("word.docx")all_paragraphs = document.paragraphs# print(type(all_paragraphs))#,打印后发现是列表# 是列表就开始循环读取dfor 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 Documentfrom 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 = Truerun1.font.underline = Truerun1.font.italic = True# # 三、保存文件document.save("word.docx")all_paragraphs = document.paragraphs# print(type(all_paragraphs))#,打印后发现是列表# 是列表就开始循环读取dfor paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的样式名称
    # 打印每一个段落的文字
    print(paragraph.text)
    # 循环读取每个段落里的run内容
    # for run in paragraph.runs:
    # print(run.text)  # 打印run内容

在这里插入图片描述

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多