python docx库的简单使用

docx库的下载指令:

pip install python-docx

1、文档

创建文档

from docx import Document
document = Document()

保存文档

document.save('test.docx')

打开现有文档

document = Document('filename.docx')  # 打开
document.save('new-filename.docx')  #保存文档

如果使用相同的文件名来打开和保存文件,将默认覆盖原文件

打开file-like文档

f = open('a.docx', 'rb')
document = Document(f)
f.close()

or

with open('a.docx', 'rb') as f:
	source_stream = StringIO(f.read())
document = Document(source_stream)
source_stream.close()

target_stream = StringIO()
document.save(target_stream)

2、段落

添加段落

paragraph = document.add_paragraph("This is the first paragraph")

作用:在文档末尾添加一个新的段落,并返回该段落的引用。

prior_paragraph = paragraph.insert_paragraph_before("such a good boy")

作用:以某个段落为基准,在这个段落前添加一个新的段落。

添加段落风格

paragraph.style = "List Bullet"

这种风格使得段落看起来像子弹头。

在段落中添加内容

run = paragraph.add_run("it is wonderful")
run = paragraph.add_run("it is wonderful", "Emphasis")  # 创建时指定风格

这还将返回一个对字符串"it is wonderful"的引用,我们可以对这个字符串进行格式的设置。

run.bold = True  # 设置为粗体
run.italic = True  # 设置为斜体
run.style = "Emphasis" # 创建后设置风格

设置段落格式

段落的对齐方式

from docx.enum.text import WD_ALIGN_PARAGRAPH

paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER  # 设置为居中对齐

段落的缩进

paragraph.paragraph_format.left_indent = Inches(0.5)  # 向左缩进
paragraph.paragraph_format.right_indent = Inches(0.5)  # 向右缩进
paragraph.paragraph_format.first_line_indent = Inches(0.5)  #第一行缩进

段落间隔

from docx.shared import Pt

paragraph.paragraph_format.space_before = Pt(18)
paragraph.paragraph_format.space_after = Pt(12)

行间距

paragraph.paragraph_format.line_spacing = Pt(18)

分页属性:

keep_together:使整个段落出现在同一页上,如果该段落可能被分割成两页,则在该段之前加入分页符。

keep_with_next:保持与下一段保持一个段落在相同的页面。

page_break_before:使用分页符将段落放置在新页的顶部。

widow_control:避免将该段的第一行或最后一行放在与该段其余部分分开的页面上。

paragraph.paragraph_format.keep_together = True
paragraph.paragraph_format.keep_with_next = False

3、标题

添加标题

document.add_heading('My first doc created by python')

作用:添加一个顶级标题。

标题共有9级,可通过level属性来指定级数,如:

document.add_heading('My first doc created by python', level=3)

会创建一个3级标题。

若指定级别为0,则将创建一个段落

4、分页符

document.add_page_break()

作用:无论所在页面是否已满,都将转到一个新的页面上

5、表格

添加表格

table = document.add_table(rows=2, cols=2)

作用:创建一个2行,2列的表格(可视作一个二维数组)

获取引用

cell = table.cell(0,1)  # 获取第一行第二列的元素的引用
row = table.rows[1]  # 获取第二行的引用
cell = row.cells[1] #  获取第二行(即上一行代码所获得的行)的第二个元素

方法

cell.text = 'hello, good morning'  # 修改单元格的文本
row_count = len(table.rows)  # 统计表格行数
row = table.add_row()  # 添加新的一行
table.style = 'LightShading-Acccent1'  # 表格样式

6、图片

添加图片

document.add_picture('snow.jpg')

添加了一张文件名为"snow.jpg"的图片

控制图片大小

form docx.shared import Inches

document.add_picture('snow.jpg', width=Inches(1))

7、字体设置

from docx.shared import Pt
from docx.shared import RGBColor
from docx.enum.dml import MSO_THEME_COLOR


run = document.add_paragraph().add_run()
font = run.font
font.name = 'Calibri'  #字体种类
font.size = Pt(12)   #字体大小
font.bold = True  # 粗体
font.italic = False  # 斜体
font.underline = True  # 下划线
font.color.rgb = RGBColor(0x42, 0x24, 0xE9)  # 字体颜色
font.clor.theme_color = MSO_THEME_COLOR.ACCENT_1  # 设置配色
font.color.rgb = None  # 默认颜色

8、section属性

section的使用

sections = document.sections
section = sections[0]

section的属性

  • start_type:这个部分之前的断开类型

  • orientation:页面方向

  • page_width:页面宽度

  • page_height:页面高度

  • left_margin:左边距

  • right_margin:右边距

  • top_margin:上边距

  • bottom_margin:下边距

  • gutter

  • header_distance

  • footer_distance

section.start_type = WD_SECTION.ODD_PAGE
section.orientation = WD_ORIENT.LANDSCAPE
section.page_width = new_width
section.page_height = new_height
section.left_margin = Inches(1.5)
section.right_margin = Inches(1)

9、页眉和页脚

获取

section = document.sections[0]
header = section.header

使用

paragraph = header.paragraphs[0]
paragraph.text = "Title of my document"  # 在页眉添加内容
paragraph.text = "Left Text\tCenter Text\tRight Text"  #制表符用于分隔左、中和右对齐的标题内容
paragraph.style = document.styles['header']

删除

header.is_linked_to_previous = True
  • 14
    点赞
  • 94
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值