首页 > 编程语言 >python-docx - 3

python-docx - 3

时间:2023-05-21 16:34:52浏览次数:36  
标签:docx Medium python Accent List style Grid document

1. 样式

1.1 访问样式

使用Document.styles属性访问样式。

from docx import Document
document = Document()
# 获取样式对象,这里面可以像字典一样访问,也可以迭代
styles = document.styles
for style in styles:
    print(style.name, "\t", style.type)

# 获取一个正文样式
print(styles['Normal'])

默认样式以英文方式存储,因此需要通过英文名称访问。

以下是默认样式的列表

Paragraph styles in default template

  • Normal
  • Body Text
  • Body Text 2
  • Body Text 3
  • Caption
  • Heading 1
  • Heading 2
  • Heading 3
  • Heading 4
  • Heading 5
  • Heading 6
  • Heading 7
  • Heading 8
  • Heading 9
  • Intense Quote
  • List
  • List 2
  • List 3
  • List Bullet
  • List Bullet 2
  • List Bullet 3
  • List Continue
  • List Continue 2
  • List Continue 3
  • List Number
  • List Number 2
  • List Number 3
  • List Paragraph
  • Macro Text
  • No Spacing
  • Quote
  • Subtitle
  • TOCHeading
  • Title

Character styles in default template

  • Body Text Char
  • Body Text 2 Char
  • Body Text 3 Char
  • Book Title
  • Default Paragraph Font
  • Emphasis
  • Heading 1 Char
  • Heading 2 Char
  • Heading 3 Char
  • Heading 4 Char
  • Heading 5 Char
  • Heading 6 Char
  • Heading 7 Char
  • Heading 8 Char
  • Heading 9 Char
  • Intense Emphasis
  • Intense Quote Char
  • Intense Reference
  • Macro Text Char
  • Quote Char
  • Strong
  • Subtitle Char
  • Subtle Emphasis
  • Subtle Reference
  • Title Char

Table styles in default template

  • Table Normal
  • Colorful Grid
  • Colorful Grid Accent 1
  • Colorful Grid Accent 2
  • Colorful Grid Accent 3
  • Colorful Grid Accent 4
  • Colorful Grid Accent 5
  • Colorful Grid Accent 6
  • Colorful List
  • Colorful List Accent 1
  • Colorful List Accent 2
  • Colorful List Accent 3
  • Colorful List Accent 4
  • Colorful List Accent 5
  • Colorful List Accent 6
  • Colorful Shading
  • Colorful Shading Accent 1
  • Colorful Shading Accent 2
  • Colorful Shading Accent 3
  • Colorful Shading Accent 4
  • Colorful Shading Accent 5
  • Colorful Shading Accent 6
  • Dark List
  • Dark List Accent 1
  • Dark List Accent 2
  • Dark List Accent 3
  • Dark List Accent 4
  • Dark List Accent 5
  • Dark List Accent 6
  • Light Grid
  • Light Grid Accent 1
  • Light Grid Accent 2
  • Light Grid Accent 3
  • Light Grid Accent 4
  • Light Grid Accent 5
  • Light Grid Accent 6
  • Light List
  • Light List Accent 1
  • Light List Accent 2
  • Light List Accent 3
  • Light List Accent 4
  • Light List Accent 5
  • Light List Accent 6
  • Light Shading
  • Light Shading Accent 1
  • Light Shading Accent 2
  • Light Shading Accent 3
  • Light Shading Accent 4
  • Light Shading Accent 5
  • Light Shading Accent 6
  • Medium Grid 1
  • Medium Grid 1 Accent 1
  • Medium Grid 1 Accent 2
  • Medium Grid 1 Accent 3
  • Medium Grid 1 Accent 4
  • Medium Grid 1 Accent 5
  • Medium Grid 1 Accent 6
  • Medium Grid 2
  • Medium Grid 2 Accent 1
  • Medium Grid 2 Accent 2
  • Medium Grid 2 Accent 3
  • Medium Grid 2 Accent 4
  • Medium Grid 2 Accent 5
  • Medium Grid 2 Accent 6
  • Medium Grid 3
  • Medium Grid 3 Accent 1
  • Medium Grid 3 Accent 2
  • Medium Grid 3 Accent 3
  • Medium Grid 3 Accent 4
  • Medium Grid 3 Accent 5
  • Medium Grid 3 Accent 6
  • Medium List 1
  • Medium List 1 Accent 1
  • Medium List 1 Accent 2
  • Medium List 1 Accent 3
  • Medium List 1 Accent 4
  • Medium List 1 Accent 5
  • Medium List 1 Accent 6
  • Medium List 2
  • Medium List 2 Accent 1
  • Medium List 2 Accent 2
  • Medium List 2 Accent 3
  • Medium List 2 Accent 4
  • Medium List 2 Accent 5
  • Medium List 2 Accent 6
  • Medium Shading 1
  • Medium Shading 1 Accent 1
  • Medium Shading 1 Accent 2
  • Medium Shading 1 Accent 3
  • Medium Shading 1 Accent 4
  • Medium Shading 1 Accent 5
  • Medium Shading 1 Accent 6
  • Medium Shading 2
  • Medium Shading 2 Accent 1
  • Medium Shading 2 Accent 2
  • Medium Shading 2 Accent 3
  • Medium Shading 2 Accent 4
  • Medium Shading 2 Accent 5
  • Medium Shading 2 Accent 6
  • Table Grid

1.2 应用样式

Paragraph, Run, Table对象都有一个style属性,赋值即可应用样式。

from docx import Document
document = Document()

# 添加时指定样式
document.add_paragraph("标题1", style="Heading 1")
# 直接通用字符串指定样式
p = document.add_paragraph("标题2")
p.style = "Heading 2"
# 通过字典的方式指定样式
p = document.add_paragraph("标题3")
p.style = document.styles['Heading 3']

document.save("1.docx")

1.3 添加和删除样式

1.3.1 添加样式

from docx import Document
from docx.enum.style import WD_STYLE_TYPE
from docx.shared import Pt

document = Document()
styles = document.styles
# 添加新样式。名字是:My_style, type是:PARAGRAPH
# type: CHARACTER, LIST, PARAGRAPH, TABLE
style = styles.add_style('My_style', style_type=WD_STYLE_TYPE.PARAGRAPH)
style.font.size = Pt(16)

document.add_paragraph("hello world", style='My_style')
document.save("1.docx")

1.3.2 删除样式

styles['My_style'].delete()

1.4 字符与段落样式【例】

from docx import Document
from docx.enum.style import WD_STYLE_TYPE
from docx.oxml.ns import qn
from docx.shared import Pt

text = '燕子去了,有再来的时候;杨柳枯了,有再青的时候;桃花谢了,有再开的时候。但是,聪明的,你告诉我,我们的日子为什么一去不复返呢?——是有人偷了他们罢:那是谁?又藏在何处呢?是他们自己逃走了罢:如今又到了哪里呢?'

document = Document()
styles = document.styles
# 添加样式
style = styles.add_style('My_style', style_type=WD_STYLE_TYPE.PARAGRAPH)
style.font.size = Pt(16)
style.font.name = "仿宋"
style.font.element.rPr.rFonts.set(qn('w:eastAsia'), style.font.name)
# 首先缩进
style.paragraph_format.first_line_indent = style.font.size * 2
# 1.5倍行距
style.paragraph_format.line_spacing = 1.5

document.add_paragraph(text, style='My_style')
document.save("1.docx")

style里面的font可以设置字符样式,paragraph_format可以设置段落样式。

2. 表格

2.1 添加表格

from docx import Document

document = Document()
# 添加5行3列的表格
table = document.add_table(5, 3, style="Table Grid")
document.save("1.docx")

2.2 添加行和列

from docx import Document
from docx.shared import Cm

document = Document()
table = document.add_table(rows=1, cols=1, style="Table Grid")
# 下面加一行
table.add_row()
# 右侧加一列,宽度3.5厘米
table.add_column(Cm(3.5))
document.save("1.docx")

2.3 行和列对象

结构:一个表格(Table),表格里有行(Row),列(Column),行或列里有单元格(Cell)

from docx import Document

document = Document()
table = document.add_table(rows=5, cols=4, style="Table Grid")
# 获取所有行
print(table.rows)
# 获取所有列
print(table.columns)
# 按行遍历单元格
for row in table.rows:
    for cell in row.cells:
        print(cell)
# 按列遍历单元格
for col in table.columns:
    for cell in col.cells:
        print(cell)
document.save("1.docx")

在行和列对象里,有一个table属性,指向所属表格对象。

from docx import Document
document = Document()
table = document.add_table(rows=5, cols=4, style="Table Grid")
print(table.rows[1].table)

2.3.1 行高和列宽

from docx import Document
from docx.enum.table import WD_ROW_HEIGHT_RULE
from docx.shared import Cm

document = Document()
table = document.add_table(rows=5, cols=4, style="Table Grid")
for i in range(len(table.rows)):
    if i == 2:
        # 指定行高
        table.rows[i].height = Cm(3)
    else:
        # 精确值
        table.rows[i].height_rule = WD_ROW_HEIGHT_RULE.EXACTLY
        table.rows[i].height = Cm(0.2)

document.save("1.docx")

WD_ROW_HEIGHT_RULE中可以指定:

  • AUTO 调整行高以适应行中的最高值。

  • AT_LEAST 行高至少是指定的最小值。

  • EXACTLY 行高是一个精确的值。

列宽我测试单独设置一个格不好使,所有单元格都设置才有用:

from docx.shared import Cm

document = Document()
table = document.add_table(rows=5, cols=4, style="Table Grid")
# 第3列
col = table.columns[2]
for c in col.cells:
    c.width = Cm(15)
document.save("1.docx")

2.4 单元格


document = Document()
table = document.add_table(rows=5, cols=4, style="Table Grid")
# 指定位置
cell = table.cell(0,0)
print(cell)

2.4.1 单元格文本

document = Document()
table = document.add_table(rows=5, cols=4, style="Table Grid")
# 样式
table.style.font.size = Pt(15)
table.style.font.color.rgb = RGBColor(0, 255, 255)
cell = table.cell(0, 0)
cell.text = "hello world"

cell: _Cell = table.cell(0, 1)
cell.text = "第二格"
# 垂直对齐
cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
# 水平对齐
cell.paragraphs[0].paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
document.save("1.docx")

通过text读取和写入,也可以获取cell中的段落,之后添加文本并修饰。

标签:docx,Medium,python,Accent,List,style,Grid,document
From: https://www.cnblogs.com/three-sheep/p/17418730.html

相关文章

  • Python使用pip安装第三方包
    ​ 参考文章:如何安装第三方的Python包?-知乎​pipinstall-i网址包名称例如:pipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simple/numpy常用的网址有:清华:https://pypi.tuna.tsinghua.edu.cn/simple/阿里云:http://mirrors.aliyun.com/pypi/simple/......
  • Python安装教程
    Python安装教程https://zhuanlan.zhihu.com/p/569019068python下载https://www.python.org/downloads/windows/pycharm下载https://www.jetbrains.com/pycharm/download/#section=windows配置https://zhuanlan.zhihu.com/p/587849846?utm_id=0......
  • python 云服务器部署 flask 项目
    测试模式,非生产模式1.修改host和port 2.上传项目 3.下载python项目管理器  4.创建项目 5.开放端口,远程连接数据库......
  • 深入理解 python 虚拟机:魔术方法之数学计算
    深入理解python虚拟机:魔术方法之数学计算在本篇文章当中主要给大家介绍在python当中一些常见的魔术方法,本篇文章主要是关于与数学计算相关的一些魔术方法,在很多科学计算的包当中都使用到了这些魔术方法。大小比较当我们在Python中定义自己的类时,可以通过重写一些特殊方法来......
  • Python多进程编程-进程间共享数据(Value、Array、Manager)
    转载:(14条消息)Python多进程编程-进程间共享数据(Value、Array、Manager)_managervalue_Loadinggggg的博客-CSDN博客Value、Array是通过共享内存的方式共享数据Manager是通过共享进程的方式共享数据。Value\Array实例代码:importmultiprocessing#Value/Arraydeffunc1(a,arr......
  • Python并发编程:为什么传入进程池的目标函数不执行,也没有报错?
    转载:Python并发编程:为什么传入进程池的目标函数不执行,也没有报错?-知乎(zhihu.com)python初学者使用进程池时,很容易掉坑里! python并发编程中,这个问题是新手经常容易犯的错,十个人,大概有九个都会掉入其中。借此机会,对该问题的前因后果做个记录,分享于此!一、错误代码复现我......
  • python 进程池multiprocessing.Pool
    转载:python进程池multiprocessing.Pool(44)-知乎(zhihu.com)python进程池Pool和前面讲解的python线程池类似,虽然使用多进程能提高效率,但是进程的创建会消耗大量的计算机资源(进程Process的创建远远大于线程Thread创建占用的资源),线程是计算机最小的运行单位,连线程都需要使用线程......
  • python基础-进程池、submit同异步调用、shutdown参数、ProcessPoolExecutor进程池、进
    转载:(14条消息)python基础-进程池、submit同异步调用、shutdown参数、ProcessPoolExecutor进程池、进程池ftp_pythonsubmit_易辰_的博客-CSDN博客引入进程池在学习线程池之前,我们先看一个例子frommultiprocessingimportProcessimporttimedeftask(name):print(......
  • Python3.8多进程之共享内存
    转载:Python3.8多进程之共享内存-知乎(zhihu.com)最近发了个宏愿想写一个做企业金融研究的Python框架。拖出Python一看已经更新到了3.8,于是就发现了Python3.8里新出现的模块:multiprocessing.shared_memory。随手写了个测试。生成一个240MB大小的pandas.DataFrame,然后转换成nu......
  • python进程池ProcessPoolExecutor的用法与实现分析
    转载:(14条消息)【Python随笔】python进程池ProcessPoolExecutor的用法与实现分析_utmhikari的博客-CSDN博客concurrent.futures—Launchingparalleltasks—Python3.11.3documentation在python开发期间,由于GIL的原因,不能直接采用并行的方式处理代码逻辑。在multiprocess......