Python win32com method for word-wrap in Excel?

喜你入骨 提交于 2020-01-06 08:42:03

问题


I need to write a function that will word wrap the contents of all sheets of an active Excel workbook. I have everything written except the necessary method. I am not able to find it anywhere. Does anyone have any knowledge on this?

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:


Use "microsoft excel interop yourFunctionOrConstantOrClass" for web searches (works in google). In your case "microsoft excel interop word wrap" finds Range.WrapText property which suggests you should be able to do something like

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


来源:https://stackoverflow.com/questions/24108791/python-win32com-method-for-word-wrap-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!