xp3-tool 4.2__tar.gz → 5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- xp3_tool-5.0/PKG-INFO +213 -0
- xp3_tool-5.0/README.md +191 -0
- {xp3_tool-4.2 → xp3_tool-5.0}/setup.py +2 -2
- xp3_tool-5.0/xp3_tool/__init__.py +10 -0
- {xp3_tool-4.2 → xp3_tool-5.0}/xp3_tool/xp3_tool.py +94 -1
- xp3_tool-5.0/xp3_tool.egg-info/PKG-INFO +213 -0
- {xp3_tool-4.2 → xp3_tool-5.0}/xp3_tool.egg-info/requires.txt +1 -0
- xp3_tool-4.2/PKG-INFO +0 -22
- xp3_tool-4.2/README.md +0 -1
- xp3_tool-4.2/xp3_tool/__init__.py +0 -9
- xp3_tool-4.2/xp3_tool.egg-info/PKG-INFO +0 -22
- {xp3_tool-4.2 → xp3_tool-5.0}/setup.cfg +0 -0
- {xp3_tool-4.2 → xp3_tool-5.0}/xp3_tool.egg-info/SOURCES.txt +0 -0
- {xp3_tool-4.2 → xp3_tool-5.0}/xp3_tool.egg-info/dependency_links.txt +0 -0
- {xp3_tool-4.2 → xp3_tool-5.0}/xp3_tool.egg-info/top_level.txt +0 -0
xp3_tool-5.0/PKG-INFO
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: xp3_tool
|
3
|
+
Version: 5.0
|
4
|
+
Summary: xp_t3_tool
|
5
|
+
Home-page:
|
6
|
+
Author: XuPeng
|
7
|
+
Author-email: xupeng23456@126.com
|
8
|
+
License: MIT
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
10
|
+
Classifier: Programming Language :: Python
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
12
|
+
Classifier: Programming Language :: Python :: 3.6
|
13
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
14
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
15
|
+
Requires-Python: >=3.10.0
|
16
|
+
Description-Content-Type: text/markdown
|
17
|
+
Requires-Dist: openai
|
18
|
+
Requires-Dist: pandas
|
19
|
+
Requires-Dist: oss2
|
20
|
+
Requires-Dist: python-dotenv
|
21
|
+
|
22
|
+
|
23
|
+
# Excel 与 OSS 处理工具库
|
24
|
+
|
25
|
+
这是xp的私人工具库。
|
26
|
+
|
27
|
+
## 功能特性
|
28
|
+
|
29
|
+
### 🤖 AI 对话功能 (CallAi 类)
|
30
|
+
- 支持 OpenAI 兼容的 API 调用
|
31
|
+
- 可自定义系统提示词和模型参数
|
32
|
+
- 灵活的对话配置(temperature、top_p)
|
33
|
+
|
34
|
+
### ☁️ OSS 文件管理 (ExcelOSSHandler 类)
|
35
|
+
- Excel 文件上传到阿里云 OSS
|
36
|
+
- 从 OSS 下载 Excel 文件并转换为 pandas DataFrame
|
37
|
+
- 完整的错误处理和文件验证
|
38
|
+
|
39
|
+
### 📧 数据导出与邮件发送
|
40
|
+
- 将 DataFrame 导出为 Excel 文件
|
41
|
+
- 自动通过邮件发送 Excel 附件
|
42
|
+
- 支持 HTML 格式的邮件内容
|
43
|
+
|
44
|
+
## 安装依赖
|
45
|
+
|
46
|
+
```bash
|
47
|
+
pip install openai pandas oss2 python-dotenv openpyxl
|
48
|
+
```
|
49
|
+
|
50
|
+
## 快速开始
|
51
|
+
|
52
|
+
### 1. AI 对话功能
|
53
|
+
|
54
|
+
```python
|
55
|
+
from your_module import CallAi
|
56
|
+
|
57
|
+
# 初始化 AI 客户端
|
58
|
+
ai = CallAi(
|
59
|
+
api_key="your-api-key",
|
60
|
+
base_url="https://api.openai.com/v1",
|
61
|
+
model="qwen-plus" # 默认模型
|
62
|
+
)
|
63
|
+
|
64
|
+
# 设置系统提示词
|
65
|
+
ai.prompt = "你是一个有用的助手"
|
66
|
+
|
67
|
+
# 进行对话
|
68
|
+
response = ai.chat("你好,请介绍一下自己")
|
69
|
+
print(response)
|
70
|
+
```
|
71
|
+
|
72
|
+
### 2. OSS 文件管理
|
73
|
+
|
74
|
+
```python
|
75
|
+
from your_module import ExcelOSSHandler
|
76
|
+
|
77
|
+
# 初始化 OSS 处理器
|
78
|
+
oss_handler = ExcelOSSHandler(
|
79
|
+
access_key_id="your-access-key-id",
|
80
|
+
access_key_secret="your-access-key-secret",
|
81
|
+
endpoint="https://oss-cn-hangzhou.aliyuncs.com",
|
82
|
+
bucket_name="your-bucket-name"
|
83
|
+
)
|
84
|
+
|
85
|
+
# 上传 Excel 文件到 OSS
|
86
|
+
success = oss_handler.upload_excel_to_oss(
|
87
|
+
local_file_path="local_file.xlsx",
|
88
|
+
oss_file_path="oss/path/file.xlsx"
|
89
|
+
)
|
90
|
+
|
91
|
+
# 从 OSS 下载 Excel 并转换为 DataFrame
|
92
|
+
df = oss_handler.get_excel_from_oss("oss/path/file.xlsx")
|
93
|
+
```
|
94
|
+
|
95
|
+
### 3. 数据导出与邮件发送
|
96
|
+
|
97
|
+
```python
|
98
|
+
from your_module import export_to_excel_and_email
|
99
|
+
import pandas as pd
|
100
|
+
|
101
|
+
# 创建示例数据
|
102
|
+
df = pd.DataFrame({
|
103
|
+
'Name': ['Alice', 'Bob', 'Charlie'],
|
104
|
+
'Age': [25, 30, 35],
|
105
|
+
'City': ['Beijing', 'Shanghai', 'Guangzhou']
|
106
|
+
})
|
107
|
+
|
108
|
+
# 导出数据并发送邮件
|
109
|
+
result = export_to_excel_and_email(
|
110
|
+
df=df,
|
111
|
+
receiver="recipient@example.com",
|
112
|
+
subject="数据导出报告",
|
113
|
+
sender="your-email@163.com",
|
114
|
+
password="your-email-password"
|
115
|
+
)
|
116
|
+
|
117
|
+
print(result)
|
118
|
+
```
|
119
|
+
|
120
|
+
## 环境变量配置
|
121
|
+
|
122
|
+
创建 `.env` 文件来管理敏感信息:
|
123
|
+
|
124
|
+
```env
|
125
|
+
# OpenAI 配置
|
126
|
+
OPENAI_API_KEY=your-openai-api-key
|
127
|
+
OPENAI_BASE_URL=https://api.openai.com/v1
|
128
|
+
|
129
|
+
# 阿里云 OSS 配置
|
130
|
+
OSS_ACCESS_KEY_ID=your-access-key-id
|
131
|
+
OSS_ACCESS_KEY_SECRET=your-access-key-secret
|
132
|
+
OSS_ENDPOINT=oss-cn-hangzhou.aliyuncs.com
|
133
|
+
OSS_BUCKET_NAME=your-bucket-name
|
134
|
+
|
135
|
+
# 邮件配置
|
136
|
+
email_sender=your-email@163.com
|
137
|
+
email_password=your-email-authorization-code
|
138
|
+
```
|
139
|
+
|
140
|
+
## API 参考
|
141
|
+
|
142
|
+
### CallAi 类
|
143
|
+
|
144
|
+
#### 初始化参数
|
145
|
+
- `api_key`: OpenAI API 密钥
|
146
|
+
- `base_url`: API 基础地址
|
147
|
+
- `model`: 模型名称(默认:'qwen-plus')
|
148
|
+
|
149
|
+
#### 方法
|
150
|
+
- `chat(text, top_p=0.9, temperature=0.7)`: 发送对话请求
|
151
|
+
|
152
|
+
### ExcelOSSHandler 类
|
153
|
+
|
154
|
+
#### 初始化参数
|
155
|
+
- `access_key_id`: 阿里云访问密钥 ID
|
156
|
+
- `access_key_secret`: 阿里云访问密钥 Secret
|
157
|
+
- `endpoint`: OSS 服务端点
|
158
|
+
- `bucket_name`: 存储桶名称
|
159
|
+
|
160
|
+
#### 方法
|
161
|
+
- `upload_excel_to_oss(local_file_path, oss_file_path)`: 上传 Excel 文件
|
162
|
+
- `get_excel_from_oss(oss_file_path)`: 下载并转换 Excel 文件为 DataFrame
|
163
|
+
|
164
|
+
### ExportToEmail函数
|
165
|
+
|
166
|
+
#### 参数
|
167
|
+
- `df`: 要导出的 pandas DataFrame
|
168
|
+
- `receiver`: 收件人邮箱地址(默认:'xupeng23456@126.com')
|
169
|
+
- `subject`: 邮件主题
|
170
|
+
- `sender`: 发件人邮箱
|
171
|
+
- `password`: 发件人邮箱密码/授权码
|
172
|
+
|
173
|
+
#### 返回值
|
174
|
+
返回包含操作结果的字典:
|
175
|
+
```python
|
176
|
+
{
|
177
|
+
"status": "success" | "failed",
|
178
|
+
"message": "描述信息",
|
179
|
+
"file_path": "临时文件路径",
|
180
|
+
"email_sent": True | False,
|
181
|
+
"row_count": 数据行数,
|
182
|
+
"timestamp": "时间戳"
|
183
|
+
}
|
184
|
+
```
|
185
|
+
|
186
|
+
## 注意事项
|
187
|
+
|
188
|
+
1. **文件格式**: 仅支持 `.xlsx` 和 `.xls` 格式的 Excel 文件
|
189
|
+
2. **邮件服务**: 默认使用 163 邮箱的 SMTP 服务,如需使用其他邮箱请修改 SMTP 配置
|
190
|
+
3. **临时文件**: 导出的 Excel 文件会在邮件发送成功后自动清理
|
191
|
+
4. **错误处理**: 所有操作都包含完整的异常捕获和错误信息返回
|
192
|
+
|
193
|
+
## 错误排查
|
194
|
+
|
195
|
+
### 常见问题
|
196
|
+
|
197
|
+
1. **OSS 连接失败**
|
198
|
+
- 检查 Access Key 和 Secret 是否正确
|
199
|
+
- 确认 endpoint 和 bucket name 是否正确
|
200
|
+
|
201
|
+
2. **邮件发送失败**
|
202
|
+
- 确认发件人邮箱密码/授权码是否正确
|
203
|
+
- 检查 SMTP 服务器和端口配置
|
204
|
+
- 确认网络连接正常
|
205
|
+
|
206
|
+
3. **Excel 文件处理错误**
|
207
|
+
- 确认文件路径正确且文件存在
|
208
|
+
- 检查文件是否被其他程序占用
|
209
|
+
- 验证文件格式是否为支持的 Excel 格式
|
210
|
+
|
211
|
+
## 许可证
|
212
|
+
|
213
|
+
此项目基于 MIT 许可证开源。
|
xp3_tool-5.0/README.md
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
# Excel 与 OSS 处理工具库
|
2
|
+
|
3
|
+
这是xp的私人工具库。
|
4
|
+
|
5
|
+
## 功能特性
|
6
|
+
|
7
|
+
### 🤖 AI 对话功能 (CallAi 类)
|
8
|
+
- 支持 OpenAI 兼容的 API 调用
|
9
|
+
- 可自定义系统提示词和模型参数
|
10
|
+
- 灵活的对话配置(temperature、top_p)
|
11
|
+
|
12
|
+
### ☁️ OSS 文件管理 (ExcelOSSHandler 类)
|
13
|
+
- Excel 文件上传到阿里云 OSS
|
14
|
+
- 从 OSS 下载 Excel 文件并转换为 pandas DataFrame
|
15
|
+
- 完整的错误处理和文件验证
|
16
|
+
|
17
|
+
### 📧 数据导出与邮件发送
|
18
|
+
- 将 DataFrame 导出为 Excel 文件
|
19
|
+
- 自动通过邮件发送 Excel 附件
|
20
|
+
- 支持 HTML 格式的邮件内容
|
21
|
+
|
22
|
+
## 安装依赖
|
23
|
+
|
24
|
+
```bash
|
25
|
+
pip install openai pandas oss2 python-dotenv openpyxl
|
26
|
+
```
|
27
|
+
|
28
|
+
## 快速开始
|
29
|
+
|
30
|
+
### 1. AI 对话功能
|
31
|
+
|
32
|
+
```python
|
33
|
+
from your_module import CallAi
|
34
|
+
|
35
|
+
# 初始化 AI 客户端
|
36
|
+
ai = CallAi(
|
37
|
+
api_key="your-api-key",
|
38
|
+
base_url="https://api.openai.com/v1",
|
39
|
+
model="qwen-plus" # 默认模型
|
40
|
+
)
|
41
|
+
|
42
|
+
# 设置系统提示词
|
43
|
+
ai.prompt = "你是一个有用的助手"
|
44
|
+
|
45
|
+
# 进行对话
|
46
|
+
response = ai.chat("你好,请介绍一下自己")
|
47
|
+
print(response)
|
48
|
+
```
|
49
|
+
|
50
|
+
### 2. OSS 文件管理
|
51
|
+
|
52
|
+
```python
|
53
|
+
from your_module import ExcelOSSHandler
|
54
|
+
|
55
|
+
# 初始化 OSS 处理器
|
56
|
+
oss_handler = ExcelOSSHandler(
|
57
|
+
access_key_id="your-access-key-id",
|
58
|
+
access_key_secret="your-access-key-secret",
|
59
|
+
endpoint="https://oss-cn-hangzhou.aliyuncs.com",
|
60
|
+
bucket_name="your-bucket-name"
|
61
|
+
)
|
62
|
+
|
63
|
+
# 上传 Excel 文件到 OSS
|
64
|
+
success = oss_handler.upload_excel_to_oss(
|
65
|
+
local_file_path="local_file.xlsx",
|
66
|
+
oss_file_path="oss/path/file.xlsx"
|
67
|
+
)
|
68
|
+
|
69
|
+
# 从 OSS 下载 Excel 并转换为 DataFrame
|
70
|
+
df = oss_handler.get_excel_from_oss("oss/path/file.xlsx")
|
71
|
+
```
|
72
|
+
|
73
|
+
### 3. 数据导出与邮件发送
|
74
|
+
|
75
|
+
```python
|
76
|
+
from your_module import export_to_excel_and_email
|
77
|
+
import pandas as pd
|
78
|
+
|
79
|
+
# 创建示例数据
|
80
|
+
df = pd.DataFrame({
|
81
|
+
'Name': ['Alice', 'Bob', 'Charlie'],
|
82
|
+
'Age': [25, 30, 35],
|
83
|
+
'City': ['Beijing', 'Shanghai', 'Guangzhou']
|
84
|
+
})
|
85
|
+
|
86
|
+
# 导出数据并发送邮件
|
87
|
+
result = export_to_excel_and_email(
|
88
|
+
df=df,
|
89
|
+
receiver="recipient@example.com",
|
90
|
+
subject="数据导出报告",
|
91
|
+
sender="your-email@163.com",
|
92
|
+
password="your-email-password"
|
93
|
+
)
|
94
|
+
|
95
|
+
print(result)
|
96
|
+
```
|
97
|
+
|
98
|
+
## 环境变量配置
|
99
|
+
|
100
|
+
创建 `.env` 文件来管理敏感信息:
|
101
|
+
|
102
|
+
```env
|
103
|
+
# OpenAI 配置
|
104
|
+
OPENAI_API_KEY=your-openai-api-key
|
105
|
+
OPENAI_BASE_URL=https://api.openai.com/v1
|
106
|
+
|
107
|
+
# 阿里云 OSS 配置
|
108
|
+
OSS_ACCESS_KEY_ID=your-access-key-id
|
109
|
+
OSS_ACCESS_KEY_SECRET=your-access-key-secret
|
110
|
+
OSS_ENDPOINT=oss-cn-hangzhou.aliyuncs.com
|
111
|
+
OSS_BUCKET_NAME=your-bucket-name
|
112
|
+
|
113
|
+
# 邮件配置
|
114
|
+
email_sender=your-email@163.com
|
115
|
+
email_password=your-email-authorization-code
|
116
|
+
```
|
117
|
+
|
118
|
+
## API 参考
|
119
|
+
|
120
|
+
### CallAi 类
|
121
|
+
|
122
|
+
#### 初始化参数
|
123
|
+
- `api_key`: OpenAI API 密钥
|
124
|
+
- `base_url`: API 基础地址
|
125
|
+
- `model`: 模型名称(默认:'qwen-plus')
|
126
|
+
|
127
|
+
#### 方法
|
128
|
+
- `chat(text, top_p=0.9, temperature=0.7)`: 发送对话请求
|
129
|
+
|
130
|
+
### ExcelOSSHandler 类
|
131
|
+
|
132
|
+
#### 初始化参数
|
133
|
+
- `access_key_id`: 阿里云访问密钥 ID
|
134
|
+
- `access_key_secret`: 阿里云访问密钥 Secret
|
135
|
+
- `endpoint`: OSS 服务端点
|
136
|
+
- `bucket_name`: 存储桶名称
|
137
|
+
|
138
|
+
#### 方法
|
139
|
+
- `upload_excel_to_oss(local_file_path, oss_file_path)`: 上传 Excel 文件
|
140
|
+
- `get_excel_from_oss(oss_file_path)`: 下载并转换 Excel 文件为 DataFrame
|
141
|
+
|
142
|
+
### ExportToEmail函数
|
143
|
+
|
144
|
+
#### 参数
|
145
|
+
- `df`: 要导出的 pandas DataFrame
|
146
|
+
- `receiver`: 收件人邮箱地址(默认:'xupeng23456@126.com')
|
147
|
+
- `subject`: 邮件主题
|
148
|
+
- `sender`: 发件人邮箱
|
149
|
+
- `password`: 发件人邮箱密码/授权码
|
150
|
+
|
151
|
+
#### 返回值
|
152
|
+
返回包含操作结果的字典:
|
153
|
+
```python
|
154
|
+
{
|
155
|
+
"status": "success" | "failed",
|
156
|
+
"message": "描述信息",
|
157
|
+
"file_path": "临时文件路径",
|
158
|
+
"email_sent": True | False,
|
159
|
+
"row_count": 数据行数,
|
160
|
+
"timestamp": "时间戳"
|
161
|
+
}
|
162
|
+
```
|
163
|
+
|
164
|
+
## 注意事项
|
165
|
+
|
166
|
+
1. **文件格式**: 仅支持 `.xlsx` 和 `.xls` 格式的 Excel 文件
|
167
|
+
2. **邮件服务**: 默认使用 163 邮箱的 SMTP 服务,如需使用其他邮箱请修改 SMTP 配置
|
168
|
+
3. **临时文件**: 导出的 Excel 文件会在邮件发送成功后自动清理
|
169
|
+
4. **错误处理**: 所有操作都包含完整的异常捕获和错误信息返回
|
170
|
+
|
171
|
+
## 错误排查
|
172
|
+
|
173
|
+
### 常见问题
|
174
|
+
|
175
|
+
1. **OSS 连接失败**
|
176
|
+
- 检查 Access Key 和 Secret 是否正确
|
177
|
+
- 确认 endpoint 和 bucket name 是否正确
|
178
|
+
|
179
|
+
2. **邮件发送失败**
|
180
|
+
- 确认发件人邮箱密码/授权码是否正确
|
181
|
+
- 检查 SMTP 服务器和端口配置
|
182
|
+
- 确认网络连接正常
|
183
|
+
|
184
|
+
3. **Excel 文件处理错误**
|
185
|
+
- 确认文件路径正确且文件存在
|
186
|
+
- 检查文件是否被其他程序占用
|
187
|
+
- 验证文件格式是否为支持的 Excel 格式
|
188
|
+
|
189
|
+
## 许可证
|
190
|
+
|
191
|
+
此项目基于 MIT 许可证开源。
|
@@ -18,11 +18,11 @@ URL = ''
|
|
18
18
|
EMAIL = 'xupeng23456@126.com'
|
19
19
|
AUTHOR = 'XuPeng'
|
20
20
|
REQUIRES_PYTHON = '>=3.10.0'
|
21
|
-
VERSION = '
|
21
|
+
VERSION = '5.0'
|
22
22
|
|
23
23
|
# What packages are required for this module to be executed?
|
24
24
|
REQUIRED = [
|
25
|
-
'openai', 'pandas','oss2'
|
25
|
+
'openai', 'pandas','oss2','python-dotenv'
|
26
26
|
]
|
27
27
|
|
28
28
|
# What packages are optional?
|
@@ -7,6 +7,18 @@ import pandas as pd
|
|
7
7
|
import oss2
|
8
8
|
from oss2.exceptions import ClientError
|
9
9
|
from typing import Optional
|
10
|
+
from dotenv import load_dotenv
|
11
|
+
from email.mime.multipart import MIMEMultipart
|
12
|
+
from email.mime.text import MIMEText
|
13
|
+
from email.mime.base import MIMEBase
|
14
|
+
from email import encoders
|
15
|
+
from datetime import datetime
|
16
|
+
import traceback
|
17
|
+
import logging
|
18
|
+
import tempfile
|
19
|
+
import atexit
|
20
|
+
import smtplib
|
21
|
+
import shutil
|
10
22
|
|
11
23
|
def uuid(length=16):
|
12
24
|
chars = string.ascii_letters + string.digits
|
@@ -128,4 +140,85 @@ class ExcelOSSHandler:
|
|
128
140
|
return None
|
129
141
|
except Exception as e:
|
130
142
|
print(f"获取并转换文件时发生错误: {str(e)}")
|
131
|
-
return None
|
143
|
+
return None
|
144
|
+
|
145
|
+
def ExportToEmail(df, receiver = 'xupeng23456@126.com', subject: str = None,sender: str = None, password: str = None):
|
146
|
+
if not password:
|
147
|
+
load_dotenv()
|
148
|
+
password = os.getenv("email_password")
|
149
|
+
sender = os.getenv("email_sender")
|
150
|
+
|
151
|
+
TEMP_DIR = "./temp"
|
152
|
+
os.makedirs(TEMP_DIR, exist_ok=True)
|
153
|
+
try:
|
154
|
+
# 生成 Excel 文件
|
155
|
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
156
|
+
excel_filename = f"rds_query_result_{timestamp}.xlsx"
|
157
|
+
filepath = os.path.join(TEMP_DIR, excel_filename)
|
158
|
+
|
159
|
+
with pd.ExcelWriter(filepath, engine='openpyxl') as writer:
|
160
|
+
df.to_excel(writer, sheet_name='查询结果', index=False)
|
161
|
+
|
162
|
+
|
163
|
+
# 3. 发送邮件
|
164
|
+
smtp_server = 'smtp.163.com'
|
165
|
+
smtp_port = 465
|
166
|
+
|
167
|
+
if not subject:
|
168
|
+
subject = f"数据结果 - {timestamp}"
|
169
|
+
|
170
|
+
msg = MIMEMultipart()
|
171
|
+
msg['From'] = sender
|
172
|
+
msg['To'] = receiver
|
173
|
+
msg['Subject'] = subject
|
174
|
+
|
175
|
+
body = f"""
|
176
|
+
<html>
|
177
|
+
<body>
|
178
|
+
<h2>📊 数据结果已导出</h2>
|
179
|
+
<p>导出时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
|
180
|
+
<p>数据已附在邮件中,请查收。</p>
|
181
|
+
</body>
|
182
|
+
</html>
|
183
|
+
"""
|
184
|
+
msg.attach(MIMEText(body, 'html', 'utf-8'))
|
185
|
+
|
186
|
+
with open(filepath, "rb") as attachment:
|
187
|
+
part = MIMEBase('application', 'octet-stream')
|
188
|
+
part.set_payload(attachment.read())
|
189
|
+
encoders.encode_base64(part)
|
190
|
+
part.add_header(
|
191
|
+
'Content-Disposition',
|
192
|
+
f'attachment; filename="{excel_filename}"'
|
193
|
+
)
|
194
|
+
msg.attach(part)
|
195
|
+
|
196
|
+
try:
|
197
|
+
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
|
198
|
+
server.login(sender, password)
|
199
|
+
server.sendmail(sender, receiver, msg.as_string())
|
200
|
+
server.quit()
|
201
|
+
email_sent = True
|
202
|
+
os.remove(filepath)
|
203
|
+
except Exception as e:
|
204
|
+
email_sent = False
|
205
|
+
traceback.print_exc()
|
206
|
+
|
207
|
+
return {
|
208
|
+
"status": "success",
|
209
|
+
"message": f"查询成功,Excel 已生成并发送至 {receiver}。",
|
210
|
+
"file_path": filepath,
|
211
|
+
"email_sent": email_sent,
|
212
|
+
"row_count": len(df),
|
213
|
+
"timestamp": timestamp
|
214
|
+
}
|
215
|
+
|
216
|
+
except Exception as e:
|
217
|
+
traceback.print_exc()
|
218
|
+
return {
|
219
|
+
"status": "failed",
|
220
|
+
"message": str(e),
|
221
|
+
"file_path": None,
|
222
|
+
"email_sent": False
|
223
|
+
}
|
224
|
+
|
@@ -0,0 +1,213 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: xp3_tool
|
3
|
+
Version: 5.0
|
4
|
+
Summary: xp_t3_tool
|
5
|
+
Home-page:
|
6
|
+
Author: XuPeng
|
7
|
+
Author-email: xupeng23456@126.com
|
8
|
+
License: MIT
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
10
|
+
Classifier: Programming Language :: Python
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
12
|
+
Classifier: Programming Language :: Python :: 3.6
|
13
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
14
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
15
|
+
Requires-Python: >=3.10.0
|
16
|
+
Description-Content-Type: text/markdown
|
17
|
+
Requires-Dist: openai
|
18
|
+
Requires-Dist: pandas
|
19
|
+
Requires-Dist: oss2
|
20
|
+
Requires-Dist: python-dotenv
|
21
|
+
|
22
|
+
|
23
|
+
# Excel 与 OSS 处理工具库
|
24
|
+
|
25
|
+
这是xp的私人工具库。
|
26
|
+
|
27
|
+
## 功能特性
|
28
|
+
|
29
|
+
### 🤖 AI 对话功能 (CallAi 类)
|
30
|
+
- 支持 OpenAI 兼容的 API 调用
|
31
|
+
- 可自定义系统提示词和模型参数
|
32
|
+
- 灵活的对话配置(temperature、top_p)
|
33
|
+
|
34
|
+
### ☁️ OSS 文件管理 (ExcelOSSHandler 类)
|
35
|
+
- Excel 文件上传到阿里云 OSS
|
36
|
+
- 从 OSS 下载 Excel 文件并转换为 pandas DataFrame
|
37
|
+
- 完整的错误处理和文件验证
|
38
|
+
|
39
|
+
### 📧 数据导出与邮件发送
|
40
|
+
- 将 DataFrame 导出为 Excel 文件
|
41
|
+
- 自动通过邮件发送 Excel 附件
|
42
|
+
- 支持 HTML 格式的邮件内容
|
43
|
+
|
44
|
+
## 安装依赖
|
45
|
+
|
46
|
+
```bash
|
47
|
+
pip install openai pandas oss2 python-dotenv openpyxl
|
48
|
+
```
|
49
|
+
|
50
|
+
## 快速开始
|
51
|
+
|
52
|
+
### 1. AI 对话功能
|
53
|
+
|
54
|
+
```python
|
55
|
+
from your_module import CallAi
|
56
|
+
|
57
|
+
# 初始化 AI 客户端
|
58
|
+
ai = CallAi(
|
59
|
+
api_key="your-api-key",
|
60
|
+
base_url="https://api.openai.com/v1",
|
61
|
+
model="qwen-plus" # 默认模型
|
62
|
+
)
|
63
|
+
|
64
|
+
# 设置系统提示词
|
65
|
+
ai.prompt = "你是一个有用的助手"
|
66
|
+
|
67
|
+
# 进行对话
|
68
|
+
response = ai.chat("你好,请介绍一下自己")
|
69
|
+
print(response)
|
70
|
+
```
|
71
|
+
|
72
|
+
### 2. OSS 文件管理
|
73
|
+
|
74
|
+
```python
|
75
|
+
from your_module import ExcelOSSHandler
|
76
|
+
|
77
|
+
# 初始化 OSS 处理器
|
78
|
+
oss_handler = ExcelOSSHandler(
|
79
|
+
access_key_id="your-access-key-id",
|
80
|
+
access_key_secret="your-access-key-secret",
|
81
|
+
endpoint="https://oss-cn-hangzhou.aliyuncs.com",
|
82
|
+
bucket_name="your-bucket-name"
|
83
|
+
)
|
84
|
+
|
85
|
+
# 上传 Excel 文件到 OSS
|
86
|
+
success = oss_handler.upload_excel_to_oss(
|
87
|
+
local_file_path="local_file.xlsx",
|
88
|
+
oss_file_path="oss/path/file.xlsx"
|
89
|
+
)
|
90
|
+
|
91
|
+
# 从 OSS 下载 Excel 并转换为 DataFrame
|
92
|
+
df = oss_handler.get_excel_from_oss("oss/path/file.xlsx")
|
93
|
+
```
|
94
|
+
|
95
|
+
### 3. 数据导出与邮件发送
|
96
|
+
|
97
|
+
```python
|
98
|
+
from your_module import export_to_excel_and_email
|
99
|
+
import pandas as pd
|
100
|
+
|
101
|
+
# 创建示例数据
|
102
|
+
df = pd.DataFrame({
|
103
|
+
'Name': ['Alice', 'Bob', 'Charlie'],
|
104
|
+
'Age': [25, 30, 35],
|
105
|
+
'City': ['Beijing', 'Shanghai', 'Guangzhou']
|
106
|
+
})
|
107
|
+
|
108
|
+
# 导出数据并发送邮件
|
109
|
+
result = export_to_excel_and_email(
|
110
|
+
df=df,
|
111
|
+
receiver="recipient@example.com",
|
112
|
+
subject="数据导出报告",
|
113
|
+
sender="your-email@163.com",
|
114
|
+
password="your-email-password"
|
115
|
+
)
|
116
|
+
|
117
|
+
print(result)
|
118
|
+
```
|
119
|
+
|
120
|
+
## 环境变量配置
|
121
|
+
|
122
|
+
创建 `.env` 文件来管理敏感信息:
|
123
|
+
|
124
|
+
```env
|
125
|
+
# OpenAI 配置
|
126
|
+
OPENAI_API_KEY=your-openai-api-key
|
127
|
+
OPENAI_BASE_URL=https://api.openai.com/v1
|
128
|
+
|
129
|
+
# 阿里云 OSS 配置
|
130
|
+
OSS_ACCESS_KEY_ID=your-access-key-id
|
131
|
+
OSS_ACCESS_KEY_SECRET=your-access-key-secret
|
132
|
+
OSS_ENDPOINT=oss-cn-hangzhou.aliyuncs.com
|
133
|
+
OSS_BUCKET_NAME=your-bucket-name
|
134
|
+
|
135
|
+
# 邮件配置
|
136
|
+
email_sender=your-email@163.com
|
137
|
+
email_password=your-email-authorization-code
|
138
|
+
```
|
139
|
+
|
140
|
+
## API 参考
|
141
|
+
|
142
|
+
### CallAi 类
|
143
|
+
|
144
|
+
#### 初始化参数
|
145
|
+
- `api_key`: OpenAI API 密钥
|
146
|
+
- `base_url`: API 基础地址
|
147
|
+
- `model`: 模型名称(默认:'qwen-plus')
|
148
|
+
|
149
|
+
#### 方法
|
150
|
+
- `chat(text, top_p=0.9, temperature=0.7)`: 发送对话请求
|
151
|
+
|
152
|
+
### ExcelOSSHandler 类
|
153
|
+
|
154
|
+
#### 初始化参数
|
155
|
+
- `access_key_id`: 阿里云访问密钥 ID
|
156
|
+
- `access_key_secret`: 阿里云访问密钥 Secret
|
157
|
+
- `endpoint`: OSS 服务端点
|
158
|
+
- `bucket_name`: 存储桶名称
|
159
|
+
|
160
|
+
#### 方法
|
161
|
+
- `upload_excel_to_oss(local_file_path, oss_file_path)`: 上传 Excel 文件
|
162
|
+
- `get_excel_from_oss(oss_file_path)`: 下载并转换 Excel 文件为 DataFrame
|
163
|
+
|
164
|
+
### ExportToEmail函数
|
165
|
+
|
166
|
+
#### 参数
|
167
|
+
- `df`: 要导出的 pandas DataFrame
|
168
|
+
- `receiver`: 收件人邮箱地址(默认:'xupeng23456@126.com')
|
169
|
+
- `subject`: 邮件主题
|
170
|
+
- `sender`: 发件人邮箱
|
171
|
+
- `password`: 发件人邮箱密码/授权码
|
172
|
+
|
173
|
+
#### 返回值
|
174
|
+
返回包含操作结果的字典:
|
175
|
+
```python
|
176
|
+
{
|
177
|
+
"status": "success" | "failed",
|
178
|
+
"message": "描述信息",
|
179
|
+
"file_path": "临时文件路径",
|
180
|
+
"email_sent": True | False,
|
181
|
+
"row_count": 数据行数,
|
182
|
+
"timestamp": "时间戳"
|
183
|
+
}
|
184
|
+
```
|
185
|
+
|
186
|
+
## 注意事项
|
187
|
+
|
188
|
+
1. **文件格式**: 仅支持 `.xlsx` 和 `.xls` 格式的 Excel 文件
|
189
|
+
2. **邮件服务**: 默认使用 163 邮箱的 SMTP 服务,如需使用其他邮箱请修改 SMTP 配置
|
190
|
+
3. **临时文件**: 导出的 Excel 文件会在邮件发送成功后自动清理
|
191
|
+
4. **错误处理**: 所有操作都包含完整的异常捕获和错误信息返回
|
192
|
+
|
193
|
+
## 错误排查
|
194
|
+
|
195
|
+
### 常见问题
|
196
|
+
|
197
|
+
1. **OSS 连接失败**
|
198
|
+
- 检查 Access Key 和 Secret 是否正确
|
199
|
+
- 确认 endpoint 和 bucket name 是否正确
|
200
|
+
|
201
|
+
2. **邮件发送失败**
|
202
|
+
- 确认发件人邮箱密码/授权码是否正确
|
203
|
+
- 检查 SMTP 服务器和端口配置
|
204
|
+
- 确认网络连接正常
|
205
|
+
|
206
|
+
3. **Excel 文件处理错误**
|
207
|
+
- 确认文件路径正确且文件存在
|
208
|
+
- 检查文件是否被其他程序占用
|
209
|
+
- 验证文件格式是否为支持的 Excel 格式
|
210
|
+
|
211
|
+
## 许可证
|
212
|
+
|
213
|
+
此项目基于 MIT 许可证开源。
|
xp3_tool-4.2/PKG-INFO
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: xp3_tool
|
3
|
-
Version: 4.2
|
4
|
-
Summary: xp_t3_tool
|
5
|
-
Home-page:
|
6
|
-
Author: XuPeng
|
7
|
-
Author-email: xupeng23456@126.com
|
8
|
-
License: MIT
|
9
|
-
Classifier: License :: OSI Approved :: MIT License
|
10
|
-
Classifier: Programming Language :: Python
|
11
|
-
Classifier: Programming Language :: Python :: 3
|
12
|
-
Classifier: Programming Language :: Python :: 3.6
|
13
|
-
Classifier: Programming Language :: Python :: Implementation :: CPython
|
14
|
-
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
15
|
-
Requires-Python: >=3.10.0
|
16
|
-
Description-Content-Type: text/markdown
|
17
|
-
Requires-Dist: openai
|
18
|
-
Requires-Dist: pandas
|
19
|
-
Requires-Dist: oss2
|
20
|
-
|
21
|
-
|
22
|
-
本模块提供了一个简单的 CallAi 类,用于与 OpenAI 兼容的 API 服务进行交互,支持设置提示词并发送对话请求。一个处理Excel文件与阿里云OSS的交互,并提供转换为pandas DataFrame的类。同时包含一个随机字符串生成工具函数,可用于生成指定长度的字母数字混合字符串。
|
xp3_tool-4.2/README.md
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
本模块提供了一个简单的 CallAi 类,用于与 OpenAI 兼容的 API 服务进行交互,支持设置提示词并发送对话请求。一个处理Excel文件与阿里云OSS的交互,并提供转换为pandas DataFrame的类。同时包含一个随机字符串生成工具函数,可用于生成指定长度的字母数字混合字符串。
|
@@ -1,22 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: xp3_tool
|
3
|
-
Version: 4.2
|
4
|
-
Summary: xp_t3_tool
|
5
|
-
Home-page:
|
6
|
-
Author: XuPeng
|
7
|
-
Author-email: xupeng23456@126.com
|
8
|
-
License: MIT
|
9
|
-
Classifier: License :: OSI Approved :: MIT License
|
10
|
-
Classifier: Programming Language :: Python
|
11
|
-
Classifier: Programming Language :: Python :: 3
|
12
|
-
Classifier: Programming Language :: Python :: 3.6
|
13
|
-
Classifier: Programming Language :: Python :: Implementation :: CPython
|
14
|
-
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
15
|
-
Requires-Python: >=3.10.0
|
16
|
-
Description-Content-Type: text/markdown
|
17
|
-
Requires-Dist: openai
|
18
|
-
Requires-Dist: pandas
|
19
|
-
Requires-Dist: oss2
|
20
|
-
|
21
|
-
|
22
|
-
本模块提供了一个简单的 CallAi 类,用于与 OpenAI 兼容的 API 服务进行交互,支持设置提示词并发送对话请求。一个处理Excel文件与阿里云OSS的交互,并提供转换为pandas DataFrame的类。同时包含一个随机字符串生成工具函数,可用于生成指定长度的字母数字混合字符串。
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|