英文:
Setting xlwings properties freezes or does not work
问题
我一直在尝试使用xlwings模块格式化Excel电子表格。在Stack Overflow的另一个帖子中,我找到了一系列可以尝试的格式化和打印属性。不幸的是,page_setup似乎不起作用,而ws.api.View似乎会使整个过程永远挂起。
import xlwings as xw
# 创建或修改Excel电子表格
with xw.App(visible=False) as app:
if os.path.exists(classroom_file):
wb = xw.Book(classroom_file)
else:
wb = xw.Book()
# 选择工作表
ws = xw.sheets[0]
# 页面设置
ws_ps = ws.page_setup
# 属性
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
# 填充电子表格的其余代码(分开工作并进行测试)。例如,
ws.range("A1:A2").value = [["列1"],["列2"]]
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包装器)。经过很长时间的努力,我找到了一些等效的方法,并希望分享给其他人。(如果有其他方法,我很愿意听听)
英文:
I have been trying to format an excel spreadsheet using the xlwings module. On another thread on SO, I found a series of formatting & printing properties to play with. Unfortunately, page_setup does not seem to work and ws.api.View seems to make the whole process hang forever.
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()
I tried to comment / uncomment one line at a time to see if any were working, looked at the scarce xlwings documentation and examples on xlwings, and experimented/compared it directly with VBA (as I understand it, xlwings is a VBA wrapper).
It took me a long time but I found some equivalents and wanted to share them since it could be useful to others. (I'd love to hear of any other ways to do this)
答案1
得分: 1
似乎 xlwings 在过去几年里经历了许多变化。尽管先前的用户尝试过,但 ws.page_setup 对我来说始终无法正常工作,无论我如何努力。
然而,可以通过 api 模块进行直接的 VBA 调用:
- 大多数 VBA 常量在 constant.py 中的类中定义。它们通过 xlwings.constants.CLASS_NAME.VARIABLE_NAME 调用。
- 与页面相关的属性通过 xlwings.sheets[index].api.PageSetup.PROPERTY_NAME 设置。
- 与窗口相关的属性通过 xlwings.App.api.ActiveWindow.PROPERTY_NAME 设置。
以下是在当前的 xlwings 模块(0.30.9)中对我有效的属性示例:
import xlwings as xw
with xw.App(visible=False) as app:
# 修改与窗口相关的属性
app.api.ActiveWindow.DisplayGridlines = False
app.api.ActiveWindow.View = xw.constants.WindowView.xlPageLayoutView
# 修改与页面相关的属性
ws.api.PageSetup.Orientation = xw.constants.PageOrientation.xlLandscape
ws.api.PageSetup.PaperSize = xw.constants.PaperSize.xlPaperLetter
ws.api.PageSetup.Zoom = 75
# 在一系列单元格周围绘制边框
cells = ws.range((6,1),(25,17))
cells.api.Borders.LineStyle=1
cells.api.Borders.Weight = 2
wb.save()
wb.close()
你知道还有哪些类型的属性以及如何对它们进行 api 调用吗?
英文:
It seems like xlwings has gone through a lot of changes over the years. While a previous user. ws.page_setup just never worked for me no matter how hard I tried.
However, it is possible to make direct VBA calls through the api module:
- most VBA constants are defined in classes in constant.py. They are called through xlwings.constants.CLASS_NAME.VARIABLE_NAME
- page related properties are set through the xlwings.sheets[index].api.PageSetup.PROPERTY_NAME
- window related properties are set through the xlwings.App.api.ActiveWindow.PROPERTY_NAME
Here is an example of the properties that worked for me in the current xlwings module (0.30.9):
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
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()
<!-- end snippet -->
Do you know more types of properties and how to make an api call to them?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论