2014-06-08 72 views
0

我需要编写一个函数来将文字包装在活动Excel工作簿的所有工作表中。除了必要的方法外,我已经编写了一切。我无法在任何地方找到它。有没有人有这方面的知识?用于在Excel中自动换行的Python win32com方法?

from win32com.client import Dispatch 
excel = Dispatch('Excel.Application') 

def main(): 
    WordWrapColumns() 

def WordWrapColumns(): 
    excel.ActiveWorkbook.Save() 
    filename = excel.ActiveWorkbook.FullName 
    wb = excel.Workbooks.Open(filename) 
    #Activate all sheets 
    active_sheets = wb.Sheets.Count 
    for i in range(0,active_sheets): 
     excel.Worksheets(i+1).Activate() 
     # Word wrap all columns in sheet 
     # What command goes here???? 
    #Save and close workbook 
    wb.Save() 
    wb.Close() 
    excel.Quit() 

if __name__ == '__main__': 
    main() 

回答

1

使用“microsoft excel interop yourFunctionOrConstantOrClass”进行网页搜索(在google中有效)。在你的情况“microsoft excel interop word wrap”发现Range.WrapText property这表明你应该可以做类似

for i in range(0,active_sheets): 
    ws = wb.Worksheets(i+1) 
    ws.Columns.WrapText = True 
+0

谢谢,它的工作原理! –

相关问题