python3處理word文檔實(shí)例分析
直接使用word文檔已經(jīng)難不倒大家了,有沒(méi)有想過(guò)用python構(gòu)建一個(gè)word文檔寫(xiě)點(diǎn)文章呢?當(dāng)然這個(gè)文章的框架需要我們用代碼一點(diǎn)點(diǎn)的建立,在過(guò)程上有一點(diǎn)繁瑣,一下子看不懂的小伙伴可以把它拆分成幾個(gè)部分來(lái)看。下面就在python3處理word文檔的代碼給大家?guī)?lái)講解,還會(huì)有一些設(shè)置文章格式的技巧。
一個(gè)Word文檔,主要由下面這些內(nèi)容元素構(gòu)成,每個(gè)元素都有對(duì)應(yīng)的方法處理:
- 標(biāo)題:add_heading()
- 段落:add_paragraph()
- 文本:add_run(),其返回對(duì)象支持設(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_自動(dòng)生成標(biāo)題', 0) document.add_heading('基本:文本', level=1) p = document.add_paragraph('測(cè)試文本\n測(cè)試內(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) # 測(cè)試第三方字體 x = p.add_run('三方字體測(cè)試\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、無(wú)序列表', level=2) opts = ['選項(xiàng)1','選項(xiàng)2', '選項(xiàng)3'] # 無(wú)需列表 for opt in opts: document.add_paragraph(opt, style='List Bullet') document.add_heading('標(biāo)題1.2、有序列表', level=2) # 有序列表 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)扇', '無(wú)葉風(fēng)扇'), (2, '吹風(fēng)機(jī)', '離子風(fēng)機(jī)'), (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('這是一個(gè)樣式為 ListBullet 的段落', style='ListBullet')
或
paragraph = document.add_paragraph('這是一個(gè)樣式為 ListBullet 的段落') paragraph.style = 'List Bullet'
設(shè)置段落間距
分為 段前 和 段后 ,設(shè)置值用 Pt 單位是 磅 ,如下:
paragraph_format.space_before = Pt(18) paragraph_format.space_after = Pt(12)
設(shè)置段落行距
當(dāng)行距為 最小值 和 固定值 時(shí),設(shè)置值單位為 磅 ,需要用 Pt ;當(dāng)行距為 多倍行距 時(shí),設(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倍行間距
到此這篇關(guān)于python3處理word文檔實(shí)例分析的文章就介紹到這了,更多相關(guān)python3處理word文檔代碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python數(shù)據(jù)結(jié)構(gòu)棧實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換簡(jiǎn)單示例
眾所周知計(jì)算機(jī)的內(nèi)存都是以二進(jìn)制的形式進(jìn)行數(shù)據(jù)存儲(chǔ),下面這篇文章主要給大家介紹了關(guān)于Python數(shù)據(jù)結(jié)構(gòu)棧實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02解決Python3 控制臺(tái)輸出InsecureRequestWarning問(wèn)題
這篇文章主要介紹了解決Python3 控制臺(tái)輸出InsecureRequestWarning的問(wèn)題 ,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07Python基礎(chǔ)教程之Matplotlib圖形繪制詳解
Matplotlib是一個(gè)廣泛使用的數(shù)據(jù)可視化庫(kù),提供了豐富的繪圖功能,用于創(chuàng)建各種類型的靜態(tài)、動(dòng)態(tài)和交互式圖形,本文將通過(guò)多個(gè)例子給大家詳細(xì)介紹一下Python的Matplotlib圖形繪制,需要的朋友可以參考下2023-07-07通過(guò)Python 獲取Android設(shè)備信息的輕量級(jí)框架
今天跟大家分享一下,如何通過(guò)Python實(shí)現(xiàn)一個(gè)輕量級(jí)的庫(kù)來(lái)獲取電腦上連接的Android設(shè)備信息,需要的朋友參考下吧2017-12-12Python使用matplotlib 模塊scatter方法畫(huà)散點(diǎn)圖示例
這篇文章主要介紹了Python使用matplotlib 模塊scatter方法畫(huà)散點(diǎn)圖,結(jié)合實(shí)例形式分析了Python數(shù)值運(yùn)算與matplotlib模塊圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2019-09-09對(duì)Python的交互模式和直接運(yùn)行.py文件的區(qū)別詳解
今天小編就為大家分享一篇對(duì)Python的交互模式和直接運(yùn)行.py文件的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06