【笔记】Python3操作docx
source link: https://loli.fj.cn/2023/06/08/Python3%E6%93%8D%E4%BD%9Cdocx/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Python3通过python-docx
报错操作docx实现办公自动化
1英寸=2.54厘米=72英镑=914400英国公制
pip3 install python-docx
from docx import Document
创建一个新的docx文档
document = Document()
打开一个已有的docx文档
<filename>
:指定已存在的文件名
document = Document("<filename>.docx")
保存docx文档
<filename>
:指定保存时的文件名
document.save("<filename>.docx")
添加一个段落
- 每次添加一个段落都会另起一行
<text>
:段落内容
paragraph = document.add_paragraph("<text>")
在指定段落之前添加一个段落
paragraph = paragraph.insert_paragraph_before("<text>")
添加一个标题
- 每次添加一个标题都会另起一行
<text>
:标题内容<num>
:标题等级,默认为1
0
、1
、2
、3
document.add_heading("<text>", level=<num>)
添加一个文本块
- 文本块需要追加到段落末尾
- 文本块的末尾默认不自动换行
<text>
:文本块内容
paragraph.add_run("<text>")
末尾添加换行符
- 通过
add_break()
函数可以在文本块末尾添加换行符
paragraph.add_run("<text>").add_break()
添加一个图片
- 每次添加一个图片都会另起一行
<src>
:图片文件的路径
document.add_picture("<src>")
width=<num>
:指定图片宽度height=<num>
:指定图片高度
from docx import Cm
document.add_picture("<src>", width=Cm(<num>), height=Cm(<num>))
添加一个表格
- 每次添加一个表格都另起一行
table = document.add_table(rows=<num>, cols=<num>)
向表格中追加二位列表
list = [
["", "", ""],
["", "", ""],
["", "", ""]
]
for i in range(len(list)):
cells = table.rows[i].cells
for j in range(len(list[0])):
cells[j].text = list[i][j]
生成所有表格样式
from docx import Document
from docx.enum.style import WD_STYLE_TYPE
document = Document()
styles = document.styles
for style in styles:
if style.type == WD_STYLE_TYPE.TABLE:
print(style.name)
document.add_paragraph(style.name)
table = document.add_table(rows=3, cols=3, style=style)
document.save("styles.docx")
段落的对齐方式
获取段落的对齐方式
paragraph.alignment
paragraph.paragraph_format.alignment
设置段落的对齐方式
from docx.enum.text import WD_PARAGRATH_ALIGNMENT
paragraph.alignment = WD_PARAGRATH_ALIGNMENT.CENTER
from docx.enum.text import WD_PARAGRATH_ALIGNMENT
paragraph.alignment = WD_PARAGRATH_ALIGNMENT.LEFT
from docx.enum.text import WD_PARAGRATH_ALIGNMENT
paragraph.alignment = WD_PARAGRATH_ALIGNMENT.RIGHT
from docx.enum.text import WD_PARAGRATH_ALIGNMENT
paragraph.alignment = WD_PARAGRATH_ALIGNMENT.JUSTIFY
段落的缩进
获取段落的缩进
paragraph.paragraph_format.left_indent
设置段落的缩进
from docx.shared import Inches
paragraph.paragraph_format.left_indent = Inches(<num>)
from docx.shared import Pt
paragraph.paragraph_format.left_indent = Pt(<num>)
段落的间距
设置段落前的间距
from docx.shared import Pt
paragraph.paragraph_format.space_before = Pt(<num>)
设置段落后的间距
from docx.shared import Pt
paragraph.paragraph_format.space_after = Pt(<num>)
段落的行间距
获取段落的行间距
paragraph.paragraph_format.line_spacing
设置段落的行间距
from docx.shared import Pt
paragraph.paragraph_format.line_spacing = Pt(<num>)
文字块的字体格式
设置文字块的字体颜色
手动指定RGB颜色值
<num_r>
:红色值,可以使用十进制数,也可以使用十六进制数<num_g>
:绿色值,可以使用十进制数,也可以使用十六进制数<num_b>
:蓝色值,可以使用十进制数,也可以使用十六进制数
from docx.shared import RGBColor
run = paragraph.add_run("<text>")
font = run.font
font.color.rgb = RGBColor(<num_r>, <num_g>, <num_b>)
使用预设的颜色值
from docx.enum.dml import MSO_THEME_COLOR_INDEX
run = paragraph.add_run("<text>")
font = run.font
font.color.theme_color = MSO_THEME_COLOR_INDEX.NOT_THEME_COLOR
font.color.theme_color = MSO_THEME_COLOR_INDEX.ACCENT_1
font.color.theme_color = MSO_THEME_COLOR_INDEX.ACCENT_2
font.color.theme_color = MSO_THEME_COLOR_INDEX.ACCENT_3
设置文字的字号
from docx.shared import Pt
run = paragraph.add_run("<text>")
font = run.font
font.size = Pt(<num>)
设置文字倾斜
run = paragraph.add_run("<text>")
font = run.font
font.italic = True
设置文字加粗
run = paragraph.add_run("<text>")
font = run.font
font.bold = True
设置文字下划线
run = paragraph.add_run("<text>")
font = run.font
font.underline = True
使用点状线
from docx.enum.text import WD_UNDERLINE
run = paragraph.add_run("<text>")
font = run.font
font.underline = WD_UNDERLINE.NONE
font.underline = WD_UNDERLINE.SINGLE
font.underline = WD_UNDERLINE.WORDS
font.underline = WD_UNDERLINE.DOUBLE
font.underline = WD_UNDERLINE.DOTTED
font.underline = WD_UNDERLINE.THICK
font.underline = WD_UNDERLINE.DASH
font.underline = WD_UNDERLINE.DOT_DASH
font.underline = WD_UNDERLINE.DOT_DOT_DASH
设置文字字体
run = paragraph.add_run("<text>")
font = run.font
font.name = "宋体"
只设置中文字体
run = paragraph.add_run("<text>")
font = run.font
font._element.rPr.rFonts.set(qn("w:eastAsia"), "宋体")
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK