新闻动态

Python实现Word的读写改操作

发布日期:2021-12-11 00:06 | 文章来源:源码之家

用 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))
# <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内容

到此这篇关于Python实现Word的读写改操作的文章就介绍到这了,更多相关Python的内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。

相关文章

实时开通

自选配置、实时开通

免备案

全球线路精选!

全天候客户服务

7x24全年不间断在线

专属顾问服务

1对1客户咨询顾问

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部