人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

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

發(fā)布日期:2021-12-11 00:06 | 文章來源:源碼之家

用 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("賈某人不學(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)是列表
# 是列表就開始循環(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("賈某人不學(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)是列表
# 是列表就開始循環(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)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部