使用python写入单词

时间:2014-01-08 12:26:30

标签: python winapi

我使用下面的代码在文本文件中写入ms字,但不写行。此外,我无法设置页面宽度和高度。我使用wordapp.PageSetup.width来获取某些值,但它抛出了错误。 请说明我在哪里失败了..

import win32com.client
read = open('out.txt','r')
curser = read.readlines()

wordapp = win32com.client.Dispatch("Word.Application") 
wordapp.Visible = 0 
x = wordapp.Documents.Add() 
x.PageSetup.Orientation = 1 # Make some Setup to the Document:
x.PageSetup.LeftMargin = 0.4
x.PageSetup.TopMargin = 0.5
x.PageSetup.Width = 8.27 #This threw an error
x.PageSetup.BottomMargin = 0.5
x.PageSetup.RightMargin = 0.4
x.Content.Font.Size = 11
x.Content.Paragraphs.TabStops.Add (100)
x.Content.Text = curser #This threw an error
x.Content.MoveEnd
x.Close() # Close the Word Document (a save-Dialog pops up)
x.Quit() # Close the Word Application

1 个答案:

答案 0 :(得分:3)

您的Python界面使用Microsoft Office应用程序的标准API接口,因此您需要知道的所有内容都可以在http://msdn.microsoft.com/en-us/library/office/ff835409.aspx上找到。

非常快速浏览显示您要查找的媒体资源不是PageSetup.Width,而应该是PageSetup.PageWidth。我没有检查,但我猜你的“诅咒”问题是一样的 - x.Content.Text可能只是作为一个属性不存在。

你正在做的事情被称为“Cargo Cult Programming” - 也就是说,你输入的命令并不真正知道他们做了什么,也没有查找它们,而是依赖其他“有效的代码”(我'猜测:从The Python Script Collection等资源复制,看似相关的重复命令部分,并在你去的时候弥补任何缺失的属性。

不是推荐的学习方法。

相关问题