Skip to content

Latest commit

 

History

History
278 lines (207 loc) · 5.56 KB

File metadata and controls

278 lines (207 loc) · 5.56 KB
Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 3 column 1
---
- oeasy Python 0490
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
本教程同步发布在: 

     个人网站: `https://oeasy.org` 
     蓝桥云课: `https://www.lanqiao.cn/courses/3584` 
     GitHub: `https://github.com/overmind1980/oeasy-python-tutorial` 
     Gitee: `https://gitee.com/overmind1980/oeasypython` 
---

表格的细节

回忆

  • 上次我们研究了表格
    • 从零开始 生成表格
    • 在python中读取表格
  • 表格还可以怎么玩呢?

插入表格

from docx import Document
document = Document()
table = document.add_table(2,2)
for i in range(2):
    for j in range(2):
        table.cell(i,j).text = str(i+1) + "*" + str(j+1) + "=" + str((i+1)*(j+1))
document.save("o.docx")
  • 结果

图片描述

  • 想要深入了解细节

观察包

pip3 show python-docx

图片描述

  • 找到源码目录位置

进入源码目录

cd  /home/shiyanlou/.local/lib/python3.8/site-packages
  • 观察

图片描述

  • 找到table.py
vi table.py
  • 相关函数、细节就在里面
  • 如何理解text

观察

  • /text(
    • 找到函数位置

图片描述

  • 原来text属性是
    • 将cell中所有的paragraph中的text
    • 撸到一起
  • 尝试在cell中
    • 插入paragraphs

插入段落

from docx import Document
document = Document()
table = document.add_table(2,2)
for i in range(2):
    for j in range(2):
        table.cell(i,j).text = str(i+1) + "*" + str(j+1) + "=" + str((i+1)*(j+1))
table.cell(0,0).add_paragraph("p1")
table.cell(0,0).add_paragraph("p2")
print(table.cell(0,0).text)
document.save("o.docx")
  • 结果

图片描述

  • 可以合并单元格吗?

查询文档

  • 在table.py中
    • /class
    • 找到所有的类

图片描述

  • 试试这个函数

merge

from docx import Document
document = Document()
table = document.add_table(2,2)
for i in range(2):
    for j in range(2):
        table.cell(i,j).text = str(i+1) + "*" + str(j+1) + "=" + str((i+1)*(j+1))
table.cell(0,0).merge(table.cell(0,1))
document.save("o.docx")
  • 合并后

图片描述

  • 尝试增加行

增加行

from docx import Document
document = Document()
table = document.add_table(2,2)
for i in range(2):
    for j in range(2):
        table.cell(i,j).text = str(i+1) + "*" + str(j+1) + "=" + str((i+1)*(j+1))
table.add_row()
table.cell(2,0).text = "new row"
document.save("o.docx")
  • 结果

图片描述

  • 想要新建一个列
    • column
    • 应该如何呢?

添加列

from docx import Document
from docx.shared import Mm
document = Document()
table = document.add_table(2,2)
for i in range(2):
    for j in range(2):
        table.cell(i,j).text = str(i+1) + "*" + str(j+1) + "=" + str((i+1)*(j+1))
table.add_column(Mm(50))
table.cell(0,2).text = "new column"
document.save("o.docx")
  • 结果

图片描述

  • 可以控制 已有所有列的宽度吗?

控制列宽

from docx import Document
from docx.shared import Mm
document = Document()
table = document.add_table(2,2)
for i in range(2):
    for j in range(2):
        table.cell(i,j).text = str(i+1) + "*" + str(j+1) + "=" + str((i+1)*(j+1))
        table.columns[j].width = Mm(50)
table.add_column(Mm(50))
table.cell(0,2).text = "new column"
document.save("o.docx")
  • 既然列可以设置宽度
  • 行可以控制高度吗?

行高控制

from docx import Document
from docx.shared import Mm
document = Document()
table = document.add_table(2,2)
for i in range(2):
    table.rows[i].height = Mm(10)
    for j in range(2):
        table.cell(i,j).text = str(i+1) + "*" + str(j+1) + "=" + str((i+1)*(j+1))
        table.columns[j].width = Mm(50)
table.add_column(Mm(50))
table.cell(0,2).text = "new column"
table.add_row()
table.rows[2].height = Mm(50)
table.cell(2,0).text = "new row"
document.save("o.docx")
  • 行高控制

图片描述

  • 尝试修改样式styles

修改样式

from docx import Document
document = Document()
table = document.add_table(9,9)
for i in range(9):
    for j in range(9):
        if i >= j:
            table.cell(i,j).text = str(i+1) + "*" + str(j+1) + "=" + str((i+1)*(j+1))
table.style = document.styles["Light Shading"]
document.save("o.docx")
  • 效果

图片描述

  • 可以把所有的样式列出来吗?

列出所有的样式

from docx import Document
document = Document()
print(document.styles)
for style in document.styles:
    print(style)
  • 结果

图片描述

  • 真的有很多样式啊!
  • 可以找到Light Shading这个样式吗?

找到样式

图片描述

  • 可以找到
    • 看起来像是一个
    • 表格样式
    • _TableStyle

总结🤔

  • 最后 制作了九九乘法表
    • 并且为表格设置了表格样式
  • 点击⚙️齿轮
    • 选择样式style

图片描述

  • 发现各种样式style很多
  • 样式style 到底怎么玩呢?
  • 我们下次再说!👋🏻

  • 本文来自 oeasy Python 系统教程。
  • 想完整、扎实学 Python,
  • 搜索 oeasy 即可。