本文目录一览:
- 1、如何用Python编写代码在Word中实现带公式计算过程的计算书?
- 2、python操作word文档表格
- 3、python word文件处理
- 4、python操作word文档,用win32com,如何用python中的变量来命名word文档的文件名
- 5、python处理word文档
- 6、如何用python读取word
如何用Python编写代码在Word中实现带公式计算过程的计算书?
1、打开idle。点击file,然后点击new file 这是创建一个新的文件。新建...
答:1、打开idle。点击file,然后点击new file.这是创建一个新的文件。 新建一个文件之后,我们输入第一行代码,使用print函数,在屏幕上打印一句话,其中字符串要使用双引号,输入法要使用英文输入法,如果符号使用中文输入法输入,就会出现错误。p...
2020-11-17 回答者: 环球青藤 1个回答
python sympy中生成的公式怎么粘到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 For Word. On Windows, this API is split between MathP...
2017-10-03 回答者: 天天不看java 1个回答
如何用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) 程序缩进如图所示
python操作word文档表格
app=my.Office.Word.GetInstance()
doc=app.Documents[0]
print doc.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是一个代表斜向右上的底纹样式的常数
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()
python操作word文档,用win32com,如何用python中的变量来命名word文档的文件名
import sys, time, string, win32com.client, stat, os
class CWordAutomate:
"""封装word com 连接"""
def __init__( self ):
"""创建到word的ole连接对象"""
self.m_obWord = win32com.client.Dispatch( "Word.Application" )
self.m_obDoc = self.m_obWord.Documents.Add( ) #创建文档
self.m_obWord.Visible = 1
self.m_Sel = self.m_obWord.Selection # 获取选择对象
def WriteLine( self, sTxt, sFont, lSize, bBold=0 ):
"""写一行到文档"""
self.m_Sel.Font.Name = sFont
self.m_Sel.Font.Bold = bBold
self.m_Sel.Font.Size = lSize
self.m_Sel.TypeText( Text=sTxt + "/n" )
def Save(self, sFilename):
self.m_obDoc.SaveAs(sFilename)
def Quit(self):
self.m_obWord.Quit()
def file_test(file):
"""
测试用户提供的文件,查看它是否存在并包含数据。
如果输入文件不存在或是空的,则返回一个警告代码
"""
if (0 == os.path.isfile(file) or (0 == os.stat(file)[stat.ST_SIZE])):
return 1
else:
return 0
if __name__ == "__main__":
usage = "/n/n/tUsage: msword.py {inputfile} {outputfile}/n"
#
# 测试传入数字的参数
#
if len(sys.argv) != 3:
print "/n/n/tmsword.py error: /n/n/tInsufficient arguments passed."
print usage
sys.exit(1)
# 测试源文件是否存在并包含数据
if file_test(sys.argv[1]) == 1 :
print "/n/n/tmsword.py error: /n/n/tSource file not found or is empty."
print usage
sys.exit(1)
# 测试目标文件,避免意外导致崩溃
if file_test(sys.argv[2]) == 0 :
print "/n/n/tmsword.py error: /n/n/tTarget file already exists."
print usage
sys.exit(1)
sFileName = sys.argv[1]
obFile = file( sFileName, 'r+' )
sContent = obFile.read()
obFile.close()
lstContent = sContent.splitlines()
#
# 写入数据
#
obWord = CWordAutomate()
for sLine in lstContent:
obWord.WriteLine( sLine, "Courier New", 10 )
sLastMsg = time.strftime( "document generated on %c", time.localtime() )
obWord.WriteLine( sLastMsg, "Times New Roman", 14, 0 )
obWord.Save(sys.argv[2])
obWord.Quit()
python处理word文档
有个库叫『Python-docx』
安装之后 python 可以读写 word 文档,就可以拼接了。
如何用python读取word
使用Python的内部方法open()读取文本文件
try:
f=open('/file','r')
print(f.read())
finally:
if f:
f.close()
如果读取word文档推荐使用第三方插件,python-docx 可以在官网上下载
使用方式
# -*- coding: cp936 -*-
import docx
document = docx.Document(文件路径)
docText = '/n/n'.join([
paragraph.text.encode('utf-8') for paragraph in document.paragraphs
])
print docText