excel 设置xlwings属性冻结或不起作用

kognpnkq  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(217)

我一直在尝试使用xlwings模块格式化excel电子表格。在SO上的另一个线程中,我发现了一系列可以使用的格式和打印属性。不幸的是,page_setup似乎不起作用,ws.API.View似乎使整个过程永远挂起。

import xlwings as xw

# Create or modify the excel spreadsheet
    with xw.App(visible=False) as app:
   
        if os.path.exists(classroom_file):
            wb = xw.Book(classroom_file)
        else:
            wb = xw.Book()

        # Selecting a sheet
        ws = xw.sheets[0]
        
        # Page setup
        ws_ps = ws.page_setup

        # Properties
        ws_ps.LeftMargin = 0.75
        ws_ps.RightMargin = 0.75
        ws_ps.TopMargin = 1
        ws_ps.BottomMargin = 1
        ws_ps.HeaderMargin = 0.5
        ws_ps.FooterMargin = 0.5
        ws_ps.PrintHeadings = False
        ws_ps.PrintGridlines = False
        ws_ps.PrintComments = xw.constants.PrintLocation.xlPrintNoComments
        ws_ps.PrintQuality = 600
        ws_ps.CenterHorizontally = True
        ws_ps.CenterVertically = True
        ws_ps.Draft = False
        ws_ps.PaperSize = xw.constants.PaperSize.xlPaperLetter
        ws_ps.FirstPageNumber = xw.constants.Constants.xlAutomatic
        ws_ps.Order = xw.constants.Order.xlDownThenOver
        ws_ps.BlackAndWhite = False
        ws_ps.PrintErrors = xw.constants.PrintErrors.xlPrintErrorsDisplayed
        ws_ps.OddAndEvenPagesHeaderFooter = False
        ws_ps.DifferentFirstPageHeaderFooter = False
        ws_ps.ScaleWithDocHeaderFooter = True
        ws_ps.AlignMarginsHeaderFooter = True

        ws.api.View = xw.constants.xlPageLayoutView

        # rest of the code filling up the spreadsheet (working & tested separately). e.g.,
        ws.range("A1:A2").value = [["Column1"],["Column2"]]
        ws.range('A1').api.Font.Bold = True
        ws.range('A1').api.Font.Size = 15
        # ...

        if os.path.exists(classroom_file):
            wb.save()
        else:
            wb.save(os.path.abspath(classroom_file))
        wb.close()

我试着一次注解/取消注解一行,看看是否有任何工作,查看了稀缺的xlwings文档和xlwings上的示例,并直接与VBA进行了实验/比较(据我所知,xlwings是VBA Package 器)。
这花了我很长时间,但我发现了一些等价物,并希望分享他们,因为它可能对他人有用。(我很乐意听到任何其他方法来做到这一点)

7cwmlq89

7cwmlq891#

看起来Xlwings这些年来经历了很多变化。以前的用户。www.example.com_setup对我来说从来都不起作用,无论我多么努力。ws.page_setup just never worked for me no matter how hard I tried.
但是,可以通过api模块直接调用VBA:
1.大多数VBA常量在constant.py中的类中定义。它们通过xlwings. constants. CLASS_NAME. VARIABLE_NAME调用
1.页面相关属性通过xlwings. sheets [index]. api. PageSetup. PROPERTY_NAME设置
1.窗口相关属性通过xlwings.App.api.ActiveWindow.PROPERTY_NAME设置
下面是我在当前xlwings模块(0.30.9)中使用的属性示例:

import xlwings as xw

with xw.App(visible=False) as app:

    # Changing window related properties
    app.api.ActiveWindow.DisplayGridlines = False
    app.api.ActiveWindow.View = xw.constants.WindowView.xlPageLayoutView

    # Changing page related properties
    ws.api.PageSetup.Orientation = xw.constants.PageOrientation.xlLandscape
    ws.api.PageSetup.PaperSize = xw.constants.PaperSize.xlPaperLetter
    ws.api.PageSetup.Zoom = 75

    # draw borders around a range of cells
    cells = ws.range((6,1),(25,17))
    cells.api.Borders.LineStyle=1
    cells.api.Borders.Weight = 2

    wb.save()
    wb.close()

你知道更多的属性类型以及如何对它们进行api调用吗?

相关问题