【script】python3中的docx、win32com和openpyxl模块

docx模块:对word文档的处理

读取docx文件

from docx import Document   # word处理模块

path = r"C:\Users\30797\Desktop\python\docx.docx"
document = Document(path)   # 读取docx文件

处理段落(paragraphs)

1、遍历输出段落

paragraphs = document.paragraphs
for paragraph in paragraphs:    # 遍历所有段落
    print(paragraph.text)

2、读取第二段样式

runs = paragraphs[1].runs    # 读取第二段所有样式
for run in runs:    # 遍历输出所有样式
    print(run.text)

3、将 "Jan 1 2020" 改为 "Jan 1 2021" 并且不改变样式。由上可知,"Jan 1 2020" 的样式为 "runs[3]" ,所以代码如下:

runs[3].text = "Jan 1 2021" # 更改日期内容且不改变样式
document.save(path) # 保存更改

注:"paragraphs[1].text = "Date:Jan 1 2021" 也能更改内容,但不同的是它将会改变样式

4、增加一段

document.add_paragraph("End")   # 增加一段
document.save(path) # 保存更改

扫描二维码关注公众号,回复: 8928755 查看本文章

处理表格(tables)

1、遍历输出表格

tables = document.tables
for table in tables:    # 遍历所有表格
    for row in range(len(table.rows)):  # 遍历行
        for col in range(len(table.columns)):  # 遍历列
            print(table.cell(row, col).text, end=' ')    # 输出每格的内容
        print()

2、将 "2020" 改为 "2021" 并且不改变样式

tables[0].cell(1, 2).paragraphs[0].runs[0].text = "2021"    # 2020的位置为第1个表格,第2行第3列,段落1,样式1
document.save(path) # 保存更改

win32com模块:直接操作office软件

from win32com.client import Dispatch, constants

w = Dispatch('Word.Application')
# 或者使用下面的方法,使用启动独立的进程:
# w = DispatchEx('Word.Application')

# 后台运行,不显示,不警告
w.Visible = 0
w.DisplayAlerts = 0

# 打开新的文件
doc = w.Documents.Open( FileName = filenamein )
# worddoc = w.Documents.Add() # 创建新的文档

# 插入文字
myRange = doc.Range(0,0)
myRange.InsertBefore('Hello from Python!')

# 使用样式
wordSel = myRange.Select()
wordSel.Style = constants.wdStyleHeading1

# 正文文字替换
w.Selection.Find.ClearFormatting()
w.Selection.Find.Replacement.ClearFormatting()
w.Selection.Find.Execute(OldStr, False, False, False, False, False, True, 1, True, NewStr, 2)

# 页眉文字替换
w.ActiveDocument.Sections[0].Headers[0].Range.Find.ClearFormatting()
w.ActiveDocument.Sections[0].Headers[0].Range.Find.Replacement.ClearFormatting()
w.ActiveDocument.Sections[0].Headers[0].Range.Find.Execute(OldStr, False, False, False, False, False, True, 1, False, NewStr, 2)

# 表格操作
doc.Tables[0].Rows[0].Cells[0].Range.Text ='123123'
worddoc.Tables[0].Rows.Add() # 增加一行

# 转换为html
wc = win32com.client.constants
w.ActiveDocument.WebOptions.RelyOnCSS = 1
w.ActiveDocument.WebOptions.OptimizeForBrowser = 1
w.ActiveDocument.WebOptions.BrowserLevel = 0 # constants.wdBrowserLevelV4
w.ActiveDocument.WebOptions.OrganizeInFolder = 0
w.ActiveDocument.WebOptions.UseLongFileNames = 1
w.ActiveDocument.WebOptions.RelyOnVML = 0
w.ActiveDocument.WebOptions.AllowPNG = 1
w.ActiveDocument.SaveAs( FileName = filenameout, FileFormat = wc.wdFormatHTML )

# 打印
doc.PrintOut()

# 关闭
# doc.Close()
w.Documents.Close(wc.wdDoNotSaveChanges)
w.Quit()

openpyxl模块:对excel文档的处理

from openpyxl import load_workbook, comments

# 加载、读取excel溥
workbook = load_workbook(path)

# 读取表格
sheet = workbook['Sheetname']

# 表最大行、列数
row = sheet.max_row
column = sheet.max_column

# 读取x行、y列单元格的值
value = sheet.cell(x, y).value

# 为x行、y列单元格添加注释
sheet.cell(x, y).comment = comments.Comment('Content', 'Auther')

# 保存工作溥
workbook.save(path)
发布了33 篇原创文章 · 获赞 2 · 访问量 1450

猜你喜欢

转载自blog.csdn.net/qq_34965596/article/details/103795123
今日推荐