使用python读取word,写入execl
word里面有2张表,需要找到第二张表,并写入execl中:
代码如下:
#coding:utf-8
import os
from docx import Document
import win32com
from win32com.client import Dispatch, constants def parse_docx(f,title):
d = Document(f)
for t in d.tables:
'''获取需要的表'''
tbTitle = t.cell(0, 0).text
if title == tbTitle:
tableInfo = []
columnLen = len(t.columns)
rowLen = len(t.rows)
for i in range(0,columnLen):
tmp = []
for row in t.rows:
tmp.append(row.cells[i].text)
#删除第一个元素->表名
del(tmp[0])
tableInfo.append(tmp)
#返回的后两个参数表示tableInfo表的行数和列数
return [tbTitle,tableInfo,rowLen-1,columnLen]
return None def writeExecl(fileName,sheet,tableInfo):
excel = win32com.client.Dispatch('Excel.Application')
excel.Visible=0
excel.DisplayAlerts=0
#对传入文件名的处理
if fileName:
if os.path.exists(fileName):
workbook = excel.Workbooks.Open(fileName)
else:
workbook = excel.Workbooks.Add()
workbook.SaveAs(fileName)
else:
workbook = excel.Workbooks.Add() try:
sht = workbook.Worksheets(sheet)
except:
sheetNew = workbook.Worksheets.Add()
sheetNew.Name =sheet
sheetNew.Activate()
sht = workbook.Worksheets(sheet)
#execl表格是从1开始的
sht.Cells(1, 1).Value = tableInfo[0]
#把tableInfo看作是一行数据,依次赋值
for i in range(0,tableInfo[3]):
for j in range(0,tableInfo[2]):
sht.Cells(j+2, i+1).Value = tableInfo[1][i][j] workbook.Save()
excel.Application.Quit() if __name__ == "__main__":
docxFile = "123.docx"
execlFile = "roro.xlsx"
sheet = "roro" tableName = "内科"
#读取word中tableName的内容
tableInfo = parse_docx(docxFile,tableName) #处理execl
writeExecl(execlFile,sheet,tableInfo)
运行后生成文件 roro.xlsx,内容如下:

使用python读取word,写入execl的更多相关文章
- python读取与写入csv,txt格式文件
python读取与写入csv,txt格式文件 在数据分析中经常需要从csv格式的文件中存取数据以及将数据写书到csv文件中.将csv文件中的数据直接读取为dict类型和DataFrame是非常方便也很 ...
- 使用python读取word
使用python读取word 官网:https://python-docx.readthedocs.io/en/latest/ 示例:https://blog.csdn.net/u010911997/ ...
- Python读取word文档内容
1,利用python读取纯文字的word文档,读取段落和段落里的文字. 先读取段落,代码如下: 1 ''' 2 #利用python读取word文档,先读取段落 3 ''' 4 #导入所需库 5 fro ...
- Python读取word文档(python-docx包)
最近想统计word文档中的一些信息,人工统计的话...三天三夜吧 python 不愧是万能语言,发现有一个包叫做 docx,非常好用,具体查看官方文档:https://python-docx.read ...
- python读取word文档
周末需要做一个统计word文档字数的问题,刚开始以为很简单,因为之前做过excel表格相关的任务,所以认为利用扩展模块应该比较简单. 通过搜索,确实搜到了一个python操作word的模块,pytho ...
- 用python读取word文件里的表格信息【华为云技术分享】
在企查查查询企业信息的时候,得到了一些word文件,里面有些控股企业的数据放在表格里,需要我们将其提取出来. word文件看起来很复杂,不方便进行结构化.实际上,一个word文档中大概有这么几种类型的 ...
- Python读取和写入文件
1 从文件中读取数据 1.1 读取整个文件 创建名为test的txt文本文件,添加内容如下所示: 123456789023456789013456789012 实现代码: with open('tes ...
- python读取word表格内容(1)
1.首页介绍下word表格内容,实例如下: 每两个表格后面是一个合并的单元格
- python读取并写入mat文件
用matlab生成一个示例mat文件: clear;clc matrix1 = magic(5); matrix2 = magic(6); save matData.mat 用python3读取并写入 ...
随机推荐
- R语言-时间序列
时间序列:可以用来预测未来的参数, 1.生成时间序列对象 sales <- c(18, 33, 41, 7, 34, 35, 24, 25, 24, 21, 25, 20, 22, 31, 40 ...
- python>oop
oop即Object Oriented programming,面向对象编程 何为编程:特定的语法+数据结构+算法 编程范式就是从经验中总结的套路: 面向过程编程 适用于简单的脚本,一次任务是极好的, ...
- ubuntu16.04卸载软件
root@test:/# dpkg -l | grep cobbler root@test:/# sudo dpkg --purge cobbler
- spring中自定义Event事件的使用和浅析
在我目前接触的项目中,用到了许多spring相关的技术,框架层面的spring.spring mvc就不说了,细节上的功能也用了不少,如schedule定时任务.Filter过滤器. intercep ...
- 引导加载程序之争: LILO 和 GRUB
在不考虑他们的工作或专业情况下,所有 Linux 用户都会使用的是哪个工具?引导加载程序.通过本文了解引导加载程序的工作原理,认识两个流行的引导加载程序 LILO(LInux LOader)和 GNU ...
- USB转串口驱动代码分析
1.USB插入时,创建设备 [plain] view plaincopy DriverObject->DriverExtension->AddDevice = USB2COM_PnPAdd ...
- Learning part-based templates from large collections of 3D shapse CorrsTmplt Kim 代码调试
平台: VMware上装的Ubuntu-15.10 环境准备工作:装Fortran, lapack, blas, cblas (理论上装好lapack后面两个应该是自动的),其他的有需要的随时安装就可 ...
- Go基础之--操作Mysql(三)
事务是数据库的一个非常重要的特性,尤其对于银行,支付系统,等等.database/sql提供了事务处理的功能.通过Tx对象实现.db.Begin会创建tx对象,后者的Exec和Query执行事务的数据 ...
- python 实现多层目录文件查找
本文针对多层目录下文件的查找. 利用 os模块的基本操作,并利用递归的思想实现了目录多层查找. 代码如下: import os #dir_name: 处理文件的起始目录 def count_file( ...
- iOS 双击tabbar刷新页面
/*在继承UITabBarController控制器中*/ #pragma mark <UITabBarControllerDelegate> -(void)tabBarControlle ...