當前位置:首頁 » 編程語言 » python操作word

python操作word

發布時間: 2022-12-16 00:44:32

① 如何用python讀取word

使用Python的內部方法open()讀取文本文件

try:
f=open('/file','r')
print(f.read())
finally:
iff:
f.close()

如果讀取word文檔推薦使用第三方插件,python-docx 可以在官網上下載

使用方式

#-*-coding:cp936-*-
importdocx
document=docx.Document(文件路徑)
docText=' '.join([
paragraph.text.encode('utf-8')forparagraphindocument.paragraphs
])
printdocText

② python怎麼在word表中插圖片

# -*- coding: UTF8 -*-from docx import Documentfrom docx.shared import Pt doc = Document() # 文件存儲路徑path = "C:\\Users\\Administrator\\Desktop\\word文檔\\" # 讀取文檔# doc = Document(path + "hello.docx") # 添加圖片,後面的參數設置圖片尺寸,可以選填doc.add_picture(path + 'cat.jpg', width=Pt(300))

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:547
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:832
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:536
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:716
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:639
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:954
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:210
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:68
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:760
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:665