简体   繁体   English

在Excel中自动换行的Python win32com方法?

[英]Python win32com method for word-wrap in Excel?

I need to write a function that will word wrap the contents of all sheets of an active Excel workbook. 我需要编写一个函数,该函数将自动包装活动Excel工作簿的所有工作表的内容。 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()

Use "microsoft excel interop yourFunctionOrConstantOrClass" for web searches (works in google). 使用“ microsoft excel interop yourFunctionOrConstantOrClass”进行网络搜索(适用于Google)。 In your case "microsoft excel interop word wrap" finds Range.WrapText property which suggests you should be able to do something like 在您的情况下,“ Microsoft Excel互操作性自动换行”会找到Range.WrapText属性 ,这建议您应该能够执行以下操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM