意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

用Python怎样对word实现编辑简单操作

来源:恒创科技 编辑:恒创科技编辑部
2024-01-01 23:04:59
今天小编跟大家讲解下有关“用Python怎样对word实现编辑简单操作”的内容 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了相关资料,希望小伙伴们看了有所帮助。



用Python怎样对word实现编辑简单操作


1 前言

在工作中时常会有繁重的文案工作,接触了python 之后,就会觉得这个比较简单了,python 操作word 和 excel 是比较常用的操作,相对比较简单,在本文中,我们就以 python 操作 word 为例来介绍一些简单的操作。

2 前提准备

2.1 python-docx 的安装

需要操作的前提是下载 docx 相关的操作类库python-docx,操作的环境和 IDE 环境如下所示

#使用的python 版本 python3.7.6 IDE pycharm2019  
# 安装命令 
pip install python-docx 
# 查看安装版本 
pip list | grep python-docx

2.2 docx 文档的结构说明

事先声明一下,python 操作的word版本必须是docx的版本,doc的文档暂不支持。另外 docx 文档也是一种 xml 的数据组织格式, 首先了解一下其格式情况,

在word文档中,其主要结构如下所述:

1 每个document包含多个paragraph,每个paragraph有多个run,每个run包含有(text文本,font字体,color颜色,字号) 2 每个document包含多个tables,table中有多个rows,每个row包含多个cells,每个cell中包含多个paragraph。对于写word表格不论是 head 还是paragraph 基本操作都是先添加对象,然后再添加run就好了 3 word表格的结构包含head标题、normal 正文、Caption表

3 具体使用

3.1 创建标题

# 创建一个document
document = Document()
# 创建一个标题 默认是一级标题
head = document.add_heading(level=4)
run = head.add_run("这是一个四级标题 this is a title")
# font.name 只能设置西文字体
run.font.name = 'Times New Roman'
# 中文字体需要使用这种方式设置
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
# 设置大小为11磅
run.font.size = Pt(16)
# 段落字体颜色
run.font.color.rgb = RGBColor(128, 0, 128)
# 是否加粗
run.bold = False
# 是否斜体
run.italic = False

3.2 创建段落

# 创建一个段落
ph = document.add_paragraph()
# 添加段落 段落间距段落前13磅 段落后13磅 行间距固定值18磅
ph.paragraph_format.space_before = Pt(13)
ph.paragraph_format.space_after = Pt(13)
ph.paragraph_format.line_spacing = Pt(18)
# 设置2.5倍行间距
ph.paragraph_format.line_spacing = 2.5
# 段落缩进 段落左缩进0.5英寸 left_indent right_indent
# p.paragraph_format.left_indent = Inches(0.5)
# 首行缩进 首行缩进0.9cm
ph.paragraph_format.first_line_indent = Cm(0.9)
# 段落左对齐
ph.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
run1 = ph.add_run("历史上第一个儿子当皇帝,老爹还活着的,当属刘太公,也就是刘邦的父亲。刘邦建立汉朝,称帝,"
                  "每天还去拜见刘太公,后来有大臣进言讲,虽然刘太公贵为皇帝父亲,但也为人臣,不应该由皇帝前去拜见。")
run1.font.size = Pt(12)
run1.font.color.rgb = RGBColor(128, 128, 128)
run1.font.name = 'Times New Roman'
run1._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')

3.3 创建表格

# 创建一个表格 3行四列 也可以不设置
table = document.add_table(rows=1, cols=3)
# 自动调整表格
table.autofit = True
# 设置表格样式
table.style = 'Table Grid'
# 表头
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
# 准备数据
records = (
    (3, '101', 'Spam'),
    (7, '422', 'Eggs'),
    (4, '631', 'Spam, spam, eggs, and spam')
)
# 添加内容
for qty, id, desc in records:
    row_cells = table.add_row().cells
    row_cells[0].text = str(qty)
    row_cells[1].text = id
    row_cells[2].text = desc

3.4 文档保存

# 保存文档 指定保存位置
document.save(r"demo_word.docx")

3.5 获取文档操作

#获取文档中所有段落的样式根据样式进行修改文档
docu = Document(r'D:/xxx.docx')
for p in docu.paragraphs:
    style_name = p.style.name
    print(style_name)
#获取文档中所有的表格
for tb in docu.tables:
    # tb.rows 文档中所有的行 tb.rows[0].cells 某一行的所有单元格
    # 循环单元格进行编辑样式操作

3.6 其它操作

# word表格单元格背景颜色
def set_cell_background_color(cell, color):
    # print(colorStr)
    shading_elm_1 = parse_xml(r'<w:shd {} w:fill="{color_value}"/>'.format(nsdecls('w'), color_value=color))
    cell._tc.get_or_add_tcPr().append(shading_elm_1)
    cells1[i].paragraphs[0].style = "表格体"
# 修改背景颜色为白色
set_cell_background_color(rows.cells[0], "#FFFFFF")

# 查看文档内所有的样式
for sts in document.styles:
    print(sts)
# 查看word文档结构
print(document._element.xml)

4 总结

最终产生的效果如下图所示:

在本章中,介绍了怎么使用python-docx创建wor文档,并举例说明了创建段落,表格,标题,图片等要点。


以上就是关于“用Python怎样对word实现编辑简单操作”的介绍了,感谢各位的阅读,如果大家想要了解更多相关的内容,欢迎关注恒创科技,小编每天都会为大家更新不同的知识。
上一篇: 什么是VGG16模型,复现及预测怎样实现 下一篇: Python图像腐蚀和膨胀处理怎么实现