python 操作 office


http://www.cnblogs.com/youxin/p/3548647.html?utm_source=tuicool&utm_medium=referral

首先介紹下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()
復制代碼

 

參考:

http://blog.csdn.net/lzl001/article/details/8435048

另一篇:

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屬性:

詳細看:

http://wenku.baidu.com/view/c248150d6c85ec3a87c2c57f.html

更多:

 

http://wenku.baidu.com/view/d65d36728e9951e79b892737

http://my.oschina.net/duxuefeng/blog/64137

 

我遇到錯誤com_error: (-2147221164, '\xc3\xbb\xd3\xd0\xd7\xa2\xb2\xe1\xc0\xe0', None, None),網上找了好久沒發現解決方案,說是沒找到word進程。最后猛然想到自己電腦用的不是office,而是wps,看來還是有區別的。

又搜了下python 調用 wps,方法與word下類似,只是進程名不同而已:

import win32com.client<br />
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窗口)。

參考:http://blog.csdn.net/ccat/article/details/5784933

如果操作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"
復制代碼

參考:http://blog.csdn.net/magictong/article/details/4966822

一個操作word的類:

http://wenku.baidu.com/view/ef59d6d728ea81c758f578da.html

一個操作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()
        
                        
            
復制代碼

 一個更詳細的類:http://blog.sina.com.cn/s/blog_3fcd4ff90100n2mb.html

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

http://stackoverflow.com/questions/11444207/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.

def rgb_to_hex(rgb): strValue ='%02x%02x%02x'% rgb iValue = int(strValue,16)return iValue 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

Python (refer to http://www.python.org)

Win32 Python module (refer to http://sourceforge.net/projects/pywin32)

Microsoft Excel (refer to http://office.microsoft.com/excel)

Source Files and Scripts

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

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

 

如果設置了某行的背景色,那么邊框將看不見,這是很不爽的,要設置單元格的格式border顯示格式,有line style,等多種,怎么用程序

設置呢?找了n久,終於發現了,

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM