欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)Word的讀寫(xiě)改操作

 更新時(shí)間:2021年11月23日 09:05:48   作者:Gettler?Main  
本文主要介紹了運(yùn)用docx模塊實(shí)現(xiàn)讀取Word,調(diào)整Word樣式以及Word 寫(xiě)入操作的示例代碼,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

用 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 建立文檔對(duì)象

獲取文檔段落

from docx import Document

document = Document("word.docx")  # 讀取現(xiàn)有的 word 建立文檔對(duì)象
all_paragraphs = document.paragraphs
print(type(all_paragraphs))
for paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的樣式名稱(chēng)
    # 打印每一個(gè)段落的文字
    print(paragraph.text)
    # 循環(huán)讀取每個(gè)段落里的run內(nèi)容
# 一個(gè)run對(duì)象是相同樣式文本的延續(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 建立文檔對(duì)象

# 二、寫(xiě)入內(nèi)容
# 段落
p1 = document.add_paragraph("早睡早起?。?!")
format_p1 = p1.paragraph_format
# 左右縮進(jìn)
format_p1.left_indent = Pt(20)
format_p1.right_indent = Pt(20)
# 首行縮進(jìn)
format_p1.first_line_indent = Pt(20)
# 行間距
format_p1.line_spacing = 1
# 追加
# 一個(gè)run對(duì)象是相同樣式文本的延續(xù)
run = p1.add_run("我也想做舔狗\n")
# 字體,字號(hào),文字顏色
run.font.size = Pt(12)
run.font.name = "微軟雅黑"
run.font.color.rgb = RGBColor(235, 123, 10)
run1 = p1.add_run("賈某人不學(xué)習(xí)")
# 加粗,下劃線,斜體
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)是列表
# 是列表就開(kāi)始循環(huán)讀取d
for paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的樣式名稱(chēng)
    # 打印每一個(gè)段落的文字
    print(paragraph.text)
    # 循環(huán)讀取每個(gè)段落里的run內(nèi)容
    # for run in paragraph.runs:
    # print(run.text)  # 打印run內(nèi)容

Word 寫(xiě)入操作

from docx import Document
from docx.shared import Pt, RGBColor

document = Document()  # 讀取現(xiàn)有的 word 建立文檔對(duì)象

# 二、寫(xiě)入內(nèi)容
document.add_heading("python 操作 Word")
# 段落
p1 = document.add_paragraph("早睡早起?。?!")
p1.insert_paragraph_before("Power!??!")
format_p1 = p1.paragraph_format
# 左右縮進(jìn)
format_p1.left_indent = Pt(20)
format_p1.right_indent = Pt(20)
# 首行縮進(jìn)
format_p1.first_line_indent = Pt(20)
# 行間距
format_p1.line_spacing = 1
# 追加
# 一個(gè)run對(duì)象是相同樣式文本的延續(xù)

run = p1.add_run("我也想做舔狗\n")
# 字體,字號(hào),文字顏色
run.font.size = Pt(12)
run.font.name = "微軟雅黑"
run.font.color.rgb = RGBColor(235, 123, 10)
run1 = p1.add_run("賈某人不學(xué)習(xí)")
# 加粗,下劃線,斜體
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)是列表
# 是列表就開(kāi)始循環(huán)讀取d
for paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的樣式名稱(chēng)
    # 打印每一個(gè)段落的文字
    print(paragraph.text)
    # 循環(huán)讀取每個(gè)段落里的run內(nèi)容
    # for run in paragraph.runs:
    # print(run.text)  # 打印run內(nèi)容

到此這篇關(guān)于Python實(shí)現(xiàn)Word的讀寫(xiě)改操作的文章就介紹到這了,更多相關(guān)Python的內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python之np.where()如何替換缺失值

    Python之np.where()如何替換缺失值

    這篇文章主要介紹了Python中的np.where()如何替換缺失值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python中的取反操作符(~)

    python中的取反操作符(~)

    這篇文章主要介紹了python中的取反操作符(~),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • pytorch實(shí)現(xiàn)建立自己的數(shù)據(jù)集(以mnist為例)

    pytorch實(shí)現(xiàn)建立自己的數(shù)據(jù)集(以mnist為例)

    今天小編就為大家分享一篇pytorch實(shí)現(xiàn)建立自己的數(shù)據(jù)集(以mnist為例),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • python操作mysql中文顯示亂碼的解決方法

    python操作mysql中文顯示亂碼的解決方法

    這篇文章主要介紹了python操作mysql中文顯示亂碼的解決方法,是Python數(shù)據(jù)庫(kù)程序設(shè)計(jì)中經(jīng)常會(huì)遇到的問(wèn)題,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • Python統(tǒng)計(jì)序列和文件中元素的頻度

    Python統(tǒng)計(jì)序列和文件中元素的頻度

    這篇文章主要介紹了Python統(tǒng)計(jì)序列和文件中元素的頻度,文章基于python的相關(guān)資料展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值需要的小伙伴可以參考一下
    2022-04-04
  • Tensorflow 實(shí)現(xiàn)線性回歸模型的示例代碼

    Tensorflow 實(shí)現(xiàn)線性回歸模型的示例代碼

    這篇文章主要介紹了Tensorflow 實(shí)現(xiàn)線性回歸模型,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • 詳解Python中Pytest和Unittest的區(qū)別

    詳解Python中Pytest和Unittest的區(qū)別

    Pytest?和?Unittest是Python中屬于最常用的兩個(gè)測(cè)試框架。那么他們有些什么區(qū)別呢??Playwright?為什么只給了Pytest的深度支持,而不是Unittest呢?本文就來(lái)和大家詳細(xì)聊聊
    2023-03-03
  • python3實(shí)現(xiàn)常見(jiàn)的排序算法(示例代碼)

    python3實(shí)現(xiàn)常見(jiàn)的排序算法(示例代碼)

    排序是非常常見(jiàn)的排序算法,今天給大家分享幾種比較常見(jiàn)的排序算法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-07-07
  • python經(jīng)典練習(xí)百題之猴子吃桃三種解法

    python經(jīng)典練習(xí)百題之猴子吃桃三種解法

    這篇文章主要給大家介紹了關(guān)于python經(jīng)典練習(xí)百題之猴子吃桃三種解法的相關(guān)資料, Python猴子吃桃子編程是一個(gè)趣味性十足的編程練習(xí),在這個(gè)練習(xí)中,我們將要使用Python語(yǔ)言來(lái)模擬一只猴子吃桃子的過(guò)程,需要的朋友可以參考下
    2023-10-10
  • Python如何利用正則表達(dá)式爬取網(wǎng)頁(yè)信息及圖片

    Python如何利用正則表達(dá)式爬取網(wǎng)頁(yè)信息及圖片

    這篇文章主要給大家介紹了關(guān)于Python如何利用正則表達(dá)式爬取網(wǎng)頁(yè)信息及圖片的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評(píng)論