Python 创建Word文档

上一篇 / 下一篇  2014-10-28 14:38:02 / 个人分类:Python

一个项目,需要准备一万多个office文档,每个文档最好大于0.5MB。
 
Python实现Word文档的创建:
 
import win32com.client as win32
from time import sleep
import os
   
def writeFile(myword, doc, rng, filesize, filepath):
    #myword.Open(filepath)
    #rng = doc.Range(0,0)
    rng.InsertAfter('add by writerFile\r\n')
    while (os.path.getsize(filepath) <= filesize):
        for i in RANGE:
            rng.InsertAfter('hello this is word file, Smart Classifier test data' + str(i) + "\r\n")
            rng.InsertAfter('welcome to CDC, this file is created by Icy. ' + str(i) + "\r\n")
        mydoc.SaveAs(filepath)
    doc.Close()
    myword.Application.Quit()
   
if __name__=='__main__':
    RANGE = range(1,3000)
    app = 'word'
    filepath = 'C:\\Temp\\2.docx'
    filesize = 555000
    myapp = win32.Dispatch('%s.Application' %app)
    myapp.Visible = True
    mydoc = myapp.documents.Add()
    sleep(1)
    rng = mydoc.Range(0,0)
    rng.InsertAfter('Python-to-%s Test\r\n\r\n' %app)
    mydoc.SaveAs(filepath)
    writeFile(myapp, mydoc, rng, filesize, 'C:\\Temp\\2.docx')

TAG: Python Word文档

 

评分:0

我来说两句

我的栏目

日历

« 2024-05-03  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 52629
  • 日志数: 17
  • 建立时间: 2009-06-29
  • 更新时间: 2015-04-24

RSS订阅

Open Toolbar