【读者群答疑】使用xlwings编程时,执行语句bk = xw.books.add()时报错

在Python中使用xlwings库操作Excel文件时,直接调用`xw.books.add()`可能会因没有运行的Excel应用而报错。推荐先初始化`xw.App()`来确保Excel应用存在,再进行文件操作,如`bk=app.books.active`或`bk=app.books.add()`。这样可以避免程序异常并保证文件操作的顺利进行。

在这里插入图片描述

测试发现,当至少有一个Excel应用存在时,执行语句bk = xw.books.add()不会出错;当没有Excel应用存在是直接执行语句bk = xw.books.add()会出错。
建议使用下面的方式:

>>> import xlwings as xw
>>> app=xw.App()
>>> bk=app.books.active   #或   bk=app.books.add()
>>> sht=bk.sheets.active
from pptx import Presentation from pptx.util import Inches, Pt from pptx.enum.text import PP_ALIGN from pptx.dml.color import RGBColor # 创建PPT prs = Presentation() prs.slide_width = Inches(13.33) # 16:9 prs.slide_height = Inches(7.5) # 标题页 slide = prs.slides.add_slide(prs.slide_layouts[6]) title_box = slide.shapes.add_textbox(Inches(1), Inches(2.5), Inches(11), Inches(1)) title_frame = title_box.text_frame title_frame.text = "骨折影像诊断与报告规范" title_para = title_frame.paragraphs[0] title_para.font.size = Pt(44) title_para.font.bold = True title_para.alignment = PP_ALIGN.CENTER sub_box = slide.shapes.add_textbox(Inches(3), Inches(3.5), Inches(7), Inches(1)) sub_frame = sub_box.text_frame sub_frame.text = "2025年版" sub_para = sub_frame.paragraphs[0] sub_para.font.size = Pt(28) sub_para.alignment = PP_ALIGN.CENTER # 目录页 slide = prs.slides.add_slide(prs.slide_layouts[6]) title_box = slide.shapes.add_textbox(Inches(1), Inches(0.5), Inches(11), Inches(0.8)) title_frame = title_box.text_frame title_frame.text = "目录" title_para = title_frame.paragraphs[0] title_para.font.size = Pt(36) title_para.font.bold = True content = [ "1. 骨折的基本概念与分类", "2. 常用影像检查方法", "3. 常见骨折类型与典型征象", "4. 骨折愈合过程", "5. 特殊骨折与易漏诊部位", "6. 国内外指南要点", "7. 报告书写规范与模板", "8. 病例讨论", "9. 考核与答疑" ] for i, text in enumerate(content): box = slide.shapes.add_textbox(Inches(1.5), Inches(1.5 + i*0.6), Inches(10), Inches(0.5)) frame = box.text_frame frame.text = text para = frame.paragraphs[0] para.font.size = Pt(24) # 其他页面可按同样方式添加... # 保存PPTX prs.save("骨折影像诊断与报告规范.pptx")
最新发布
09-06
Python-pptx库为创建、修改和操作PowerPoint演示文稿提供了强大而便捷的方式。以下是对创建PPT代码的优化及相关使用方法介绍: ### 代码优化 原代码创建了一个简单的PPT,添加了标题页。可以从以下几个方面进行优化: - **封装性提升**:将一些重复的操作封装成函数,提高代码的可维护性和复用性。 - **灵活性增强**:允许用户自定义更多的参数,如字体颜色、对齐方式等。 以下是优化后的代码: ```python from pptx import Presentation from pptx.util import Inches, Pt from pptx.enum.text import PP_ALIGN from pptx.dml.color import RGBColor class PPTCreator: def __init__(self): self.prs = Presentation() def add_title_slide(self, title, subtitle=None, title_size=44, subtitle_size=32, title_bold=True, title_align=PP_ALIGN.CENTER): """添加标题页""" slide = self.prs.slides.add_slide(self.prs.slide_layouts[0]) title_shape = slide.shapes.title subtitle_shape = slide.placeholders[1] # 设置标题 title_shape.text = title self._set_text_style(title_shape, title_size, title_bold, title_align) # 设置副标题 if subtitle: subtitle_shape.text = subtitle self._set_text_style(subtitle_shape, subtitle_size, False, PP_ALIGN.CENTER) return slide def _set_text_style(self, shape, size, bold, align): """设置文本样式""" paragraph = shape.text_frame.paragraphs[0] paragraph.font.size = Pt(size) paragraph.font.bold = bold paragraph.alignment = align def save(self, filename): """保存PPT文件""" self.prs.save(filename) # 使用示例 ppt = PPTCreator() ppt.add_title_slide("超级厉害的PPT", "powered by Python", title_size=48, subtitle_size=36) ppt.save('demo_optimized.pptx') ``` ### 其他使用方法 - **添加内容页**:除了标题页,还可以添加其他类型的幻灯片,如内容页。 ```python def add_content_slide(self, title, content): """添加内容页""" slide = self.prs.slides.add_slide(self.prs.slide_layouts[1]) title_shape = slide.shapes.title body_shape = slide.placeholders[1] # 设置标题 title_shape.text = title self._set_text_style(title_shape, 32, True, PP_ALIGN.CENTER) # 设置内容 tf = body_shape.text_frame p = tf.add_paragraph() p.text = content self._set_text_style(body_shape, 24, False, PP_ALIGN.LEFT) return slide ``` - **添加图片**:可以在幻灯片中添加图片。 ```python def add_image_slide(self, image_path, title=None): """添加图片页""" slide = self.prs.slides.add_slide(self.prs.slide_layouts[5]) if title: title_shape = slide.shapes.title title_shape.text = title self._set_text_style(title_shape, 32, True, PP_ALIGN.CENTER) left = Inches(1) top = Inches(2) pic = slide.shapes.add_picture(image_path, left, top, width=Inches(6), height=Inches(4)) return slide ``` ### 注意事项 - python-pptx中没有提供删除PPT页面的方法,因为删除页面的操作比较复杂,容易出错。一些代码只是不让页面在PPT中展示,而非真正删除这一页PPT [^2]。 - 创建新演示文稿,所谓的“默认模板”实际上只是一个没有任何幻灯片的幻灯片文件,与安装的python-pptx包一起存储 [^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DataLab

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值