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)文章
pytorch實現(xiàn)建立自己的數(shù)據(jù)集(以mnist為例)
今天小編就為大家分享一篇pytorch實現(xiàn)建立自己的數(shù)據(jù)集(以mnist為例),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01Tensorflow 實現(xiàn)線性回歸模型的示例代碼
這篇文章主要介紹了Tensorflow 實現(xiàn)線性回歸模型,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05詳解Python中Pytest和Unittest的區(qū)別
Pytest?和?Unittest是Python中屬于最常用的兩個測試框架。那么他們有些什么區(qū)別呢??Playwright?為什么只給了Pytest的深度支持,而不是Unittest呢?本文就來和大家詳細聊聊2023-03-03Python如何利用正則表達式爬取網(wǎng)頁信息及圖片
這篇文章主要給大家介紹了關(guān)于Python如何利用正則表達式爬取網(wǎng)頁信息及圖片的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04