上海網(wǎng)站公司電話(huà)seo推廣是什么意思
python自動(dòng)化辦公之—Word
python-docx庫(kù)
1、安裝python-docx庫(kù)
pip install python-docx
2、基本語(yǔ)法
1、打開(kāi)文檔
document = Document()
2、加入標(biāo)題
document.add_heading('總標(biāo)題',0)
document.add_heading('?級(jí)標(biāo)題',1)
document.add_heading('?級(jí)標(biāo)題',2)
3、添加文本
paragraph = document.add_paragraph('?本內(nèi)容')
4、設(shè)置字號(hào)
run = paragraph.add_run('設(shè)置字號(hào)、')
run.font.size = Pt(34)
5、設(shè)置字體
run = paragraph.add_run('設(shè)置中?字體、')
run.font.name = '宋體'
r = run._element
r.rPr.rFonts.set(qn('w:eastAsia'), '宋體')
6、首行縮進(jìn)
left_indent :左邊縮進(jìn)
right_indent :右邊縮進(jìn)
first_line_indent :??縮進(jìn)
3、樣例1
from docx import Document
from docx.shared import Inches
doc = Document()
article = doc.add_paragraph ()
a2 = article.add_run("因______,特向您請(qǐng)事假____天。請(qǐng)假時(shí)間?_____年\
___?___??_____年___?___?。這段時(shí)間內(nèi)原計(jì)劃安\
排的課程已做好處理,希望領(lǐng)導(dǎo)批準(zhǔn)。")
# ??縮進(jìn)2個(gè)字符
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', style='List Bullet')
doc.add_paragraph('?序列表2', style='List Bullet')
doc.save('test.docx')
# Style='List Bullet' # 為?序
# Style='List Number' # 為有序
5、添加圖片
from docx import Document
from docx.shared import Inches
doc = Document()
doc.add_picture('圖片.jpg',width=Inches(1.35),heigh=Inches(1.35))
#png為圖?名稱(chēng)(必須與代碼?件在同?個(gè)?件夾內(nèi))
doc.save('test.docx')
6、添加表格
table = document.add_table(rows=1, cols=3)
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 = 'test'+str(i)row_cells[1].text = str(i)row_cells[2].text = 'desc'+str(i)
doc.save('test.docx')
7、獲取文檔操作
from docx import Document
docu = Document(r'test.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()
# 全局字體設(shè)置
doc.styles['Normal'].font.name = u'宋體'
doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')
# 1、標(biāo)題
title = doc.add_paragraph ()
title1 = title.add_run ('公司員?請(qǐng)假條') # 內(nèi)容
title1.font.size = Pt(20) # 設(shè)置字體??
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(
'因_______ ,特向您請(qǐng)事假天。請(qǐng)假時(shí)間?_年???_年??。這段時(shí)間內(nèi)原計(jì) 劃安排的課程已做好處理,希望領(lǐng)導(dǎo)批準(zhǔn)。')
# ??縮進(jìn) 負(fù)值表示懸掛縮進(jìn)
article2_format = article2.paragraph_format
article2_format .first_line_indent =Inches(0.3)
article3 = doc.add_paragraph ()
a3 = article3.add_run('請(qǐng)假?:') # 內(nèi)容
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('年 ? ?') # 內(nèi)容
nowData.alignment =WD_PARAGRAPH_ALIGNMENT.RIGHT
nowData_format = nowData.paragraph_format
nowData_format .right_indent = Inches(0.3)
# 這?步調(diào)整?件格式為居中
doc.save('test.docx' )