小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

編程語言Python 操作 Word

 冒險(xiǎn)的K 2021-11-30

Python 操作 Word

用 docx 模塊讀取 Word

docx 安裝

cmd 中輸入pip install python-docx 即可安裝 docx 模塊

docx 常用函數(shù)

創(chuàng)建空白文檔

from docx import Document

document = Document()document.save("word.docx")  # 生成空白 wordprint(document)

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-Xo1siDkz-1637387517723)(python辦公自動(dòng)化.assets/image-20211119225443886.png)]

讀取文檔

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

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-dST0hckl-1637387517728)(python辦公自動(dòng)化.assets/image-20211119225308276.png)]

獲取文檔段落

from docx import Document

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

在這里插入圖片描述

Word 調(diào)整樣式

from docx import Documentfrom docx.shared import Pt, RGBColor

document = Document()  # 讀取現(xiàn)有的 word 建立文檔對象# 二、寫入內(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對象是相同樣式文本的延續(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 = Truerun1.font.underline = Truerun1.font.italic = True# # 三、保存文件document.save("word.docx")all_paragraphs = document.paragraphs# print(type(all_paragraphs))#,打印后發(fā)現(xiàn)是列表# 是列表就開始循環(huán)讀取dfor paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的樣式名稱
    # 打印每一個(gè)段落的文字
    print(paragraph.text)
    # 循環(huán)讀取每個(gè)段落里的run內(nèi)容
    # for run in paragraph.runs:
    # print(run.text)  # 打印run內(nèi)容

在這里插入圖片描述

Word 寫入操作

from docx import Documentfrom docx.shared import Pt, RGBColor

document = Document()  # 讀取現(xiàn)有的 word 建立文檔對象# 二、寫入內(nèi)容document.add_heading("python 操作 Word")# 段落p1 = document.add_paragraph("早睡早起?。。?quot;)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對象是相同樣式文本的延續(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 = Truerun1.font.underline = Truerun1.font.italic = True# # 三、保存文件document.save("word.docx")all_paragraphs = document.paragraphs# print(type(all_paragraphs))#,打印后發(fā)現(xiàn)是列表# 是列表就開始循環(huán)讀取dfor paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的樣式名稱
    # 打印每一個(gè)段落的文字
    print(paragraph.text)
    # 循環(huán)讀取每個(gè)段落里的run內(nèi)容
    # for run in paragraph.runs:
    # print(run.text)  # 打印run內(nèi)容

在這里插入圖片描述

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多