Python操作word常見(jiàn)方法示例【win32com與docx模塊】
本文實(shí)例講述了Python操作word常見(jiàn)方法。分享給大家供大家參考,具體如下:
這里介紹兩種方式:
- 使用win32com
- 使用docx
1. 使用win32com擴(kuò)展包
只對(duì)windows平臺(tái)有效
代碼:
# coding=utf-8
import win32com
from win32com.client import Dispatch, DispatchEx
word = Dispatch('Word.Application') # 打開(kāi)word應(yīng)用程序
# word = DispatchEx('Word.Application') #啟動(dòng)獨(dú)立的進(jìn)程
word.Visible = 0 # 后臺(tái)運(yùn)行,不顯示
word.DisplayAlerts = 0 # 不警告
path = 'G:/WorkSpace/Python/tmp/test.docx' # word文件路徑
doc = word.Documents.Open(FileName=path, Encoding='gbk')
# content = doc.Range(doc.Content.Start, doc.Content.End)
# content = doc.Range()
print '----------------'
print '段落數(shù): ', doc.Paragraphs.count
# 利用下標(biāo)遍歷段落
for i in range(len(doc.Paragraphs)):
para = doc.Paragraphs[i]
print para.Range.text
print '-------------------------'
# 直接遍歷段落
for para in doc.paragraphs:
print para.Range.text
# print para #只能用于文檔內(nèi)容全英文的情況
doc.Close() # 關(guān)閉word文檔
# word.Quit #關(guān)閉word程序
2. 使用docx擴(kuò)展包
優(yōu)點(diǎn):不依賴操作系統(tǒng),跨平臺(tái)
安裝:
pip install python-docx
參考文檔: https://python-docx.readthedocs.io/en/latest/index.html
代碼:
import docx def read_docx(file_name): doc = docx.Document(file_name) content = '\n'.join([para.text for para in doc.paragraphs]) return content
創(chuàng)建表格
# coding=utf-8
import docx
doc = docx.Document()
table = doc.add_table(rows=1, cols=3, style='Table Grid') #創(chuàng)建帶邊框的表格
hdr_cells = table.rows[0].cells # 獲取第0行所有所有單元格
hdr_cells[0].text = 'Name'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
# 添加三行數(shù)據(jù)
data_lines = 3
for i in range(data_lines):
cells = table.add_row().cells
cells[0].text = 'Name%s' % i
cells[1].text = 'Id%s' % i
cells[2].text = 'Desc%s' % i
rows = 2
cols = 4
table = doc.add_table(rows=rows, cols=cols)
val = 1
for i in range(rows):
cells = table.rows[i].cells
for j in range(cols):
cells[j].text = str(val * 10)
val += 1
doc.save('tmp.docx')
讀取表格
# coding=utf-8
import docx
doc = docx.Document('tmp.docx')
for table in doc.tables: # 遍歷所有表格
print '----table------'
for row in table.rows: # 遍歷表格的所有行
# row_str = '\t'.join([cell.text for cell in row.cells]) # 一行數(shù)據(jù)
# print row_str
for cell in row.cells:
print cell.text, '\t',
print
相關(guān)樣式參考: https://python-docx.readthedocs.io/en/latest/user/styles-understanding.html
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python自動(dòng)化操作之動(dòng)態(tài)驗(yàn)證碼、滑動(dòng)驗(yàn)證碼的降噪和識(shí)別
很多網(wǎng)站登錄都需要輸入驗(yàn)證碼,如果要實(shí)現(xiàn)自動(dòng)登錄就不可避免的要識(shí)別驗(yàn)證碼,下面這篇文章主要給大家介紹了關(guān)于python自動(dòng)化操作之動(dòng)態(tài)驗(yàn)證碼、滑動(dòng)驗(yàn)證碼的降噪和識(shí)別,需要的朋友可以參考下2021-08-08
解決python報(bào)錯(cuò)ImportError:urllib3?v2.0?only?supports?OpenSSL
這篇文章主要介紹了解決python報(bào)錯(cuò)ImportError:urllib3?v2.0?only?supports?OpenSSL?1.1.1+的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Python爬取英雄聯(lián)盟MSI直播間彈幕并生成詞云圖
很開(kāi)心RNG最近在英雄聯(lián)盟季中賽奪冠了,特地爬取了直播間彈幕并生成詞云圖,大家一起開(kāi)心一下,看看奪冠時(shí)大家都在說(shuō)什么,需要的朋友可以參考下2021-06-06
Python實(shí)現(xiàn)去除Excel重復(fù)數(shù)據(jù)并統(tǒng)計(jì)重復(fù)次數(shù)
這篇文章主要為大家詳細(xì)介紹了如何利用Python語(yǔ)言實(shí)現(xiàn)文本數(shù)據(jù)去重,創(chuàng)建包含唯一值的新列,并統(tǒng)計(jì)文本數(shù)據(jù)出現(xiàn)的次數(shù),需要的可以參考下2023-08-08
Django中如何使用Celery執(zhí)行異步任務(wù)
這篇文章主要介紹了Django中如何使用Celery執(zhí)行異步任務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
python序列化與數(shù)據(jù)持久化實(shí)例詳解
這篇文章主要介紹了python序列化與數(shù)據(jù)持久化,結(jié)合實(shí)例形式詳細(xì)分析了Python序列化與數(shù)據(jù)持久化相關(guān)原理、實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-12-12

