栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 操作 Word

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python 操作 Word

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)

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

获取文档段落
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))
# ,打印后发现是列表
# 是列表就开始循环读取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))
# ,打印后发现是列表
# 是列表就开始循环读取d
for paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的样式名称
    # 打印每一个段落的文字
    print(paragraph.text)
    # 循环读取每个段落里的run内容
    # for run in paragraph.runs:
    # print(run.text)  # 打印run内容

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/580410.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号