1. python讀取已經打開的3個word和excle文件的路徑
使用os.path.abspath()函數來獲取文件絕對路徑
文件目錄結構如下:
2. 如何用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
3. 如何用Python編寫代碼在Word中實現帶公式計算過程的計算書
1、打開idle。點擊file,然後點擊new file 這是創建一個新的文件。新建...
答:1、打開idle。點擊file,然後點擊new file.這是創建一個新的文件。 新建一個文件之後,我們輸入第一行代碼,使用print函數,在屏幕上列印一句話,其中字元串要使用雙引號,輸入法要使用英文輸入法,如果符號使用中文輸入法輸入,就會出現錯誤。p...
2020-11-17回答者:環球青藤1個回答
pythonsympy中生成的公式怎麼粘到word里
答:右鍵選 show math as→mathML Code 全選復制,在word中右鍵以文本形式粘貼
2018-01-27回答者:夜歌在路上2個回答
如何使用python提取並處理word文檔中插入的mathtyp...
答:我沒做過,只能提供大概思路給你。這是mathtype的SDK:,裡面關於API的描述: MathType API Documentation The MathType API allows you to call functions used by the MathType Commands ForWord. On Windows, this API is split between MathP...
2017-10-03回答者:天天不看java1個回答
如何用python寫這個代碼
問:使用兩個參數定義一個名為dictionaryToListOfValues的函數。 此函數的第...
答:使用Python自帶的IDLE 在開始-->程序-->Python2.5(視你安裝的版本而不同)中找到IDLE(Python GUI)。 點擊後彈出如下窗體: 1,在>>>提示符後輸入代碼,回車,就可以執行此代碼。 IDLE支持語法高亮,支持自動縮進,支持方法提示,不過提示的很慢。...
2019-10-17回答者:司馬刀劍2個回答3
如何用python編寫計算器
答:我想你的需求應該是一個圖形界面的程序,而不是簡單的在命令行上輸入。 那麼,要做的第一件事就是選擇一個圖形界面套件。可以使用原生的TK,也可以用跨平台性能很好的wxPython,或者是整體結構很像MFC的PyWin32。至於pyGTK,pyQT,都是可選的,但...
2011-05-30回答者:碧藍右耳3個回答6
用Python編寫代碼1×2×3+4×5×6+7×8×9+++···99×100×1...
答:t=1 for i in range(1,102): t*=i print(t)
2020-04-05回答者:知道網友1個回答2
python如何識別docx中的公式
答:import fnmatch, os, sys, win32com.client readpath=r'D:123'wordapp = win32com.client.gencache.EnsureDispatch("Word.Application") try: for path, dirs, files in os.walk(readpath): for filename in files: if not fnmatch.fnmatch(fi...
2016-07-09回答者:知道網友1個回答2
如何用Python代碼運行Word中的VBA
問:請問有什麼好的辦法用Python代碼運行Word中的VBA嗎, 具體需要import哪...
答:安裝pypiwin32 import win32com.client app= win32com.client.Dispatch("word.Application") app.Workbooks.Open("宏代碼所在文件路徑") app.Application.Run("宏名稱") app.Application.Quit()
2019-08-31回答者:娘化的新世界1個回答
用Python寫一個,兩個數的加,減,乘,除的函數,...
答:我課程中的部分代碼(除沒寫): def f_add(a,b): return a+bdef f_mul(a,b): return a*bdef f_sub(a,b): return a-b def g1(f,a,b): return f(a,b)a,b,c,d = 1,2,3,4print g1(f_sub, g1(f_mul, g1(f_add,a,b), c), d), g1(f_mul, g1(f_add,a,b)...
2017-11-21回答者:黑板客1個回答4
python編寫2個函數代碼,實現求最小公倍數和最大公...
問:使用兩個函數實現,最小公倍數和最大公約數
答:def gcd(a, b): # 求最大公約數 x = a % b while (x != 0): a, b = b, x x = a % b return bdef lcm(a,b): # 求最小公倍數 return a*b//gcd(a,b) 程序縮進如圖所示
4. word圖片和文字文混排內容怎麼用python讀取寫入
Python可以利用python-docx模塊處理word文檔,處理方式是面向對象的。也就是說python-docx模塊會把word文檔,文檔中的段落、文本、字體等都看做對象,對對象進行處理就是對word文檔的內容處理。
二,相關概念
如果需要讀取word文檔中的文字(一般來說,程序也只需要認識word文檔中的文字信息),需要先了解python-docx模塊的幾個概念。
1,Document對象,表示一個word文檔。
2,Paragraph對象,表示word文檔中的一個段落
3,Paragraph對象的text屬性,表示段落中的文本內容。
三,模塊的安裝和導入
需要注意,python-docx模塊安裝需要在cmd命令行中輸入pip install python-docx,如下圖表示安裝成功(最後那句英文Successfully installed,成功地安裝完成,十分考驗英文水平。)
注意在導入模塊時,用的是import docx。
也真是奇了怪了,怎麼安裝和導入模塊時,很多都不用一個名字,看來是很有必要出一個python版本的模塊管理程序python-maven了,本段純屬PS。
四,讀取word文本
在了解了上面的信息之後,就很簡單了,下面先創建一個D:\temp\word.docx文件,並在其中輸入如下內容。
然後寫一段程序,代碼及輸出結果如下:
#讀取docx中的文本代碼示例
import docx
#獲取文檔對象
file=docx.Document("D:\\temp\\word.docx")
print("段落數:"+str(len(file.paragraphs)))#段落數為13,每個回車隔離一段
#輸出每一段的內容
for para in file.paragraphs:
print(para.text)
#輸出段落編號及段落內容
for i in range(len(file.paragraphs)):
print("第"+str(i)+"段的內容是:"+file.paragraphs[i].text)
運行結果:
================ RESTART: F:/360data/重要數據/桌面/學習筆記/readWord.py ================
段落數:13
啊
我看見一座山
雄偉的大山
真高啊
啊
這座山是!
真的很高!
第0段的內容是:啊
第1段的內容是:
第2段的內容是:我看見一座山
第3段的內容是:
第4段的內容是:雄偉的大山
第5段的內容是:
第6段的內容是:真高啊
第7段的內容是:
第8段的內容是:啊
第9段的內容是:
第10段的內容是:這座山是!
第11段的內容是:
第12段的內容是:真的很高!
>>>
總結
以上就是本文關於Python讀取word文本操作詳解的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
5. python處理word文檔
有個庫叫『Python-docx』
安裝之後 python 可以讀寫 word 文檔,就可以拼接了。
6. 如何使用python讀取word的表格並輸出為字典
直接讀取value寫入csv文件,
import csv
f = open('file.csv','a',newline='')
w = writer(f)
w.writerow(dict(key))
打開csv文件另存為excel.
如果是很多個字典組成的列表,形式像[{a:1,b:2,c:3},……{a:4,b:5,c:6}],就可以用pandas來進行處理,存儲為excel, 表頭為a,b,c
dict_l = [{a:1,b:2,c:3},……{a:4,b:5,c:6}]
from pandas import DataFrame as DF
df = DF(dict_l)
df.to_csv(filename)
7. python如何讀取word文件
>>>defPrintAllParagraphs(doc):
count=doc.Paragraphs.Count
foriinrange(count-1,-1,-1):
pr=doc.Paragraphs[i].Range
printpr.Text
>>>app=my.Office.Word.GetInstance()
>>>doc=app.Documents[0]
>>>PrintAllParagraphs(doc)
1.什麼是域
域應用基礎
>>>
@staticmethod
defGetInstance():
u'''獲取Word應用程序的Application對象'''
importwin32com.client
returnwin32com.client.Dispatch('Word.Application')
my.Office.Word.GetInstance的方法實現如上,是一個使用win32com操縱Word Com的介面的封裝
所有Paragraph即段落對象,都是通過Paragraph.Range.Text來訪問它的文字的
8. python讀取word文檔內容
import fnmatch, os, sys, win32com.client
readpath=r'D:\123'
wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")
try:
for path, dirs, files in os.walk(readpath):
for filename in files:
if not fnmatch.fnmatch(filename, '*.docx'):continue
doc = os.path.abspath(os.path.join(path,filename))
print 'processing %s...' % doc
wordapp.Documents.Open(doc)
docastext = doc[:-4] + 'txt'
wordapp.ActiveDocument.SaveAs(docastext,FileFormat=win32com.client.constants.wdFormatText)
wordapp.ActiveDocument.Close()
finally:
wordapp.Quit()
print 'end'
f=open(r'd:\123\test.txt','r')
for line in f.readlines():
print line.decode('gbk')
f.close()
9. 求助大神:如何用Python docx解析一個Word文檔,在某些欄位處插入文本或表格,更換頁眉頁腳等急~
from docx import Document
from docx.shared import Inches
document = Document()
document.add_heading('Document Title', 0)
p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True
document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='IntenseQuote')
document.add_paragraph(
'first item in unordered list', style='ListBullet'
)
document.add_paragraph(
'first item in ordered list', style='ListNumber'
)
document.add_picture('monty-truth.png', width=Inches(1.25))
table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for item in recordset:
row_cells = table.add_row().cells
row_cells[0].text = str(item.qty)
row_cells[1].text = str(item.id)
row_cells[2].text = item.desc
document.add_page_break()
document.save('demo.docx')
這是一個demo for docx 你可以試試
10. python如何讀取word文件中的文本內容並寫入到新的txt文件