标签:

首先介绍下office win32 com接口,这个是MS为自动化提供的操作接口,比如我们打开一个WORD文档,就可以在里面编辑VB脚本,实现我们自己的效果。对于这种一本万利的买卖,Python怎么能放过,它内置了对于win32 com接口的支持,我们可以方便的控制。

要想熟练使用office win32 com接口,没有什么比MS提供的API文档更加权威了.

ORD中最重要的概念有几个:

Application - 这个毫无疑问是我们的WORD应用程序

Document - 这个就是一个打开的文档对象

Range - 这个东东必须要好好利用,基本上所有对象都是有Range属性的,而这也为我们排版提供了极大的便利。。。

Paragraph - 顾名思义,这个是段落的意思,也就是我们文档中的一个段内容(可以是文本、图片等)。

Section - 在我学习的时候,这个东东给我制造了最大的障碍,因为我当时苦苦琢磨,究竟怎么才能插入一个新的页,然后在新页上开始输出内容。。。

ParagraphFormat - 这个是为了设置格式的,你不可能不使用它。。。

有了以上几个最重要的概念铺垫,接下来的代码基本上可以平蹚了,来,看一段代码先,事先声明,这段代码摘自网上,不知道哪位兄台写的,只写了可写可不写的东西(这么说有点过分,不过确实没有实质性的内容),我添油加醋的处理了一下,确保营养比较丰富,大家看起来应该实用性更强一些。。。

import win32com

from win32com.client import Dispatch, constants

w = win32com.client.Dispatch(‘Word.Application‘)

# 或者使用下面的方法,使用启动独立的进程:

# w = win32com.client.DispatchEx(‘Word.Application‘)

# 后台运行,显示程序界面,不警告

w.Visible = 1 #这个至少在调试阶段建议打开,否则如果等待时间长的话,它至少给你耐心。。。

w.DisplayAlerts = 0

# 打开新的文件

#worddoc = w.Documents.Open(file_name) #这句话用来打开已有的文件,当然,在此之前你最好判断文件是否真的存在。。。

doc = w.Documents.Add() # 创建新的文档,我用的更多的是这个,因为我要的是创建、然后保存为。。

# 插入文字

myRange = doc.Range(0,0) #这句话让你获取的是doc的最前面位置,如果想要获取到其他位置,就要改变Range中的参数,两个参数分别代表起始点,结束点。。。

myRange.InsertBefore(‘Hello from Python!‘)

‘‘‘

以下一段是增加10个新页,然后跳转到新页中增加内容。。。。

‘‘‘

section_index = 0

for i in range(0, 10):

#由于增加页的使用如此频繁,我们最好将其提取为一个函数,类似def NewTable(self):

pre_section = doc.Secitons(section_index)

new_seciton = doc.Range(pre_section.Range.End, pre_section.Range.End).Sections.Add()

new_range = new_seciton.Range

content_pg = new_range.Paragraphs.Add()

content_pg.Range.Font.Name,content_pg.Range.Font.Size = ‘Times New Roman‘,24

caption_pg.Range.ParagraphFormat.Alignment = 0 # 0,1,2 分别对应左对齐、居中、右对齐

caption_pg.Range.InsertBefore(‘Hello,Page ‘ + str(i+1))

section_index = section_index + 1 #记录这个的目的是为了方便的找到doc的尾端,不然的话,我还真没有想到怎么搞。。。

# 正文文字替换

w.Selection.Find.ClearFormatting()

w.Selection.Find.Replacement.ClearFormatting()

w.Selection.Find.Execute(OldStr, False, False, False, False, False, True, 1, True, NewStr, 2)

#设置页眉文字,如果要设置页脚值需要把SeekView由9改为10就可以了。。。

w.ActiveWindow.ActivePane.View.SeekView = 9 #9 - 页眉; 10 - 页脚

w.Selection.ParagraphFormat.Alignment = 0

w.Selection.Text = ‘New Header‘

w.ActiveWindow.ActivePane.View.SeekView = 0 # 释放焦点,返回主文档

# 页眉文字替换

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)

# 在文档末尾新增一页,并添加一个表格。。。

pre_section = doc.Secitons(section_index)

new_seciton = doc.Range(pre_section.Range.End, pre_section.Range.End).Sections.Add()

new_range = new_seciton.Range

new_table = new_range.Tables.Add(doc.Range(new_range.End,new_range.End), 5, 5) #在文档末尾添加一个5*5的表格

#接下来Table怎么操作,这里就不细说了,检索Table对象参照就OK了。。。

# 表格操作

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()

简单示例:(wps下)

#导入引用win32com.client模块

import win32com.client

#新建WPS进程

wpsApp=win32com.client.Dispatch("wps.Application")

#可视

wpsApp.Visible=1

#添加文档

wpsDoc=wpsApp.Documents.Add()

#添加内容

wpsDoc.content.text="Hello World!"

#保存文档

wpsDoc.SaveAs("C:\Sample.wps")

#关闭文档 wpsdoc.Close()

#关闭进程 wpsApp.Quit()

参考:

另一篇:

import win32com

from win32com.client import Dispatch, constants

接著,要讓我們的Python程式和MS Word建立起連結。

msword = Dispatch(‘Word.Application‘)

用Dispatch()的方式將會啟動MS Word。不過,如果您已經有執行MS Word,則此方式只會與現存的MS Word建立連結。如果您強烈希望能夠有一個新的MS Word程式出現,可用下面的方式:

msword = DispatchEx(‘Word.Application‘)

此時您會發現畫面上沒有任何MS Word出現,其實他已經在背後執行了。您可以透過工作管理員來查看是否有一個名為"WINWORD.EXE"的

Process。不產生畫面的好處是您可 以在背景來處理您要進行的工作。如果您想要看看到底是不是真的有成功的啟動MS Word,請設定Visible屬性。

msword.Visible = 1 # 1表示要顯示畫面,若為0則不顯示畫面。您可以隨時的更改此屬性。

除了不顯示畫面外,您也許還會希望不要顯示一些警告訊息。此時請設定DisplayAlerts屬性:

详细看:

更多:

我遇到错误com_error: (-2147221164, ‘\xc3\xbb\xd3\xd0\xd7\xa2\xb2\xe1\xc0\xe0‘, None, None),网上找了好久没发现解决方案,说是没找到word进程。最后猛然想到自己电脑用的不是office,而是wps,看来还是有区别的。

又搜了下python 调用 wps,方法与word下类似,只是进程名不同而已:

import win32com.client

o = win32com.client.Dispatch("wps.application") (office下是Word.application)

o.Visible=True

doc = o.Documents.Add()

doc.Content.text="Hello world!"

执行。会看到弹出一个 WPS 窗口,其中新建了一个文档,正文为“Hello World”。

如果在交互环境中逐行执行,会看到每一步的效果(有时需要激活一下WPS窗口)。

如果操作excel,在网上搜了下,一篇文章是这样说的:

我在网上找了下,发现至少有两种方法,第一种是直接操作excle的com库,当然python自带的lib里面已经给我们封装好了实现,直接使用就可以了,win32com.client,这种方法甚至可以直接把excle的进程调用起来。用法很简单,网上的文章也汗牛充栋,就不详细解说了,给个小例子吧,嘻嘻。这种只能在windows下运行,并且需要安装MS Excel。

wps下面的Excel.application叫et.application

# -*- coding: utf-8 -*-

from win32com.client import constants, Dispatch

xlsApp = Dispatch("Excel.Application") (wps下叫et.application)

# 通过赋值Visible为True或者False可以控制是否调出excle

xlsApp.Visible = 1

# xlsBook = xlsApp.Workbooks.Open("c://magictong.xls")

# xlsSht = xlsBook.Worksheets("sheet1") (第一个sheet1)。

xlsBook = xlsApp.Workbooks.Add()(wps已经默认帮你创建了sheet1,sheet2,sheet3,这里创建另一个sheet,)

xlsSht = xlsBook.Sheets.Add()

xlsSht.Cells(2, 3).Value = "Tecent QQ"

xlsSht.Cells(2, 3).Font.Color = 0xff0000

xlsSht.Name = "GCD go to bell"

xlsBook.SaveAs("c://magictong.xls")

xlsApp.Quit()

print "__end"

一个操作word的类:

一个操作excel的类:(《Python Programming on Win32》书中也有很详细的介绍)

#!/usr/bin/python

#coding=utf-8

import win32com

from win32com.client import Dispatch,constants

class EasyExcel:

def __init__(self,filename=None):

self.xlApp=win32com.client.Dispatch("et.Application")

self.xlApp.Visible=True

if filename:

self.filename=filename

self.xlBook=self.xlApp.Workbooks.Open(filename)

else:

self.xlBook=self.xlApp.Workbooks.Add()

self.filename=‘‘

def save(self,newfilename=None):

if newfilename:

self.filename=newfilename

self.xlBook.SaveAs(newfilename)

else:

self.xlBook.Save()

def close(self):

#self.xlBook.Close(SaveChanges=0)

#self.xlApp.Quit()

#del self.xlApp

print "close"

def getCell(self,sheet,row,col):

"get value of one cell"

sht=self.xlBook.Worksheets(sheet)

return sht.Cells(row,col).Value

def setCell(self,sheet,row,col,value):

‘set value of one cell‘

sht=self.xlBook.Worksheets(sheet)

sht.Cells(row,col).Value=value

def getRange(self,sheet,row1,col1,row2,col2):

‘return a 2d array(ie:tuple of tuples‘

sht=self.xlBook.Worksheets(sheet)

return sht.Range(sht.Cells(row1,col1),sht.Cells(row2,col2)).Value

def addPicture(self,sheet,pictureName,Left,Top,Width,Height):

‘insert a picture in sheet‘

sht=self.xlBook.Worksheets(sheet)

sht.Shapes.AddPicture(pictureName,1,1,Left,Top,Width,Height)

def cpSheet(self,before):

‘copy sheet‘

shts=self.xlBook.Worksheets

shts(1).Copy(None,shts(1))

def addSheet(self,sheetName=‘MySheet1‘):

self.sht1=self.xlBook.Sheets.Add()

self.sht1.Name=sheetName

if __name__=="__main__":

pic=‘D:/image/sunyanzi/yanzi760_580.jpg‘

xls=EasyExcel()

sht1=xls.addSheet()

xls.addPicture("MySheet1",pic,20,20,700,700)

xls.setCell("MySheet1",1,1,"1_1_value")

xls.setCell("MySheet1",1,2,"1_2_value");

xls.setCell("MySheet1",1,3,"1_3_value")

xls.setCell("MySheet1",2,1,"2_1_value")

xls.setCell("MySheet1",2,2,"2_2_value")

xls.save("D:/MySheet.xls")

xls.close()

setting-a-cells-fill-rgb-color-with-pywin32-in-excel

interior.color expects a hex value If you want to specify in RGB form below code can be used.

defrgb_to_hex(rgb):strValue =‘%02x%02x%02x‘%rgb

iValue =int(strValue,16)returniValue

xl.ActiveSheet.Cells(row,column).interior.color =rgb_to_hex((255,255,0))font颜色可以直接设置:

sht.Rows(1).Font.Color= 0xff0000

非常好的文档:

http://pythonexcels.com/python-excel-mini-cookbook/

Ranges and Offsets

This script illustrates different techniques for addressing cells by using the Cells() and Range()operators. Individual cells can be addressed using Cells(row,column), where row is the row number, column is the column number, both start from 1. Groups of cells can be addressed using Range(), where the argument in the parenthesis can be a single cell denoted by its textual name (eg "A2"), a group noted by a textual name with a colon (eg "A3:B4") or a group denoted with two Cells() identifiers (eg ws.Cells(1,1),ws.Cells(2,2)). The Offsetmethod provides a way to address a cell based on a reference to another cell.

#

# Using ranges and offsets

#

import win32com.client as win32

excel = win32.gencache.EnsureDispatch(‘Excel.Application‘)

wb = excel.Workbooks.Add()

ws = wb.Worksheets("Sheet1")

ws.Cells(1,1).Value = "Cell A1"

ws.Cells(1,1).Offset(2,4).Value = "Cell D2"

ws.Range("A2").Value = "Cell A2"

ws.Range("A3:B4").Value = "A3:B4"

ws.Range("A6:B7,A9:B10").Value = "A6:B7,A9:B10"

wb.SaveAs(‘ranges_and_offsets.xlsx‘)

excel.Application.Quit()

Autofill Cell Contents

This script uses Excel’s autofill capability to examine data in cells A1 and A2, then autofill the remaining column of cells through A10.

#

# Autofill cell contents

#

import win32com.client as win32

excel = win32.gencache.EnsureDispatch(‘Excel.Application‘)

wb = excel.Workbooks.Add()

ws = wb.Worksheets("Sheet1")

ws.Range("A1").Value = 1

ws.Range("A2").Value = 2

ws.Range("A1:A2").AutoFill(ws.Range("A1:A10"),win32.constants.xlFillDefault)

wb.SaveAs(‘autofill_cells.xlsx‘)

excel.Application.Quit()

Cell Color

This script illustrates adding an interior color to the cell using Interior.ColorIndex. Column A, rows 1 through 20 are filled with a number and assigned that ColorIndex.

#

# Add an interior color to cells

#

import win32com.client as win32

excel = win32.gencache.EnsureDispatch(‘Excel.Application‘)

wb = excel.Workbooks.Add()

ws = wb.Worksheets("Sheet1")

for i in range (1,21):

ws.Cells(i,1).Value = i

ws.Cells(i,1).Interior.ColorIndex = i

wb.SaveAs(‘cell_color.xlsx‘)

excel.Application.Quit()

Column Formatting

This script creates two columns of data, one narrow and one wide, then formats the column width with the ColumnWidth property. You can also use the Columns.AutoFit() function to autofit all columns in the spreadsheet.

#

# Set column widths

#

import win32com.client as win32

excel = win32.gencache.EnsureDispatch(‘Excel.Application‘)

wb = excel.Workbooks.Add()

ws = wb.Worksheets("Sheet1")

ws.Range("A1:A10").Value = "A"

ws.Range("B1:B10").Value = "This is a very long line of text"

ws.Columns(1).ColumnWidth = 1

ws.Range("B:B").ColumnWidth = 27

# Alternately, you can autofit all columns in the worksheet

# ws.Columns.AutoFit()

wb.SaveAs(‘column_widths.xlsx‘)

excel.Application.Quit()

Copying Data from Worksheet to Worksheet

This script uses the FillAcrossSheets() method to copy data from one location to all other worksheets in the workbook. Specifically, the data in the range A1:J10 is copied from Sheet1 to sheets Sheet2 and Sheet3.

#

# Copy data and formatting from a range of one worksheet

# to all other worksheets in a workbook

#

import win32com.client as win32

excel = win32.gencache.EnsureDispatch(‘Excel.Application‘)

wb = excel.Workbooks.Add()

ws = wb.Worksheets("Sheet1")

ws.Range("A1:J10").Formula = "=row()*column()"

wb.Worksheets.FillAcrossSheets(wb.Worksheets("Sheet1").Range("A1:J10"))

wb.SaveAs(‘copy_worksheet_to_worksheet.xlsx‘)

excel.Application.Quit()

Format Worksheet Cells

This script creates two columns of data, then formats the font type and font size used in the worksheet. Five different fonts and sizes are used, the numbers are formatted using a monetary format.

#

# Format cell font name and size, format numbers in monetary format

#

import win32com.client as win32

excel = win32.gencache.EnsureDispatch(‘Excel.Application‘)

wb = excel.Workbooks.Add()

ws = wb.Worksheets("Sheet1")

for i,font in enumerate(["Arial","Courier New","Garamond","Georgia","Verdana"]):

ws.Range(ws.Cells(i+1,1),ws.Cells(i+1,2)).Value = [font,i+i]

ws.Range(ws.Cells(i+1,1),ws.Cells(i+1,2)).Font.Name = font

ws.Range(ws.Cells(i+1,1),ws.Cells(i+1,2)).Font.Size = 12+i

ws.Range("A1:A5").HorizontalAlignment = win32.constants.xlRight

ws.Range("B1:B5").NumberFormat = "$###,##0.00"

ws.Columns.AutoFit()

wb.SaveAs(‘format_cells.xlsx‘)

excel.Application.Quit()

Setting Row Height

This script illustrates row height. Similar to column height, row height can be set with the RowHeight method. You can also useAutoFit() to automatically adjust the row height based on cell contents

#

# Set row heights and align text within the cell

#

import win32com.client as win32

excel = win32.gencache.EnsureDispatch(‘Excel.Application‘)

wb = excel.Workbooks.Add()

ws = wb.Worksheets("Sheet1")

ws.Range("A1:A2").Value = "1 line"

ws.Range("B1:B2").Value = "Two\nlines"

ws.Range("C1:C2").Value = "Three\nlines\nhere"

ws.Range("D1:D2").Value = "This\nis\nfour\nlines"

ws.Rows(1).RowHeight = 60

ws.Range("2:2").RowHeight = 120

ws.Rows(1).VerticalAlignment = win32.constants.xlCenter

ws.Range("2:2").VerticalAlignment = win32.constants.xlCenter

# Alternately, you can autofit all rows in the worksheet

# ws.Rows.AutoFit()

wb.SaveAs(‘row_height.xlsx‘)

excel.Application.Quit()

Prerequisites

Source Files and Scripts

Source for the program and data text file are available athttp://github.com/pythonexcels/examples

如果设置了某行的背景色,那么边框将看不见,这是很不爽的,要设置单元格的格式border显示格式,有line style,等多种,怎么用程序

设置呢?找了n久,终于发现了,

sht.Rows(1).Borders.LineStyle=1

标签:

原文地址:http://www.cnblogs.com/Jacklovely/p/5743868.html

使用python处理wps表格_python 操作 office相关推荐

  1. 使用python处理wps表格_python中常⽤的excel模块库

    python中常用的excel模块库&安装方法 openpyxlopenpyxl是⼀个Python库,用于读取/写⼊Excel 2010 xlsx / xlsm / xltx / xltm⽂件 ...

  2. java poi wps_POI操作WPS表格POI操作WPS表格.docx

    POI操作WPS表格POI操作WPS表格 原文网址:/thread1-1.htmlWps二次开发--POI入门教程一.POI的介绍Apache POI是Apache软件基金会的开放源码函式库,POI提 ...

  3. wps公式编辑器 wps表格如何操作筛选格式呢?

    转载请说明来源于"厦门SEO" 本文地址:http://www.96096.cc/Article/169705.html wps公式编辑器 wps表格是我们办公时经常用的软件,为了 ...

  4. Python对excel表格进行操作

    利用Python对Excel表格进行操作 对Excel表格进行操作首先需要下载好第三方库(Xlrd,Xlwt,xlutils,XlsxWriter) 对表格元素进行遍历操作 import xlrd # ...

  5. WPS表格转换成office用什么软件

    WPS表格怎么转换成office?现在的办公软件越来越多,很多人用的软件是不一样的,WPS和office就是比较有代表性的软件,WPS表格转office表格在使用软件的情况下是可以进行转换的,WPS表 ...

  6. python处理wps表格数据匹配_两个excel表格数据匹配wps-WPS怎样用VLOOKUP引用另一个表格的数据...

    wps精确匹配,把一个wps表格里面的数据匹配到另一... 材料/工具:wps2016 1.打开匹配个表格,表1和表2. 2.在需要放匹配值的格中输入=vl,就会自动提示vlookup函 3.双击选择 ...

  7. Python控制Excel表格的操作以及问题处理

    Python控制Excel表格(openpyxl) Python创建的sheet表并操作(因为新建表所以这里只有存值操作): ​ 1.引入: from openpyxl import Workbook ...

  8. python读取pdf表格_Python使用Tabula提取PDF表格数据

    今天遇到一个批量读取pdf文件中表格数据的需求,样式大体是以下这样: python读取PDF无非就是三种方式(我所了解的),pdfminer.pdf2htmlEX 和 Tabula.综合考虑后,选择了 ...

  9. 使用python处理wps表格_用python写wps的excel文件 | 学步园

    参考了网上的一些帖子,试了一下用python写wps的excel文件(需要windows操作系统,安装了wps的表格处理程序,安装了pywin32)--我安装的是WPS个人版,可以免费使用的. imp ...

最新文章

  1. python 图表_Python入门学习系列——使用Python调用Web API实现图表统计
  2. [ASP.NET MVC 小牛之路]10 - Controller 和 Action (2)
  3. 企业应用系统总体设计-----面向对象方法
  4. mysql_upgrade --force_社区投稿 | MySQL 8.0.16 告别mysql_upgrade升级方式
  5. jrockit_JRockit – JRCMD有用的命令
  6. POJ 1222 1681 1830 3185 开关灯问题 (高斯消元 异或方程组)
  7. CNN(Convolutional Neural Network) 的基础
  8. 【课题总结】OpenCV 抠图项目实战(3)抠图综述
  9. Kafka的高性能原理
  10. Android studio无法创建类和接口问题解决办法。提示 Unable to parse template Class
  11. 查看mysql单个表大小限制_查看单个mysql数据库中各个表的大小
  12. 【测试】软件测试之测试用例的设计方法
  13. The JAVA_HOME environment variable is not defined correctly 解决方法
  14. Canvas 烟花合集 -- 将粉丝头像做成烟花在天空绽放✨
  15. 学会这一招,轻松处理图片转ico格式
  16. Linux磁盘存储和文件系统
  17. android 禁用触摸屏,animation时禁用所有触摸屏交互
  18. 1024 程序员节,带你走进程序员的世界——
  19. 移动式护栏巡逻机器人_重磅!移动式护栏巡逻执法机器人上岗!专盯高速乱停乱行!...
  20. 教你计算个人所得税(企业员工)

热门文章

  1. 国民体质测定标准计算机应用软件,(完整版)国民体质测定标准》(成年人部分.docx...
  2. Failed to load class “org.slf4j.impl.StaticLoggerBinder“.
  3. 北理工远程教育在线作业统考计算机,北京理工大学计算机统考网上作业答案
  4. 诗词大全给力版_6大版本语文课本大比拼,看哪个省背的课文最多?
  5. idea 控制台乱码
  6. ubuntu18.04如何新建文件
  7. 求助:STM32中U32存储范围
  8. tigase mysql_tigase5.2.3使用mysql5.6出错
  9. 855 Plus+四摄+90Hz流体屏,这款新旗舰给力不贵
  10. 美团0.99充10元话费,秒到账,不实名不绑卡,超简单!