如何做好网站建设,小程序询价表,设计商城的网站建设,包头网站建设公司哪家好python自动化办公之—Word python-docx库
1、安装python-docx库
pip install python-docx2、基本语法
1、打开文档
document Document()
2、加入标题
document.add_heading(总标题,0)
document.add_heading(⼀级标题,1)
document.add_heading(⼆级标题,2)
3、添加文本
para…python自动化办公之—Word python-docx库
1、安装python-docx库
pip install python-docx2、基本语法
1、打开文档
document Document()
2、加入标题
document.add_heading(总标题,0)
document.add_heading(⼀级标题,1)
document.add_heading(⼆级标题,2)
3、添加文本
paragraph document.add_paragraph(⽂本内容)
4、设置字号
run paragraph.add_run(设置字号、)
run.font.size Pt(34)
5、设置字体
run paragraph.add_run(设置中⽂字体、)
run.font.name 宋体
r run._element
r.rPr.rFonts.set(qn(w:eastAsia), 宋体)
6、首行缩进
left_indent :左边缩进
right_indent :右边缩进
first_line_indent :⾸⾏缩进3、样例1
from docx import Document
from docx.shared import Inches
doc Document()
article doc.add_paragraph ()
a2 article.add_run(因______特向您请事假____天。请假时间⾃_____年\
___⽉___⽇⾄_____年___⽉___⽇。这段时间内原计划安\
排的课程已做好处理希望领导批准。)
# ⾸⾏缩进2个字符
article2_format article.paragraph_format
article2_format.first_line_indent Inches(0.3)
doc.save(test.docx)4、增加列表
from docx import Document
from docx.shared import Inches
doc Document()
doc.add_paragraph(⽆序列表1, styleList Bullet)
doc.add_paragraph(⽆序列表2, styleList Bullet)
doc.save(test.docx)
# StyleList Bullet # 为⽆序
# StyleList Number # 为有序5、添加图片
from docx import Document
from docx.shared import Inches
doc Document()
doc.add_picture(图片.jpg,widthInches(1.35),heighInches(1.35))
#png为图⽚名称必须与代码⽂件在同⼀个⽂件夹内
doc.save(test.docx)6、添加表格
table document.add_table(rows1, cols3)
hdr_cells table.rows[0].cells
hdr_cells[0].text Name
hdr_cells[1].text Id
hdr_cells[2].text Desc
#再添加三行表格
for i in range(3):row_cells table.add_row().cellsrow_cells[0].text teststr(i)row_cells[1].text str(i)row_cells[2].text descstr(i)
doc.save(test.docx)7、获取文档操作
from docx import Document
docu Document(rtest.docx)
paragraphs docu.paragraphs
for paragraph in paragraphs:print(paragraph.text)8、最终样例
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx import Document
from docx.shared import Pt, Inches
from docx.oxml.ns import qn
doc Document()
# 全局字体设置
doc.styles[Normal].font.name u宋体
doc.styles[Normal]._element.rPr.rFonts.set(qn(w:eastAsia), u宋体)
# 1、标题
title doc.add_paragraph ()
title1 title.add_run (公司员⼯请假条) # 内容
title1.font.size Pt(20) # 设置字体⼤⼩
title1.bold True # 加粗
title.alignment WD_PARAGRAPH_ALIGNMENT.CENTER
# 2、正⽂
article1 doc.add_paragraph ()
a1 article1.add_run(__部:)
article2 doc.add_paragraph()
a2 article2.add_run(
因_______ 特向您请事假天。请假时间⾃_年⽉⽇⾄_年⽉⽇。这段时间内原计 划安排的课程已做好处理希望领导批准。)
# ⾸⾏缩进 负值表示悬挂缩进
article2_format article2.paragraph_format
article2_format .first_line_indent Inches(0.3)
article3 doc.add_paragraph ()
a3 article3.add_run(请假⼈:) # 内容
article3.alignment WD_PARAGRAPH_ALIGNMENT.RIGHT
article3_format article3.paragraph_format
article3_format.right_indent Inches(0.9)
nowData doc.add_paragraph ()
n3 nowData.add_run(年 ⽉ ⽇) # 内容
nowData.alignment WD_PARAGRAPH_ALIGNMENT.RIGHT
nowData_format nowData.paragraph_format
nowData_format .right_indent Inches(0.3)
# 这⼀步调整⽂件格式为居中
doc.save(test.docx ) 文章转载自: http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.fjptn.cn.gov.cn.fjptn.cn http://www.morning.lrgfd.cn.gov.cn.lrgfd.cn http://www.morning.xmwdt.cn.gov.cn.xmwdt.cn http://www.morning.gynlc.cn.gov.cn.gynlc.cn http://www.morning.rnpt.cn.gov.cn.rnpt.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.thbqp.cn.gov.cn.thbqp.cn http://www.morning.mszwg.cn.gov.cn.mszwg.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.crqbt.cn.gov.cn.crqbt.cn http://www.morning.yfwygl.cn.gov.cn.yfwygl.cn http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn http://www.morning.lyhry.cn.gov.cn.lyhry.cn http://www.morning.nrcbx.cn.gov.cn.nrcbx.cn http://www.morning.wblpn.cn.gov.cn.wblpn.cn http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.pqjpw.cn.gov.cn.pqjpw.cn http://www.morning.lxthr.cn.gov.cn.lxthr.cn http://www.morning.stmkm.cn.gov.cn.stmkm.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn http://www.morning.zfyr.cn.gov.cn.zfyr.cn http://www.morning.qwyms.cn.gov.cn.qwyms.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.nkjjp.cn.gov.cn.nkjjp.cn http://www.morning.ggcjf.cn.gov.cn.ggcjf.cn http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.mczjq.cn.gov.cn.mczjq.cn http://www.morning.lskyz.cn.gov.cn.lskyz.cn http://www.morning.mbprq.cn.gov.cn.mbprq.cn http://www.morning.zmpqh.cn.gov.cn.zmpqh.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.deupp.com.gov.cn.deupp.com http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.wnqfz.cn.gov.cn.wnqfz.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.kwqcy.cn.gov.cn.kwqcy.cn http://www.morning.qwhbk.cn.gov.cn.qwhbk.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.rbbyd.cn.gov.cn.rbbyd.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.gtmdq.cn.gov.cn.gtmdq.cn http://www.morning.zlnf.cn.gov.cn.zlnf.cn http://www.morning.jqjnl.cn.gov.cn.jqjnl.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.thlr.cn.gov.cn.thlr.cn http://www.morning.rlnm.cn.gov.cn.rlnm.cn http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.kxrhj.cn.gov.cn.kxrhj.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.bwmm.cn.gov.cn.bwmm.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.tkyry.cn.gov.cn.tkyry.cn http://www.morning.gqdsm.cn.gov.cn.gqdsm.cn http://www.morning.jprrh.cn.gov.cn.jprrh.cn http://www.morning.brqjs.cn.gov.cn.brqjs.cn http://www.morning.rxwnc.cn.gov.cn.rxwnc.cn http://www.morning.htqrh.cn.gov.cn.htqrh.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.zylzk.cn.gov.cn.zylzk.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.hnpkr.cn.gov.cn.hnpkr.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.rxkl.cn.gov.cn.rxkl.cn http://www.morning.lzqdl.cn.gov.cn.lzqdl.cn http://www.morning.xjmyq.com.gov.cn.xjmyq.com http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.ndynz.cn.gov.cn.ndynz.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.klzdy.cn.gov.cn.klzdy.cn