繁体   English   中英

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

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

我需要编写一个函数,该函数将自动包装活动Excel工作簿的所有工作表的内容。 除了必要的方法,我已经写了所有东西。 我在任何地方都找不到。 有人对此有任何知识吗?

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()

使用“ microsoft excel interop yourFunctionOrConstantOrClass”进行网络搜索(适用于Google)。 在您的情况下,“ 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