feat: python poetry和pptx使用

master
LCJ-MinYa 5 months ago
parent 0f51991169
commit 059ce3751c

@ -4,6 +4,10 @@ const titleArr = [
key: 'init', key: 'init',
title: '初始化环境和工程', title: '初始化环境和工程',
}, },
{
key: 'poetry',
title: 'windows下使用poetry',
},
]; ];
// @/views/demo/**/*.vue // @/views/demo/**/*.vue

@ -0,0 +1,15 @@
<template>
<iframe
class="iframe"
src="https://luckrnx09.com/python-guide-for-javascript-engineers/zh-cn/"
frameborder="0"
></iframe>
</template>
<script setup></script>
<style lang="scss" scoped>
.iframe {
width: 100%;
height: 100%;
}
</style>

@ -0,0 +1,18 @@
<template>
<div
class="markdown-body"
v-html="htmlStr"
/>
</template>
<script setup>
import { ref } from 'vue';
import { marked } from 'marked';
import { getMarkdownContent } from '@/utils/tools';
const htmlStr = ref('');
getMarkdownContent('./md/poetry.md').then((res) => {
htmlStr.value = marked(res);
});
</script>

@ -0,0 +1,26 @@
# python工程化poetrynode中的npm包管理和package包依赖
### 全局安装poetry类似node全局安装npm,yarn,pnpm
```bash
pip install poetry
```
### pip install poetry之后使用poetry命令提示'poetry'不是内部或者外部命令,也不是可运行的程序或批处理文件。
```python
# 这表明poetry没有被添加到系统的环境变量中需要配置环境变量
# 1. where poetry 找到poetry.exe的位置(poetry通常被安装在python安装目录的Scripts文件夹中, default: C:\Users\minya\AppData\Local\Programs\Python\Python39\Scripts)
# 2. 设置环境变量在系统环境变量中新建一个名为POETRY_HOME的变量值为C:\Users\minya\AppData\Local\Programs\Python\Python39\Scripts的位置切记不需要加poetry.exe的名称只需要设置Scripts文件夹的路径
# 3. 在path中新建输入%POETRY_HOME%
# 4. 重启命令行输入poetry命令如果成功则表示poetry安装成功
```
### poetry怎么单独设置项目的安装源
- 打开pyproject.toml文件
- 指定项目的安装源(注意name不能为pypi不能加default: true属性)
```toml
[[tool.poetry.source]]
name = "pingan"
url = "https://mirrors.aliyun.com/pypi/simple/"
```
### 命令行启动poetry
- 如果使用vscode这些编辑器通过命令行启动项目不要直接python main.py而是运行poetry run python main.py

@ -0,0 +1,18 @@
<template>
<div
class="markdown-body"
v-html="htmlStr"
/>
</template>
<script setup>
import { ref } from 'vue';
import { marked } from 'marked';
import { getMarkdownContent } from '@/utils/tools';
const htmlStr = ref('');
getMarkdownContent('./md/pptx.md').then((res) => {
htmlStr.value = marked(res);
});
</script>

@ -0,0 +1,172 @@
# python-pptx使用
### 安装
```bash
poetry add python-pptx
```
### 修改摘要信息,修改模版名称
```python
import os
from pptx import Presentation
from config import Config
from pptx.util import Inches
def set_slides_prop(ppt_path):
prs = Presentation(ppt_path)
text = "创意素材铺"
# 设置文档属性
prs.core_properties.title = text
prs.core_properties.subject = text
prs.core_properties.author = text
prs.core_properties.category = text
prs.core_properties.keywords = text
prs.core_properties.comments = text
# 保存演示文稿
prs.save(ppt_path)
print(f"'{os.path.basename(ppt_path)}'设置属性成功")
def batch_set_slides(directory):
for filename in os.listdir(directory):
if filename.endswith(".pptx") or filename.endswith(".ppt"):
ppt_path = os.path.join(directory, filename)
set_slides_prop(ppt_path)
# 调用批量删除函数
batch_set_slides(Config.get_latest_folder(Config.WORK_PATH))
```
# pywin32使用
### 安装
```bash
poetry add pywin32
```
### 修改摘要信息,修改模版名称
```python
import win32com.client
# 连接PowerPoint
powerpoint = win32com.client.Dispatch("PowerPoint.Application")
# 不显示PowerPoint界面
if powerpoint.Visible != 0:
powerpoint.Visible = 0
# 打开指定文件
presentation = powerpoint.Presentations.Open("xxx.pptx")
# 修改模版名称
presentation.SlideMaster.Name = "模版名称"
# 修改摘要信息
presentation.BuiltInDocumentProperties("Title").Value = "标题"
presentation.BuiltInDocumentProperties("Author").Value = "作者"
presentation.BuiltInDocumentProperties("Subject").Value = "主题"
presentation.BuiltInDocumentProperties("Keywords").Value = "关键字"
presentation.BuiltInDocumentProperties("Comments").Value = "摘要信息"
# BuiltInDocumentProperties 断点调试无法查看所有属性问题
for item in presentation.BuiltInDocumentProperties:
print(item.Name, item.Value)
# 保存文件
presentation.Save()
# 关闭文件
presentation.Close()
# 退出PowerPoint
powerpoint.Quit()
```
----
----
# presentation.BuiltInDocumentProperties("Title").Value = '新标题' 这句代码可以成功修改但是在断点调试的时候为什么无法看到Title属性
在使用 `win32com.client` 操作 PowerPoint 的 `BuiltInDocumentProperties` 时,虽然在代码中可以通过 `presentation.BuiltInDocumentProperties("Title").Value = '新标题'` 成功修改属性,但在 **断点调试时无法直接看到 `Title` 属性**,这是由以下原因导致的:
### **1. 动态 COM 对象的限制**
`win32com.client` 生成的 COM 对象是 **动态派发Late Binding** 的Python 调试器(如 VS Code、PyCharm无法直接识别这些动态属性。具体表现
- **调试器中看不到完整属性列表**`BuiltInDocumentProperties` 是一个动态集合,调试器只能显示基础的 COM 接口方法(如 `Item`, `Count`),而无法显示具体的属性名(如 `Title`, `Author`)。
- **属性需通过字符串名称访问**:调试时无法像普通 Python 对象那样直接通过 `.` 访问属性(如 `obj.Title`),必须用 `Item("Title")``("Title")` 语法。
---
### **2. 如何验证属性存在?**
#### **方法 1遍历所有属性调试时可用**
在调试器中执行以下代码片段,查看所有内置属性:
```python
props = presentation.BuiltInDocumentProperties
for i in props:
print(f"{i.Name}: {i.Value}")
```
这会输出类似以下内容:
```
Title: 新标题
Author: 张三
Subject: 年度报告
...
```
#### **方法 2直接访问属性需知道名称**
在调试器中手动测试:
```python
# 检查属性是否存在
has_title = hasattr(presentation.BuiltInDocumentProperties, "Title") # 可能返回 False动态属性无法被 hasattr 识别)
title_value = presentation.BuiltInDocumentProperties("Title").Value # 实际可运行
```
---
### **3. 为什么代码能运行但调试器看不到?**
- **COM 动态接口**PowerPoint 的 COM 对象在运行时通过 IDispatch 接口动态解析属性名称,调试器无法静态分析这些属性。
- **Python 的 `dir()` 限制**:调试器依赖 `dir(obj)` 获取对象属性列表,但 `win32com` 动态对象不会返回完整的属性名。
---
### **4. 解决方案**
#### **(1) 调试时显式打印属性**
在代码中添加临时调试语句:
```python
print([prop.Name for prop in presentation.BuiltInDocumentProperties])
```
#### **(2) 使用 `makepy` 生成静态接口**
运行以下命令生成静态包装类(提升调试体验):
```bash
python -m win32com.client.makepy "Microsoft PowerPoint 16.0 Object Library"
```
生成后,调试器可能能识别更多属性(但部分动态属性仍不可见)。
#### **(3) 查阅官方文档**
参考 [Microsoft Office 文档属性列表](https://docs.microsoft.com/en-us/office/vba/api/powerpoint.documentproperties) 确认支持的属性名。
---
### **5. 示例:安全的属性修改**
```python
def safe_modify_property(doc_props, name, value):
try:
doc_props.Item(name).Value = value
print(f"修改成功: {name} -> {value}")
except Exception as e:
print(f"修改失败(属性可能不存在或只读): {name} - {e}")
# 调用示例
safe_modify_property(presentation.BuiltInDocumentProperties, "Title", "新标题")
```
---
### **总结**
- **代码能运行但调试器看不到属性**:这是 `win32com` 动态 COM 对象的正常行为。
- **验证属性**:通过遍历 `BuiltInDocumentProperties` 或直接访问属性名(字符串形式)。
- **调试技巧**:添加打印语句或使用 `makepy` 生成静态接口。
如果问题仍存在,可能是 PowerPoint 版本差异导致属性名不同(如 `"Title"` 在某些语言版本中可能是本地化名称)。
Loading…
Cancel
Save