Chu*_*aka 3 python attributeerror win32com
我们准备以下python脚本以在单词表中显示图片。
import matplotlib.pyplot as plt
import pylab
import win32com.client as win32
import os
# Skip picture making parts
#Generate word file
#Create and formating
wordapp = win32.Dispatch("Word.Application") #create a word application object
wordapp.Visible = 1 # hide the word application
doc=wordapp.Documents.Add()
# create a new application
doc.PageSetup.RightMargin = 20
doc.PageSetup.LeftMargin = 20
doc.PageSetup.Orientation = 1
# a4 paper size: 595x842
doc.PageSetup.PageWidth = 595
doc.PageSetup.PageHeight = 842
header_range= doc.Sections(1).Headers(win32.constants.wdHeaderFooterPrimary).Range
header_range.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphCenter
header_range.Font.Bold = True
header_range.Font.Size = 12
header_range.Text = ""
#Create and formating
#insert table
total_column = 3
total_row = len(compound_name)+1
rng = doc.Range(0,0)
rng.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphCenter
table = doc.Tables.Add(rng,total_row, total_column)
table.Borders.Enable = True
if total_column > 1:
table.Columns.DistributeWidth()
#table title
table.Cell(1,1).Range.InsertAfter("title1")
table.Cell(1,2).Range.InsertAfter("title2")
table.Cell(1,3).Range.InsertAfter("title3")
#collect image
frame_max_width= 167 # the maximum width of a picture
frame_max_height= 125 # the maximum height of a picture
#
for index, filename in enumerate(filenames): # loop through all the files and folders for adding pictures
if os.path.isfile(os.path.join(os.path.abspath("."), filename)): # check whether the current object is a file or not
if filename[len(filename)-3: len(filename)].upper() == 'PNG': # check whether the current object is a JPG file
cell_column= index % total_column + 1
cell_row = index / total_column + 2
cell_range= table.Cell(cell_row, cell_column).Range
cell_range.ParagraphFormat.LineSpacingRule = win32.constants.wdLineSpaceSingle
cell_range.ParagraphFormat.SpaceBefore = 0
cell_range.ParagraphFormat.SpaceAfter = 3
#this is where we are going to insert the images
current_pic = cell_range.InlineShapes.AddPicture(os.path.join(os.path.abspath("."), filename))
doc.SaveAs(os.path.join(os.path.abspath("."),"final.doc"))
doc.Close()
Run Code Online (Sandbox Code Playgroud)
但是,在运行它时,由于此行,将弹出以下错误
header_range= doc.Sections(1).Headers(win32.constants.wdHeaderFooterPrimary).Range
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
Traceback (most recent call last):
File "Drawing.py", line 99, in <module>
header_range= doc.Sections(1).Headers(win32.constants.wdHeaderFooterPrimary).Range
File "C:\Python27\Lib\site-packages\win32com\client\__init__.py", line 170, in __getattr__
raise AttributeError(a)
AttributeError: wdHeaderFooterPrimary
Run Code Online (Sandbox Code Playgroud)
在我看来“ wdHeaderFooterPrimary”有问题。因此,我只是将以下几行静音,然后再次运行。
#header_range= doc.Sections(1).Headers(win32.constants.wdHeaderFooterPrimary).Range
#header_range.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphCenter
#header_range.Font.Bold = True
#header_range.Font.Size = 12
#header_range.Text = ""
Run Code Online (Sandbox Code Playgroud)
将会显示另一个错误消息:
Traceback (most recent call last):
File "C:Drawing.py", line 114, in <module>
rng.ParagraphFormat.Alignment = win32.constants.wdAlignParagraphCenter
File "C:\Python27\Lib\site-packages\win32com\client\__init__.py", line 170, in __getattr__
raise AttributeError(a)
AttributeError: wdAlignParagraphCenter
Run Code Online (Sandbox Code Playgroud)
我在64位Windows 7中运行python 2.7.6。安装了matplotlib.pyplot和pylab。win32com.client是2.7.6 32位,版本219。Office是64位,但是下载站点评论说win32 32bit在Office 64位/ Windows 7 64位中应该可以正常工作(http://sourceforge.net/projects/ pywin32 /?source = navbar)。我可以知道任何一位大师可能有任何意见/解决方案吗?谢谢!
仅当静态调度可用时,常量才可用。需要使用EnsureDispatch(而不是Dispatch)或通过makepy.py或genclient生成类型库(这EnsureDispatch对您有用)。所以我会尝试使用EnsureDispatch。注意:位于win32com.client.gencache:
xl = win32com.client.gencache.EnsureDispatch ("Word.Application")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3719 次 |
| 最近记录: |