Python操作word常见方法示例【win32com与docx模块】

作者:wanlifeipeng 时间:2022-09-22 13:12:33 

本文实例讲述了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程序设计有所帮助。

来源:https://www.cnblogs.com/hupeng1234/p/6920129.html

标签:Python,word,win32com,docx
0
投稿

猜你喜欢

  • 王孟友教你如何设计标志(LOGO)

    2008-04-17 13:30:00
  • 交互设计师应该具备哪些素质

    2009-03-12 12:21:00
  • 如何不通过DSN访问SQL Server?

    2009-11-11 19:21:00
  • Js实现简单的小球运动特效

    2023-07-08 23:40:40
  • Thinkphp5文件包含漏洞解析

    2023-07-01 19:42:51
  • python实现图片文件批量重命名

    2023-08-10 03:39:57
  • 三谈Iframe自适应高度

    2010-08-03 13:04:00
  • mysql 错误:ERROR 1045 (28000): Access deni

    2010-09-30 14:48:00
  • 悟道Web标准:让W3C标准兼容终端

    2009-10-11 16:40:00
  • 简单解读面包屑

    2009-06-09 14:16:00
  • js图片随机显示技巧

    2007-08-19 20:20:00
  • php实现网站留言板功能

    2023-11-23 21:06:36
  • 品牌的统一体验

    2010-05-19 13:08:00
  • MYSQL初学者扫盲

    2009-02-27 13:15:00
  • ASP 获取腾讯IP地址的代码

    2011-02-26 11:19:00
  • 使用PHP批量生成随机用户名

    2023-07-22 13:10:10
  • 实现php删除链表中重复的结点

    2023-09-05 09:36:15
  • PHP const定义常量及global定义全局常量实例解析

    2023-11-17 07:24:57
  • asp+jsp+JavaScript动态实现添加数据行

    2023-07-03 05:37:15
  • 用ASP+CSS实现随机背景

    2007-09-26 12:33:00
  • asp之家 网络编程 m.aspxhome.com