>>>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.什么是域
域应用基础
>>>
@staticmethoddefGetInstance():
u'''获取Word应用程序的Application对象'''
importwin32com.client
returnwin32com.client.Dispatch('Word.Application')
my.Office.Word.GetInstance的方法实现如上,是一个使用win32com操纵Word Com的接口的封装
所有Paragraph即段落对象,都是通过Paragraph.Range.Text来访问它的文字的
python如何获取word文件中某个关键字之后的表格
最好是全部都读取到程序中,在程序中进行判断。
本文实例讲述了Python实现批量读取word中表格信息的方法。分享给大家供大家参考。具体如下:
单位收集了很多word格式的调查表,领导需要收集表单里的信息,我就把所有调查表放一个文件里,写了个python小程序把所需的信息打印出来
#coding:utf-8
import os
import win32com
from win32com.client import Dispatch, constants
from docx import Document
def parse_doc(f):
"""读取doc,返回姓名和行业
"""
doc = w.Documents.Open( FileName = f )
t = doc.Tables[0] # 根据文件中的图表选择信息
name = t.Rows[0].Cells[1].Range.Text
situation = t.Rows[0].Cells[5].Range.Text
people = t.Rows[1].Cells[1].Range.Text
title = t.Rows[1].Cells[3].Range.Text
print name, situation, people,title
doc.Close()
def parse_docx(f):
"""读取docx,返回姓名和行业
"""
d = Document(f)
t = d.tables[0]
name = t.cell(0,1).text
situation = t.cell(0,8).text
people = t.cell(1,2).text
title = t.cell(1,8).text
print name, situation, people,title
if __name__ == "__main__":
w = win32com.client.Dispatch('Word.Application')
# 遍历文件
PATH = "H:\work\\aaa" # windows文件路径
doc_files = os.listdir(PATH)
for doc in doc_files:
if os.path.splitext(doc)[1] == '.docx':
try:
parse_docx(PATH+'\\'+doc)
except Exception as e:
print e
elif os.path.splitext(doc)[1] == '.doc':
try:
parse_doc(PATH+'\\'+doc)
except Exception as e:
print e
希望本文所述对大家的Python程序设计有所帮助。
python 如何读取复杂文件? 比如我有个配置文件, 格式为 “#文本内容 配置参数“ 这样的格式 该怎么读取
f=open("config.ini")
for line in f:
if line[0]=="#":
continue
elif line[0]==";":
continue
else:
print line
忽略掉#和;开头的行就可以得配置参数了。更多
还有个问题 就是,我读取的文件是Open***的配置文件,文件名是client.o***
读取的时候老是出错,请问需要怎样的步骤才能正确读取。
PS:我是pyhton新手不好意思,问题多了点
出错,你要说出错的信息。
贴上client.o***内容。
读取文件已经没问题了,但是现在读取这个文件会出现问题,我把文件内容发给你看看
# Silence repeating messages
;mute 20
redirect-gateway
route-method exe
route-delay 2
他读取的是最后一个route-delay 2
而我想要读取的是那个redirect-gateway
python怎么读取比较大的json文件
你的比较大是有多大,大到内存里放不下?
# -*- coding:utf-8 -*-
import json
import sys
import re
import traceback
reload(sys)
sys.setdefaultencoding("utf-8")
try:
f = file(r'XXX.json')
jsonobj = json.load(f)
for i in range(len(jsonobj)):
for key in jsonobj[i]:
print key,jsonobj[i][key]
except:
traceback.print_exc()
用python读取文本文件,对读出的每一行进行操作,这个怎么写?
把你的代码改一下
f=open("test.txt","r")whileTrue:
line=f.readline()
ifline:
pass#dosomethinghere
line=line.strip()
p=line.rfind('.')
filename=line[0:p]
print"create%s"%line
else:
break
f.close()
写得可能罗嗦些,就是方便看。
Python 读取文件夹将里面的图片处理成想要的大小并保存在个指定位置
fromPILimportImageimportos.path
importglob
defconvertjpg(jpgfile,outdir,width=1280,height=720):
img=Image.open(jpgfile)
new_img=img.resize((width,height),Image.BILINEAR)
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
forjpgfileinglob.glob("D:/python/*.jpg"):
convertjpg(jpgfile,"D:/newfile")
convertjpg调用时可以有四个参数,如convertjpg(jpgfile,"D:/newfile",800,600)
Image open了jpg用完后要不要close?
传了4个参数,但是现在报TypeError: an integer is required?
width = sys.argv[3] height = sys.argv[4]
new_img=img.resize((width,height),Image.BILINEAR)
命令行是c:>image.py D:\python\*.jpg D:\python\1\ 1280 720
python按行读取文件,如何去掉换行符"\n
说明:
python按行读取文件直接使用for in open方式即可,去除'\n',可以使用两种方法,一种是替换函数:replace('\n','')把换行符替换成空;一种是去除空白字符函数:rstrip('\n')就是把字符串末尾指定字符删除,这里删除换行符('\n')。
代码:
方法1:
forlineinopen('要打开的文件名','r'):#打开文件rs=line.replace('\n','')#替换换行符
print(rs)#显示替换后的行
方法2:
forlineinopen('要打开的文件名','r'):#打开文件rs=line.rstrip('\n')#移除行尾换行符
print(rs)#输出移除后的行
函数说明:
replace(...)
S.replace(old, new[, count]) -> str
返回S出现的所有old子串的换成new子串的字符串。如果可选参数count指定,只有第一计数出现被替换。
rstrip(...)
S.rstrip([chars]) -> str
返回字符串S结尾去掉空格的副本。如果参数字符给出,则只删除指定字符,而不是空格。
python 如何读取大文件
你可以不用一次读完存到内存里,可以一行一行的读,用.readline()读一行处理一行,这样不会占很多内存,多大的文件都没关系
转载请注明出处51数据库 » python读取word格式文件大小 python如何读取word文件
Photoshop|
Dreamweaver|
SVG|
WebGL|
Visual Studio|
PowerDesigner|
Eclipse|
Git|
Apache Ant|
Atom|
Composer|
CodeSmith|
Flex|
Gradle|
Maven|
Sublime Text3|
SVN|
Tableau|
Vim|
Chrome开发者工具|
OpenGL|
Unity|
Direct3D|
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....