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文件