show | version | enable_checker |
---|---|---|
step |
1.0 |
true |
- 上次
- 从 设置中文文本 字体开始
- 观察到了 docx的 结构
- docx
- 本质上是一个zip压缩包
- 可以 解压出 相关文件夹和文件
文件 | 作用 |
---|---|
word/document.xml | 文档内容 |
word/styles.xml | 负责样式 |
- 我们
- 可以解压docx
- 也可以编辑xml文件
- 然后压缩进入docx
- 目前学习到的样式
- 都是 以字体为基础的
- 能否 有以段落为基础的样式 呢?
- 段落样式
- 应该 算是段落的 一种属性
- 段落 都有啥属性
- 去游乐场试试
from docx import Document
doc = Document()
paragraph = doc.add_paragraph("oeasy")
paragraph.
- 观察到有个
- alignment
- 对齐
- 在文档搜索alignment
- 发现alignment的值是
- WD_ALGIN_PARAGRAPH.RIGHT
- 这个东西怎么来的呢?
- 继续搜索
- 看起来是一个
- enum
- 枚举类型
- 可以遍历 着个枚举类型吗?
for align in WD_ALIGN_PARAGRAPH:
print(align)
- 输出对齐方式
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
doc = Document()
paragraph = doc.add_paragraph("oeasy")
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
doc.save("oeasy.docx")
- 结果
- 实现居中对齐
- 除了 居中对齐 之外的
- 段前、段后
- 段左、段右
- 行间距呢?
- 在软件中
- 找到 Paragraph对话框
- 找到Indents & Spacing选项卡
- 可以观察到
- 各种段落 属性
- 段前、段后
- 段左、段右
- 行间距呢?
from docx import Document
doc = Document()
paragraph = doc.add_paragraph("oeasy")
paragraph.paragraph_format.
- 查看对象内容
- 行内距有两种
- 一种是 具体的数值
- 另一种是 行高(文字大小) 的倍数
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
doc = Document()
paragraph = doc.add_paragraph("Line spacing can be specified either as a specific length or as a multiple of the line height (font size). Line spacing is specified by the combination of values in w:spacing/@w:line and w:spacing/@w:lineRule. The ParagraphFormat.line_spacing property determines which method to use based on whether the assigned value is an instance of Length.")
paragraph.paragraph_format.line_spacing = 2
print("line_spacing: ",paragraph.paragraph_format.line_spacing)
print("line_spacing_rule: ",paragraph.paragraph_format.line_spacing_rule)
doc.save("oeasy.docx")
- 执行结果
- 规则为 DOUBLE(2)
- 多倍行距
- 观察效果
- 观察 属性
- 段前距 space_before
- 段后距 space_after
- 具体怎么设置呢?
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
doc = Document()
paragraph1 = doc.add_paragraph("Line spacing can be specified either as a specific length or as a multiple of the line height (font size). Line spacing is specified by the combination of values in w:spacing/@w:line and w:spacing/@w:lineRule. The ParagraphFormat.line_spacing property determines which method to use based on whether the assigned value is an instance of Length.")
paragraph2 = doc.add_paragraph("Spacing between subsequent paragraphs is controlled by the paragraph spacing attributes. Spacing can be applied either before the paragraph, after it, or both. The concept is similar to that of padding or margin in CSS. WordprocessingML supports paragraph spacing specified as either a length value or as a multiple of the line height; however only a length value is supported via the Word UI. Inter-paragraph spacing “overlaps”, such that the rendered spacing between two paragraphs is the maximum of the space after the first paragraph and the space before the second.")
paragraph1.paragraph_format.line_spacing = 1
paragraph1.paragraph_format.space_before = Pt(12)
paragraph1.paragraph_format.space_after = Pt(24)
paragraph2.paragraph_format.line_spacing = 1
paragraph2.paragraph_format.space_before = Pt(12)
paragraph2.paragraph_format.space_after = Pt(24)
doc.save("oeasy.docx")
- 效果
- 第一段的段后
- 和 第二段 怎么直接挨上了呢?
- 段落中的距离
- 会产生 塌陷
- 第一二段间的距离 比较
- 第一段 的段后
- 第二段 的段前
- 找到 其中 较大者
- Pt(24)
- 段左、段右 是如何设计的呢?
- 准备编写代码
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
doc = Document()
paragraph1 = doc.add_paragraph("Line spacing can be specified either as a specific length or as a multiple of the line height (font size). Line spacing is specified by the combination of values in w:spacing/@w:line and w:spacing/@w:lineRule. The ParagraphFormat.line_spacing property determines which method to use based on whether the assigned value is an instance of Length.")
paragraph2 = doc.add_paragraph("Spacing between subsequent paragraphs is controlled by the paragraph spacing attributes. Spacing can be applied either before the paragraph, after it, or both. The concept is similar to that of padding or margin in CSS. WordprocessingML supports paragraph spacing specified as either a length value or as a multiple of the line height; however only a length value is supported via the Word UI. Inter-paragraph spacing “overlaps”, such that the rendered spacing between two paragraphs is the maximum of the space after the first paragraph and the space before the second.")
paragraph1.paragraph_format.line_spacing = 1
paragraph1.paragraph_format.space_before = Pt(12)
paragraph1.paragraph_format.space_after = Pt(24)
paragraph1.paragraph_format.left_indent = Pt(36)
paragraph1.paragraph_format.right_indent = Pt(48)
doc.save("oeasy.docx")
- 段落四面都有了距离
- First line 指的是
- 首行缩进吗?
- 设置
- 首行缩进
- First Line Indent
- 两字符(2ch)
- 这个数值 读出来会是多少呢?
from docx import Document
from docx.shared import Pt
doc = Document("oeasy.docx")
paragraph = doc.paragraphs[0]
print(paragraph.paragraph_format.first_line_indent)
- 读取到 2字符 大概对应 Pt(21)
- 如果想要用程序设置呢?
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
doc = Document()
paragraph1 = doc.add_paragraph("Line spacing can be specified either as a specific length or as a multiple of the line height (font size). Line spacing is specified by the combination of values in w:spacing/@w:line and w:spacing/@w:lineRule. The ParagraphFormat.line_spacing property determines which method to use based on whether the assigned value is an instance of Length.")
paragraph2 = doc.add_paragraph("Spacing between subsequent paragraphs is controlled by the paragraph spacing attributes. Spacing can be applied either before the paragraph, after it, or both. The concept is similar to that of padding or margin in CSS. WordprocessingML supports paragraph spacing specified as either a length value or as a multiple of the line height; however only a length value is supported via the Word UI. Inter-paragraph spacing “overlaps”, such that the rendered spacing between two paragraphs is the maximum of the space after the first paragraph and the space before the second.")
paragraph1.paragraph_format.line_spacing = 1
paragraph1.paragraph_format.space_before = Pt(12)
paragraph1.paragraph_format.space_after = Pt(24)
paragraph1.paragraph_format.left_indent = Pt(36)
paragraph1.paragraph_format.right_indent = Pt(48)
paragraph1.paragraph_format.first_line_indent = Pt(-21)
doc.save("oeasy.docx")
- 悬挂缩进 2字符
- 首行缩进 什么时候开始有的呢?
- 古代简牍年代
- 为很早的首行缩进
- 控制 文章的阅读习惯
- 今天的 python
- 需要缩进
- 控制逻辑借口
- 还有一些属性是干什么用的呢?
- 段落控制
- 这些可以设置为段落的样式吗?
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.style import WD_STYLE_TYPE
doc = Document()
paragraph1 = doc.add_paragraph("Line spacing can be specified either as a specific length or as a multiple of the line height (font size). Line spacing is specified by the combination of values in w:spacing/@w:line and w:spacing/@w:lineRule. The ParagraphFormat.line_spacing property determines which method to use based on whether the assigned value is an instance of Length.")
paragraph2 = doc.add_paragraph("Spacing between subsequent paragraphs is controlled by the paragraph spacing attributes. Spacing can be applied either before the paragraph, after it, or both. The concept is similar to that of padding or margin in CSS. WordprocessingML supports paragraph spacing specified as either a length value or as a multiple of the line height; however only a length value is supported via the Word UI. Inter-paragraph spacing “overlaps”, such that the rendered spacing between two paragraphs is the maximum of the space after the first paragraph and the space before the second.")
styles = doc.styles
style = styles.add_style('oeasyp', WD_STYLE_TYPE.PARAGRAPH)
style.paragraph_format.line_spacing = 1
style.paragraph_format.space_before = Pt(12)
style.paragraph_format.space_after = Pt(24)
style.paragraph_format.left_indent = Pt(36)
style.paragraph_format.right_indent = Pt(48)
style.paragraph_format.first_line_indent = Pt(21)
paragraph1.style = style
paragraph2.style = style
doc.save("oeasy.docx")
- 打开效果
-
两个段落
- 确实 都已经
- 按照oeasyp样式设置了
-
如果同时有
- 段落格式
- 段落样式
- 会如何呢?
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.style import WD_STYLE_TYPE
doc = Document()
paragraph1 = doc.add_paragraph("Line spacing can be specified either as a specific length or as a multiple of the line height (font size). Line spacing is specified by the combination of values in w:spacing/@w:line and w:spacing/@w:lineRule. The ParagraphFormat.line_spacing property determines which method to use based on whether the assigned value is an instance of Length.")
paragraph2 = doc.add_paragraph("Spacing between subsequent paragraphs is controlled by the paragraph spacing attributes. Spacing can be applied either before the paragraph, after it, or both. The concept is similar to that of padding or margin in CSS. WordprocessingML supports paragraph spacing specified as either a length value or as a multiple of the line height; however only a length value is supported via the Word UI. Inter-paragraph spacing “overlaps”, such that the rendered spacing between two paragraphs is the maximum of the space after the first paragraph and the space before the second.")
styles = doc.styles
style = styles.add_style('oeasyp', WD_STYLE_TYPE.PARAGRAPH)
style.paragraph_format.line_spacing = 1
style.paragraph_format.space_before = Pt(12)
style.paragraph_format.space_after = Pt(24)
style.paragraph_format.left_indent = Pt(36)
style.paragraph_format.right_indent = Pt(48)
style.paragraph_format.first_line_indent = Pt(21)
paragraph1.style = style
paragraph1.paragraph_format.left_indent = Pt(0)
paragraph2.style = style
doc.save("oeasy.docx")
-
和 文字格式样式一样
- 段落 格式 会
- 覆盖 段落样式
-
想解压后看看
- document.xml
- style.xml
- 可以吗?
yes | unzip -d oeasyp oeasy.docx
cd oeasyp/word
firefox styles.xml document.xml
- 查看效果
- 确实新建了 oeasyp 这个style
- style 中包含
- 段前段后
- 段左段右
- 首行缩进
- 行高
- 两段落 确实应用了
- oeasyp样式
- 第一段中 设置了
- 段落 格式
- 段左距离为0
- 段落 和 文字一样
- 具体格式 会覆盖
- 样式
- 文档最后的东西是什么?
- 好像也是个上下左右
- 这次我们了解了
- 段落的格式和样式
- 段落的格式 属性
- 段前 段后
- 段左 段右
- 行间距
- 首行缩进
- ...
- 段落的格式 会覆盖 段落的样式
- document.xml中最后的东西 是什么?
- 好像 和纸张有关
- 具体怎么玩的呢?
- 我们下次再说!👋🏻