本文实例讲述了Python操作word常见方法。分享给大家供大家参考,具体如下:
这里介绍两种方式:
- 使用win32com
- 使用docx
1. 使用win32com扩展包
只对windows平台有效
代码:
# coding=utf-8 import win32com from win32com.client import Dispatch, DispatchEx word = Dispatch('Word.Application') # 打开word应用程序 # word = DispatchEx('Word.Application') #启动独立的进程 word.Visible = 0 # 后台运行,不显示 word.DisplayAlerts = 0 # 不警告 path = 'G:/WorkSpace/Python/tmp/test.docx' # word文件路径 doc = word.Documents.Open(FileName=path, Encoding='gbk') # content = doc.Range(doc.Content.Start, doc.Content.End) # content = doc.Range() print '----------------' print '段落数: ', doc.Paragraphs.count # 利用下标遍历段落 for i in range(len(doc.Paragraphs)): para = doc.Paragraphs[i] print para.Range.text print '-------------------------' # 直接遍历段落 for para in doc.paragraphs: print para.Range.text # print para #只能用于文档内容全英文的情况 doc.Close() # 关闭word文档 # word.Quit #关闭word程序
2. 使用docx扩展包
优点:不依赖操作系统,跨平台
安装:
pip install python-docx
参考文档: https://python-docx.readthedocs.io/en/latest/index.html
代码:
import docx def read_docx(file_name): doc = docx.Document(file_name) content = '\n'.join([para.text for para in doc.paragraphs]) return content
创建表格
# coding=utf-8 import docx doc = docx.Document() table = doc.add_table(rows=1, cols=3, style='Table Grid') #创建带边框的表格 hdr_cells = table.rows[0].cells # 获取第0行所有所有单元格 hdr_cells[0].text = 'Name' hdr_cells[1].text = 'Id' hdr_cells[2].text = 'Desc' # 添加三行数据 data_lines = 3 for i in range(data_lines): cells = table.add_row().cells cells[0].text = 'Name%s' % i cells[1].text = 'Id%s' % i cells[2].text = 'Desc%s' % i rows = 2 cols = 4 table = doc.add_table(rows=rows, cols=cols) val = 1 for i in range(rows): cells = table.rows[i].cells for j in range(cols): cells[j].text = str(val * 10) val += 1 doc.save('tmp.docx')
读取表格
# coding=utf-8 import docx doc = docx.Document('tmp.docx') for table in doc.tables: # 遍历所有表格 print '----table------' for row in table.rows: # 遍历表格的所有行 # row_str = '\t'.join([cell.text for cell in row.cells]) # 一行数据 # print row_str for cell in row.cells: print cell.text, '\t', print
相关样式参考: https://python-docx.readthedocs.io/en/latest/user/styles-understanding.html
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- 魔兽世界祖尔格拉布在哪 魔兽世界祖尔格拉布位置介绍
- 群星《名列前茅五大顶级发烧男声》2CD[DTS-WAV]
- 群星《经典再现·国语女声》2CD[DTS-WAV]
- 群星《十大民歌天后》2CD[DTS-WAV分轨]
- 《黑神话:悟空》浮屠牢是最糟糕的部分:但也是做得最好的
- 《艾诺提亚:失落之歌》PC版反向跳票!9月17日PC开玩
- 老外谈《黑神话:悟空》:在我心里10分 但它确实是8分游戏
- 陈秋霞.1977-温馨在我心(LP版)【歌林】【WAV+CUE】
- 张秀卿.2019-感情骗子【豪记】【WAV+CUE】
- 林隆璇.1993-收藏昨日【福茂】【WAV+CUE】
- 魔兽世界wlk德拉诺金钻如何获得 wlk德拉诺金钻获取方法介绍
- 魔兽世界wlk祖阿曼战熊如何获得 wlk祖阿曼战熊获取方式介绍
- 魔兽世界阿奴巴拉克有什么背景故事 阿奴巴拉克背景故事介绍
- NeilYoung-EarlyDaze(2024)[24Bit]WAV
- NeaveTrio-Rooted(2024)[24Bit-WAV]