Python笔记:python-docx
python-docx是什么
一个自动化办公的神器,可以将打工人从重复又繁琐的文档资料编辑工作中解救出来。
可以实现文档资料的自动生成和批量编辑功能。
python-docx的官方文档
python-docx — python-docx 0.8.11 documentation
然而网站并没有中文,只有英文。
python-docx的安装
pip install python-docx
python-docx的使用
导入包
from docx import Document #从docx库中导入Document类 #Document,即文档
新建文档
my_docx = Document() #新建一个名为my_docx的文档类,即创建一个新的文档对象
打开已有的文档
open_docx = Document("我的文档.docx") #打开当前目录下的“我的文档.docx" open_docx = Document(".\test\我的文档.docx") #打开当前目录下test文件夹里的“我的文档.docx" #如需指定目录,在文档名前加绝对路径或相对路径即可
PS:经笔者测试,最新版本的python-docx不支持通过以下的形式创建新的文档,程序会报错
new_docx = Document("新的文档.docx")
PS:并且,Document()不支持打开文件大小为0的文档(也就是通过鼠标右键新建且没有打开编辑和操作过的的docx),程序也会报错。
保存文档
my_docx = Document() file_name = "我的文档.docx" my_docx.save(file_name) #将文档保存为”我的文档.docx“
添加段落与文本
new_docx = Docment() #添加段落 new_p = new_docx.add_paragraph("第一个段落") #添加文本 new_p.add_run("第一个文本的内容") new_p = new_docx.add_paragraph("第二个的段落") new_p.add_run("第二个文本的内容") new_p.add_run("第三个文本的内容") new_docx.save('我的文档.docx')
添加新的段落样式和文本样式
from docx import Document from docx.shared import Pt, Cm from docx.oxml.ns import qn new_docx = Document() #添加新的段落样式,name是样式名称,style_type是样式类型,1是段落样式,2是文本样式 new_paragraph_style = new_docx.styles.add_style(name="new_paragraph_style",style_type=1) #设置样式的字体 new_paragraph_style.font.name = "宋体" new_paragraph_style._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体') #设置样式的字体大小 new_paragraph_style.font.size = Pt(18) #添加新的文本样式 new_text_style = new_docx.styles.add_style("new_text_style",2) #设置样式的字体 new_text_style.font.name = "楷体" new_text_style._element.rPr.rFonts.set(qn('w:eastAsia'), '楷体') #设置样式的字体大小 new_text_style.font.size = Pt(14) #将第一个段落的样式设置为new_paragraph_style new_p = new_docx.add_paragraph("第一个段落",style=new_paragraph_style) #如果新增的文本没有被设置样式,则继承段落的样式 new_p.add_run("第一个文本的内容") new_p = new_docx.add_paragraph("第二个段落",style=new_paragraph_style) new_p.add_run("第二个文本的内容") #为新增的文本设置new_text_style样式 new_p.add_run("第三个文本的内容",style=new_text_style) new_docx.save('我的文档.docx')
PS:新增的样式只在当前的文档对象中有效
文档的常用尺寸单位
from docx.shared import Pt, Cm #Pt,字号大小 #Cm,厘米
常用的字体属性
new_style = new_docx.styles.add_style(name="new_style",style_type=1) #字体 new_style.font.name = "宋体" new_style._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体') #字体大小 new_style.font.size = Pt(18) #字体加粗 new_style.font.bold = True #字体倾斜 new_style.font.italic = True #字体rgb颜色 new_style.font.color.rgb = RGBColor(255,0,0) #字体主题颜色 #需要导入docx.enum.dml.MSO_THEME_COLOR_INDEX模块 #有ACCENT_1、ACCENT_2、ACCENT_3、ACCENT_4、ACCENT_5、ACCENT_6、BACKGROUND_1、BACKGROUND_2、DARK_1 #DARK_2、FOLLOWED_HYPERLINK、HYPERLINK、LIGHT_1、LIGHT_2、TEXT_1、TEXT_2等内置主题颜色 from docx.enum.dml import MSO_THEME_COLOR_INDEX new_style.font.color.theme_color = MSO_THEME_COLOR_INDEX.ACCENT_1 #字体单下划线 new_style.font.underline = True #如需设置下划线的样式,则需导入from docx.enum.text.WD_UNDERLINE模块 from docx.enum.text import WD_UNDERLINE new_style.font.underline = WD_UNDERLINE.NONE #取消下划线 new_style.font.underline = WD_UNDERLINE.SINGLE #单下划线 new_style.font.underline = WD_UNDERLINE.WORDS #单个词的下划线 new_style.font.underline = WD_UNDERLINE.DOUBLE #双下划线 new_style.font.underline = WD_UNDERLINE.DOTTED #点 new_style.font.underline = WD_UNDERLINE.THICK #粗线 new_style.font.underline = WD_UNDERLINE.DASH #破折号 new_style.font.underline = WD_UNDERLINE.DOT_DASH #粗点-粗破折号 new_style.font.underline = WD_UNDERLINE.DOT_DOT_DASH #点-点-破折号 new_style.font.underline = WD_UNDERLINE.WAVY #波浪线 new_style.font.underline = WD_UNDERLINE.DOTTED_HEAVY #粗点 new_style.font.underline = WD_UNDERLINE.DASH_HEAVY #粗破折号 new_style.font.underline = WD_UNDERLINE.DOT_DASH_HEAVY #粗点-粗破折号 new_style.font.underline = WD_UNDERLINE.DOT_DOT_DASH_HEAVY #粗点-粗点-粗破折号 new_style.font.underline = WD_UNDERLINE.WAVY_HEAVY #粗波浪线 new_style.font.underline = WD_UNDERLINE.DASH_LONG #长破折号 new_style.font.underline = WD_UNDERLINE.WAVY_DOUBLE #双波浪线 new_style.font.underline = WD_UNDERLINE.DASH_LONG_HEAVY #长粗破折号 #字体特殊效果 new_style.font.strike = True #单删除线 new_style.font.double_strike = True #双删除线 new_style.font.superscript = True #上标 new_style.font.subscript = True #下标 new_style.font.imprint = True #阴文 new_style.font.emboss = True #阳文 new_style.font.shadow = True #阴影 new_style.font.outline = True #空心 new_style.font.all_caps = True #全部大写字母 new_style.font.small_caps = True #小型大写字母 new_style.font.hidden = True #隐藏 new_style.font.web_hidden = True #网页浏览隐藏
常用的段落属性
from docx.shared import Inches new_style.paragraph_format.left_indent = Inches(0.5) #段落缩进 new_style.paragraph_format.first_line_indent = Inches(0.5) #首行缩进 from docx.enum.text import WD_LINE_SPACING new_style.paragraph_format.line_spacing_rule = WD_LINE_SPACING.EXACTLY #固定值 new_style.paragraph_format.line_spacing = Pt(18) #固定值18磅 new_style.paragraph_format.line_spacing_rule = WD_LINE_SPACING.MULTIPLE #多倍行距 new_style.paragraph_format.line_spacing = 1.75 #1.75倍行间距 new_style.paragraph_format.page_break_before = True #段前分页 new_style.paragraph_format.widow_control = True #孤行控制 new_style.paragraph_format.keep_with_next = True #与下段同页 new_style.paragraph_format.keep_together = True #段中不分页
表格
table = new_docx.add_table(rows=7, cols=4, style='Table Grid') #添加7行4列表格,样式为'Table Grid' from docx.enum.table import WD_TABLE_ALIGNMENT for row in table.rows: for cell in row.cells: cell.vertical_alignment = WD_TABLE_ALIGNMENT.CENTER #设置单元格内容的垂直对齐方式,分别有LEFT、CENTER、RIGHT cell.width = Cm(5) #设置单元格的宽度 table.style.paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER #设置单元格内容的水平对齐方式,分别有LEFT、CENTER、RIGHT #设置表格每一行的行高 table.rows[0].height = Cm(1) table.rows[1].height = Cm(2) table.rows[2].height = Cm(3) table.cell(0,0).merge(table.cell(0,1)) #合并第1行第1个单元格至第1行第2个单元格 table.cell(1,0).merge(table.cell(2,1)) #合并第2行第1个单元格至第3行第2个单元格
插入图片
new_docx.add_picture('demo.png', width=Inches(1.0), height=Inches(1.0))
页面设置
new_docx.sections[0].top_margin = Cm(2) #上边距 new_docx.sections[0].bottom_margin = Cm(2) #下边距 new_docx.sections[0].left_margin = Cm(2) #左边距 new_docx.sections[0].right_margin = Cm(2) #右边距 new_docx.sections[0].header.paragraphs[0].add_run('我是页眉') #添加页眉内容 new_docx.sections[0].footer.paragraphs[0].add_run('我是页脚') #添加页脚内容 #设置页眉页脚的边距 new_docx.sections[0].header_distance = Cm(3) new_docx.sections[0].footer_distance = Cm(3)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步