diff --git a/src/router/modules/python.ts b/src/router/modules/python.ts index 692d8e4..62a954b 100644 --- a/src/router/modules/python.ts +++ b/src/router/modules/python.ts @@ -4,6 +4,10 @@ const titleArr = [ key: 'init', title: '初始化环境和工程', }, + { + key: 'poetry', + title: 'windows下使用poetry', + }, ]; // @/views/demo/**/*.vue diff --git a/src/views/python/jsForPython/JavaScript 工程师的 Python 指南.epub b/src/views/python/jsForPython/JavaScript 工程师的 Python 指南.epub new file mode 100644 index 0000000..104f245 Binary files /dev/null and b/src/views/python/jsForPython/JavaScript 工程师的 Python 指南.epub differ diff --git a/src/views/python/jsForPython/index.vue b/src/views/python/jsForPython/index.vue new file mode 100644 index 0000000..10a344a --- /dev/null +++ b/src/views/python/jsForPython/index.vue @@ -0,0 +1,15 @@ + + + + diff --git a/src/views/python/poetry/index.vue b/src/views/python/poetry/index.vue new file mode 100644 index 0000000..0fabab6 --- /dev/null +++ b/src/views/python/poetry/index.vue @@ -0,0 +1,18 @@ + + + diff --git a/src/views/python/poetry/poetry.md b/src/views/python/poetry/poetry.md new file mode 100644 index 0000000..e2b9d64 --- /dev/null +++ b/src/views/python/poetry/poetry.md @@ -0,0 +1,26 @@ +# python工程化poetry(node中的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 \ No newline at end of file diff --git a/src/views/python/pptx/index.vue b/src/views/python/pptx/index.vue new file mode 100644 index 0000000..ad3146e --- /dev/null +++ b/src/views/python/pptx/index.vue @@ -0,0 +1,18 @@ + + + diff --git a/src/views/python/pptx/pptx.md b/src/views/python/pptx/pptx.md new file mode 100644 index 0000000..2c7c053 --- /dev/null +++ b/src/views/python/pptx/pptx.md @@ -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"` 在某些语言版本中可能是本地化名称)。 \ No newline at end of file