新聞中心
直接使用word文檔已經(jīng)難不倒大家了,有沒有想過用python構(gòu)建一個word文檔寫點文章呢?當(dāng)然這個文章的框架需要我們用代碼一點點的建立,在過程上有一點繁瑣,一下子看不懂的小伙伴可以把它拆分成幾個部分來看。下面就在python3處理word文檔的代碼給大家?guī)碇v解,還會有一些設(shè)置文章格式的技巧。

一個Word文檔,主要由下面這些內(nèi)容元素構(gòu)成,每個元素都有對應(yīng)的方法處理:
-
標(biāo)題:add_heading()
-
段落:add_paragraph()
-
文本:add_run(),其返回對象支持設(shè)置文本屬性
-
圖片:add_picture()
-
表格:add_table()、add_row()、add_col()
import pathlib
from docx import Document
from docx.shared import Inches, Pt
from docx.oxml.ns import qn
path = list(pathlib.Path.cwd().parents)[1].joinpath('data/automate/003word')
out_path = path.joinpath('003word_create.docx')
img_path = path.joinpath('dance.jpg')
document = Document()
document.add_heading('Python1024_自動生成標(biāo)題', 0)
document.add_heading('基本:文本', level=1)
p = document.add_paragraph('測試文本\n測試內(nèi)容\n')
p.add_run('粗體部分內(nèi)容\n').bold = True
p.add_run('斜體部分\n').italic = True
p.add_run('下劃線部分\n').underline = True
p.add_run('字體設(shè)置\n').font.size = Pt(24)
# 測試第三方字體
x = p.add_run('三方字體測試\n')
x.font.name = 'Source Han Sans CN' # 思源字體
x.element.rPr.rFonts.set(qn('w:eastAsia'), 'Source Han Sans CN')
# 段落和引用
document.add_heading('標(biāo)題一:段落', level=1)
document.add_paragraph('引用塊', style='Intense Quote')
document.add_heading('標(biāo)題1.1、無序列表', level=2)
opts = ['選項1','選項2', '選項3']
# 無需列表
for opt in opts:
document.add_paragraph(opt, style='List Bullet')
document.add_heading('標(biāo)題1.2、有序列表', level=2)
# 有序列表
for opt in opts:
document.add_paragraph(opt, style='List Number')
document.add_heading('標(biāo)題二:圖片', level=1)
document.add_picture(str(img_path), width=Inches(5))
document.add_page_break()
document.add_heading('標(biāo)題三:表格', level=1)
records = (
(1, '電風(fēng)扇', '無葉風(fēng)扇'),
(2, '吹風(fēng)機', '離子風(fēng)機'),
(3, 'Macbook pro', 'Apple macbook pro 15寸')
)
# 表格
table = document.add_table(rows=1, cols=3)
# 表頭
hdr_cells = table.rows[0].cells
hdr_cells[0].text = '數(shù)量'
hdr_cells[1].text = 'ID'
hdr_cells[2].text = '描述信息'
# 表格數(shù)據(jù)
for qty, cid, desc in records:
row_cells = table.add_row().cells
row_cells[0].text = str(qty)
row_cells[1].text = cid
row_cells[2].text = desc
# 保存文檔
document.save(out_path)設(shè)置段落樣式,如下:
document.add_paragraph('這是一個樣式為 ListBullet 的段落', style='ListBullet')或
paragraph = document.add_paragraph('這是一個樣式為 ListBullet 的段落')
paragraph.style = 'List Bullet'設(shè)置段落間距
分為 段前 和 段后 ,設(shè)置值用 Pt 單位是 磅 ,如下:
paragraph_format.space_before = Pt(18) paragraph_format.space_after = Pt(12)
設(shè)置段落行距
當(dāng)行距為 最小值 和 固定值 時,設(shè)置值單位為 磅 ,需要用 Pt ;當(dāng)行距為 多倍行距 時,設(shè)置值為數(shù)值,如下:
from docx.shared import Length #SINGLE => 單倍行距(默認(rèn)) #ONE_POINT_FIVE => 1.5倍行距 #DOUBLE2 => 倍行距 #AT_LEAST => 最小值 #EXACTLY => 固定值 #MULTIPLE => 多倍行距 paragraph.line_spacing_rule = WD_LINE_SPACING.EXACTLY #固定值 paragraph_format.line_spacing = Pt(18) # 固定值18磅 paragraph.line_spacing_rule = WD_LINE_SPACING.MULTIPLE #多倍行距 paragraph_format.line_spacing = 1.75 # 1.75倍行間距
這里設(shè)置word的幾個格式技巧就講完啦,有的小伙伴直呼困難。小編想說的是只有我們親自去參與框架的搭建,才能對于以后操作word有更好的理解。更多Python學(xué)習(xí)指路:PyThon學(xué)習(xí)網(wǎng)教學(xué)中心。
名稱欄目:創(chuàng)新互聯(lián)Python教程:如何編寫python3處理word文檔代碼?
瀏覽地址:http://www.fisionsoft.com.cn/article/cdijpce.html


咨詢
建站咨詢
