python-docx庫可用于創(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ǔ)上對個(gè)別地方稍微修改,并加上函數(shù)的使用說明
from docx import Document from docx.shared import Inches document = Document() #添加標(biāo)題,并設(shè)置級別,范圍: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) #每一段的編號、內(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]
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
