Python使用python-docx讀寫word文檔
python-docx庫(kù)可用于創(chuàng)建和編輯Microsoft Word(.docx)文件。
官方文檔:鏈接地址
備注:
doc是微軟的專有的文件格式,docx是Microsoft Office2007之后版本使用,其基于Office Open XML標(biāo)準(zhǔn)的壓縮文件格式,比 doc文件所占用空間更小。docx格式的文件本質(zhì)上是一個(gè)ZIP文件,所以其實(shí)也可以把.docx文件直接改成.zip,解壓后,里面的 word/document.xml包含了Word文檔的大部分內(nèi)容,圖片文件則保存在word/media里面。
python-docx不支持.doc文件,間接解決方法是在代碼里面先把.doc轉(zhuǎn)為.docx。
一、安裝包
pip3 install python-docx
二、創(chuàng)建word文檔
下面是在官文示例基礎(chǔ)上對(duì)個(gè)別地方稍微修改,并加上函數(shù)的使用說(shuō)明
from docx import Document from docx.shared import Inches document = Document() #添加標(biāo)題,并設(shè)置級(jí)別,范圍:0 至 9,默認(rèn)為1 document.add_heading('Document Title', 0) #添加段落,文本可以包含制表符(\t)、換行符(\n)或回車符(\r)等 p = document.add_paragraph('A plain paragraph having some ') #在段落后面追加文本,并可設(shè)置樣式 p.add_run('bold').bold = True p.add_run(' and some ') p.add_run('italic.').italic = True document.add_heading('Heading, level 1', level=1) document.add_paragraph('Intense quote', style='Intense Quote') #添加項(xiàng)目列表(前面一個(gè)小圓點(diǎn)) document.add_paragraph( 'first item in unordered list', style='List Bullet' ) document.add_paragraph('second item in unordered list', style='List Bullet') #添加項(xiàng)目列表(前面數(shù)字) document.add_paragraph('first item in ordered list', style='List Number') document.add_paragraph('second item in ordered list', style='List Number') #添加圖片 document.add_picture('monty-truth.png', width=Inches(1.25)) records = ( (3, '101', 'Spam'), (7, '422', 'Eggs'), (4, '631', 'Spam, spam, eggs, and spam') ) #添加表格:一行三列 # 表格樣式參數(shù)可選: # Normal Table # Table Grid # Light Shading、 Light Shading Accent 1 至 Light Shading Accent 6 # Light List、Light List Accent 1 至 Light List Accent 6 # Light Grid、Light Grid Accent 1 至 Light Grid Accent 6 # 太多了其它省略... table = document.add_table(rows=1, cols=3, style='Light Shading Accent 2') #獲取第一行的單元格列表 hdr_cells = table.rows[0].cells #下面三行設(shè)置上面第一行的三個(gè)單元格的文本值 hdr_cells[0].text = 'Qty' hdr_cells[1].text = 'Id' hdr_cells[2].text = 'Desc' for qty, id, desc in records: #表格添加行,并返回行所在的單元格列表 row_cells = table.add_row().cells row_cells[0].text = str(qty) row_cells[1].text = id row_cells[2].text = desc document.add_page_break() #保存.docx文檔 document.save('demo.docx')
創(chuàng)建的demo.docx內(nèi)容如下:
三、讀取word文檔
from docx import Document doc = Document('demo.docx') #每一段的內(nèi)容 for para in doc.paragraphs: print(para.text) #每一段的編號(hào)、內(nèi)容 for i in range(len(doc.paragraphs)): print(str(i), doc.paragraphs[i].text) #表格 tbs = doc.tables for tb in tbs: #行 for row in tb.rows: #列 for cell in row.cells: print(cell.text) #也可以用下面方法 '''text = '' for p in cell.paragraphs: text += p.text print(text)'''
運(yùn)行結(jié)果:
Document Title A plain paragraph having some bold and some italic. Heading, level 1 Intense quote first item in unordered list second item in unordered list first item in ordered list second item in ordered list Document Title A plain paragraph having some bold and some italic. Heading, level 1 Intense quote first item in unordered list second item in unordered list first item in ordered list second item in ordered list Qty Id Desc 101 Spam 422 Eggs 631 Spam, spam, eggs, and spam [Finished in 0.2s]
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python輸出結(jié)果刷新及進(jìn)度條的實(shí)現(xiàn)操作
這篇文章主要介紹了python輸出結(jié)果刷新及進(jìn)度條的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-07-07Effective Python bytes 與 str 的區(qū)別
這篇文章主要介紹了Effective Python bytes 與 str 的區(qū)別,Python 有兩種類型可以表示字符序列,下面圍繞Python bytes 與 str 的相關(guān)資料展開內(nèi)容,需要的朋友可以參考一下2021-11-11Python實(shí)戰(zhàn)項(xiàng)目刮刮樂的實(shí)現(xiàn)詳解流程
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python實(shí)現(xiàn)一個(gè)刮刮樂的小項(xiàng)目,大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11python中將zip壓縮包轉(zhuǎn)為gz.tar的方法
今天小編就為大家分享一篇python中將zip壓縮包轉(zhuǎn)為gz.tar的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-10-10利用python、tensorflow、opencv、pyqt5實(shí)現(xiàn)人臉實(shí)時(shí)簽到系統(tǒng)
這篇文章主要介紹了利用python、tensorflow、opencv、pyqt5實(shí)現(xiàn)人臉實(shí)時(shí)簽到系統(tǒng),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09Python的hashlib庫(kù)獲取超大文件的md5值實(shí)例探究
這篇文章主要為大家介紹了Python的hashlib庫(kù)獲取超大文件的md5值實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01python中字符串內(nèi)置函數(shù)的用法總結(jié)
這篇文章給大家總結(jié)了python中字符串內(nèi)置函數(shù)的用法以及相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友學(xué)習(xí)下。2018-09-09如何實(shí)現(xiàn)Python調(diào)用Golang代碼詳解
這篇文章主要介紹了如何實(shí)現(xiàn)Python調(diào)用Golang代碼,Python和Golang都是當(dāng)下非常流行的編程語(yǔ)言,在實(shí)際開發(fā)中,我們可能會(huì)遇到需要將Python和Golang進(jìn)行組合使用的場(chǎng)景,感興趣想要詳細(xì)了解可以參考下文2023-05-05