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

python操作word文檔

發布時間: 2022-04-24 03:04:13

A. python能打開word文檔嗎

首先下載安裝win32com

from win32com import client as wc
word = wc.Dispatch('Word.Application')
doc = word.Documents.Open('c:/test')
doc.SaveAs('c:/test.text', 2)
doc.Close()
word.Quit()

這種方式產生的text文檔,不能用python用普通的r方式讀取,為了讓python可以用r方式讀取,應當寫成

doc.SaveAs('c:/test', 4)

注意:系統執行完成後,會自動產生文件後綴txt(雖然沒有指明後綴)。
在xp系統下面,應當,

open(r'c:\text','r')
wdFormatDocument = 0
wdFormatDocument97 = 0
wdFormatDocumentDefault = 16
wdFormatDOSText = 4
wdFormatDOSTextLineBreaks = 5
wdFormatEncodedText = 7
wdFormatFilteredHTML = 10
wdFormatFlatXML = 19
wdFormatFlatXMLMacroEnabled = 20
wdFormatFlatXMLTemplate = 21
= 22
wdFormatHTML = 8
wdFormatPDF = 17
wdFormatRTF = 6
wdFormatTemplate = 1
wdFormatTemplate97 = 1
wdFormatText = 2
wdFormatTextLineBreaks = 3
wdFormatUnicodeText = 7
wdFormatWebArchive = 9
wdFormatXML = 11
wdFormatXMLDocument = 12
= 13
wdFormatXMLTemplate = 14
= 15
wdFormatXPS = 18

照著字面意思應該能對應到相應的文件格式,如果你是office
2003可能支持不了這么多格式。word文件轉html有兩種格式可選wdFormatHTML、wdFormatFilteredHTML(對應數字
8、10),區別是如果是wdFormatHTML格式的話,word文件裡面的公式等ole對象將會存儲成wmf格式,而選用
wdFormatFilteredHTML的話公式圖片將存儲為gif格式,而且目測可以看出用wdFormatFilteredHTML生成的HTML
明顯比wdFormatHTML要干凈許多。

當然你也可以用任意一種語言通過com來調用office API,比如PHP.

from win32com import client as wc
word = wc.Dispatch('Word.Application')
doc = word.Documents.Open(r'c:/test1.doc')
doc.SaveAs('c:/test1.text', 4)
doc.Close()

import re
strings=open(r'c:\test1.text','r').read()
result=re.findall('\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)|\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)',strings)
chan=re.sub('\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)|\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)','()',strings)
question=open(r'c:\question','a+')
question.write(chan)
question.close()
answer=open(r'c:\answeronly','a+')
for i,a in enumerate(result):
m=re.search('[A-D]',a)
answer.write(str(i+1)+' '+m.group()+'\n')
answer.close()
chan=re.sub(r'\xa3\xa8\s*[A-D]\s*\xa3\xa9','()',strings)
#不要(),容易引起歧義。

B. 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()

C. python如何讀取word文件中的文本內容並寫入到新的txt文件

D. python操作word文檔表格

>>>app=my.Office.Word.GetInstance()
>>>doc=app.Documents[0]
>>>printdoc.Name
VBA工具集.doc
>>>doc.Tables.Count
2
>>>table=doc.Tables[1]
>>>table.Cell(1,1).Select()
>>>app.Selection.MoveEnd(Unit=12,Count=4)
4
>>>app.Selection.Cells.Shading.Texture=-10
>>>

1.my.Office.Word.GetInstance()用win32com得到Word的Application對象的實例

2.我所使用的樣本word文件中包含兩個Table第二個Table是想要修改的

3.table.Cell(1,1).Select()用於選中這個樣表的第一個單元格

4.app.Selection.MoveEnd用於獲得向右多選取4個單元格,wdCell=12,用於指示按單元格移動

5.app.Selection.Cells.Shading.Texture = -10用於執行陰影底紋的設置工作,wdTextureDiagonalUp=-10是一個代表斜向右上的底紋樣式的常數

E. 如何用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) 程序縮進如圖所示

F. 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')
  1. my.Office.Word.GetInstance的方法實現如上,是一個使用win32com操縱Word Com的介面的封裝

  2. 所有Paragraph即段落對象,都是通過Paragraph.Range.Text來訪問它的文字的

G. 如何用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

H. python操作word文檔,如何合並單元格

>>>app=my.Office.Word.GetInstance()
>>>doc=app.Documents[0]
>>>table=doc.Tables[1]
>>>table.Cell(1,1).Select()
>>>app.Selection.MoveDown(Unit=5,Count=2,Extend=1)
>>>app.Selection.Cells.Merge()
>>>
  1. my.Office.Word.GetInstance()用win32com得到Word的Application對象的實例

  2. 我所使用的樣本word文件中包含兩個Table第二個Table是想要修改的

  3. table.Cell(1,1).Select()用於選中這個樣表的第一個單元格

  4. app.Selection.MoveDown用於獲得向下多選取3個單元格

  5. app.Selection.Cells.Merge()用於執行合並工作

I. python word文件處理

#-*- encoding: utf8 -*-
import win32com
from win32com.client import Dispatch, constants
import win32com.client
import __main__
import os
import new
import sys
import re
import string
reload(sys)
sys.setdefaultencoding('utf8')
#from fileinput import filename

class Word(object):
#初始化word對象
def __init__(self, uri):
self.objectword(uri)

#創建word對象
def objectword(self,url):
self.word = win32com.client.Dispatch('Word.Application')
self.word.Visible = 0
self.word.DisplayAlerts = 0

self.docx = self.word.Documents.Open(url)
self.wrange = self.docx.Range(0, 0)

#關閉word
def close(self):
self.word.Documents.Close()
self.word.Quit()
#創建word
def create(self):

pass
#在word中進行查找
def findword(self, key):
question = []
uri = r'E:\XE\ctb.docx'
self.objectword(uri)
#讀取所有的word文檔內容
range = self.docx.Range(self.docx.Content.Start,self.docx.Content.End)
question = str(range).split("&")
#查找內容
#question = re.split(r"(\r[1][0-9][0-9]+.)",str(range))
#l = question[0].split("\d+.")
for questionLine in question:
questionLine = questionLine.strip('\n')
l = re.split(r"([1][0-9][0-9]+.)",questionLine)
del l[0]
for t in l:
s = str(key[0:3])
if str(t).find(s) > -1:
#插入
g = string.join(l)

print g.encode('gb2312')
#print g.decode("")
self.insertword(g)
print "sss"
else:
print "ttt"

#插入word
def insertword(self,w):
url = r'E:\XE\ctb.doc'
self.objectword(url)
self.wrange.InsertAfter(w)
pass

#讀取數據源
def source(self, src):
f = open(src)
d = f.readlines()
for l in d:
name, question01, question02, question03, question04, question05 = tuple(l.decode('utf8').split('\t'))
if question01 != u'全對':
#self.wrange.InsertAfter(name)
self.findword(question01)
return self

Word(r'E:\XE\xx.docx').source(r'E:\XE\xe.txt').close()

J. python處理word文檔

有個庫叫『Python-docx』
安裝之後 python 可以讀寫 word 文檔,就可以拼接了。

熱點內容
我配置很高了ae為什麼卡 發布:2025-05-17 14:54:50 瀏覽:166
python數據分析實戰pdf 發布:2025-05-17 14:49:42 瀏覽:950
海瀾之家廣告腳本 發布:2025-05-17 13:56:06 瀏覽:30
手文件夾恢復 發布:2025-05-17 13:53:32 瀏覽:993
linux怎麼看進程 發布:2025-05-17 13:53:30 瀏覽:303
thinkphp欄位緩存 發布:2025-05-17 13:52:01 瀏覽:575
山靈app安卓版如何設置 發布:2025-05-17 13:51:49 瀏覽:388
帆布壓縮袋 發布:2025-05-17 13:26:27 瀏覽:457
c語言16進製表示方法 發布:2025-05-17 13:11:25 瀏覽:480
ftp單位 發布:2025-05-17 13:10:03 瀏覽:142