Python實現(xiàn)Word的讀寫改操作
用 docx 模塊讀取 Word
docx 安裝
cmd 中輸入pip install python-docx 即可安裝 docx 模塊
docx 常用函數(shù)
創(chuàng)建空白文檔
from docx import Document
document = Document()
document.save("word.docx") # 生成空白 word
print(document)

讀取文檔
from docx import Document
document = Document("word.docx") # 讀取現(xiàn)有的 word 建立文檔對象

獲取文檔段落
from docx import Document
document = Document("word.docx") # 讀取現(xiàn)有的 word 建立文檔對象
all_paragraphs = document.paragraphs
print(type(all_paragraphs))
for paragraph in all_paragraphs:
# print(paragraph.paragraph_format) # 打印出word中每段的樣式名稱
# 打印每一個段落的文字
print(paragraph.text)
# 循環(huán)讀取每個段落里的run內(nèi)容
# 一個run對象是相同樣式文本的延續(xù)
for paragraph in all_paragraphs:
for run in paragraph.runs:
print(run.text) # 打印run內(nèi)容

Word 調(diào)整樣式
from docx import Document
from docx.shared import Pt, RGBColor
document = Document() # 讀取現(xiàn)有的 word 建立文檔對象
# 二、寫入內(nèi)容
# 段落
p1 = document.add_paragraph("早睡早起?。?!")
format_p1 = p1.paragraph_format
# 左右縮進
format_p1.left_indent = Pt(20)
format_p1.right_indent = Pt(20)
# 首行縮進
format_p1.first_line_indent = Pt(20)
# 行間距
format_p1.line_spacing = 1
# 追加
# 一個run對象是相同樣式文本的延續(xù)
run = p1.add_run("我也想做舔狗\n")
# 字體,字號,文字顏色
run.font.size = Pt(12)
run.font.name = "微軟雅黑"
run.font.color.rgb = RGBColor(235, 123, 10)
run1 = p1.add_run("賈某人不學習")
# 加粗,下劃線,斜體
run1.bold = True
run1.font.underline = True
run1.font.italic = True
# # 三、保存文件
document.save("word.docx")
all_paragraphs = document.paragraphs
# print(type(all_paragraphs))
# <class 'list'>,打印后發(fā)現(xiàn)是列表
# 是列表就開始循環(huán)讀取d
for paragraph in all_paragraphs:
# print(paragraph.paragraph_format) # 打印出word中每段的樣式名稱
# 打印每一個段落的文字
print(paragraph.text)
# 循環(huán)讀取每個段落里的run內(nèi)容
# for run in paragraph.runs:
# print(run.text) # 打印run內(nèi)容

Word 寫入操作
from docx import Document
from docx.shared import Pt, RGBColor
document = Document() # 讀取現(xiàn)有的 word 建立文檔對象
# 二、寫入內(nèi)容
document.add_heading("python 操作 Word")
# 段落
p1 = document.add_paragraph("早睡早起?。?!")
p1.insert_paragraph_before("Power!??!")
format_p1 = p1.paragraph_format
# 左右縮進
format_p1.left_indent = Pt(20)
format_p1.right_indent = Pt(20)
# 首行縮進
format_p1.first_line_indent = Pt(20)
# 行間距
format_p1.line_spacing = 1
# 追加
# 一個run對象是相同樣式文本的延續(xù)
run = p1.add_run("我也想做舔狗\n")
# 字體,字號,文字顏色
run.font.size = Pt(12)
run.font.name = "微軟雅黑"
run.font.color.rgb = RGBColor(235, 123, 10)
run1 = p1.add_run("賈某人不學習")
# 加粗,下劃線,斜體
run1.bold = True
run1.font.underline = True
run1.font.italic = True
# # 三、保存文件
document.save("word.docx")
all_paragraphs = document.paragraphs
# print(type(all_paragraphs))
# <class 'list'>,打印后發(fā)現(xiàn)是列表
# 是列表就開始循環(huán)讀取d
for paragraph in all_paragraphs:
# print(paragraph.paragraph_format) # 打印出word中每段的樣式名稱
# 打印每一個段落的文字
print(paragraph.text)
# 循環(huán)讀取每個段落里的run內(nèi)容
# for run in paragraph.runs:
# print(run.text) # 打印run內(nèi)容

到此這篇關(guān)于Python實現(xiàn)Word的讀寫改操作的文章就介紹到這了,更多相關(guān)Python的內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實現(xiàn)照片集變成視頻的代碼實現(xiàn)
看著電腦里亂七八糟的照片,有大有小,寬高不一,突然想找個方式把他們統(tǒng)一起來,然后做成視頻更好,所以本文給大家介紹了python實現(xiàn)照片集變成視頻的實現(xiàn),需要的朋友可以參考下2024-10-10
Python數(shù)據(jù)結(jié)構(gòu)之棧、隊列的實現(xiàn)代碼分享
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之棧、隊列的實現(xiàn)代碼分享,具有一定參考價值,需要的朋友可以了解下。2017-12-12
Selenium結(jié)合BeautifulSoup4編寫簡單的python爬蟲
這篇文章主要介紹了Selenium結(jié)合BeautifulSoup4編寫簡單的python爬蟲,幫助大家更好的理解和學習python 爬蟲的相關(guān)知識,感興趣的朋友可以了解下2020-11-11
python opencv設(shè)置攝像頭分辨率以及各個參數(shù)的方法
下面小編就為大家分享一篇python opencv設(shè)置攝像頭分辨率以及各個參數(shù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
python中的一些類型轉(zhuǎn)換函數(shù)小結(jié)
python中的一些類型轉(zhuǎn)換函數(shù),供大家參考2013-02-02

