Email → Excel 传输 完整工作流:从 WP 邮箱下载附件 → 读取数据 → 填充 Excel 文件。
import imaplib, ssl, os, base64, time
import email
m = imaplib.IMAP4_SSL('imap.wp.pl', 993, ssl_context=ssl.create_default_context())
m.login('USER_EMAIL', 'USER_APP_PASSWORD')
m.select('INBOX')
time.sleep(0.5)
_, messages = m.search(None, 'FROM "adres@wp.pl" SUBJECT "fraza"')
ids = messages[0].split()
uid = ids[-1] # 最新
_, msg_data = m.fetch(uid, '(RFC822)')
msg = email.message_from_bytes(msg_data[0][1])
os.makedirs('/tmp/mail_attachments', exist_ok=True)
for part in msg.walk():
if part.get_content_disposition() == 'attachment':
filename = part.get_filename()
data = part.get_payload(decode=True)
if data and 'xlsx' in filename:
if '=?utf-8?b?' in filename:
fname = base64.b64decode(filename.split('?')[3]).decode('utf-8')
else:
fname = filename
with open(f'/tmp/mail_attachments/{fname}', 'wb') as f:
f.write(data)
m.logout()
xlsx 文件本质是 ZIP。无需外部库:
import zipfile, re
def read_xlsx(path):
with zipfile.ZipFile(path) as z:
with z.open('xl/worksheets/sheet1.xml') as f:
content = f.read().decode('utf-8')
with z.open('xl/sharedStrings.xml') as f:
ss = f.read().decode('utf-8')
strings = re.findall(r']
>([^<]+)', ss)
cells = re.findall(r'])>(.*?)', content, re.DOTALL)
data = {}
for cell_ref, attrs, cell_content in cells:
v = re.search(r'([^<]+)', cell_content)
if v:
val = v.group(1)
if 't="s"' in attrs:
idx = int(val)
val = strings[idx] if idx < len(strings) else val
data[cell_ref] = val
return data
- 填充 Excel 文件(PowerShell + COM)
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
$wb = $excel.Workbooks.Open("C:\sciezka\do\pliku.xlsx")
$ws = $wb.ActiveSheet
# 插入值(行, 列)——Excel 列:B=2, C=3, D=4, E=5, F=6, G=7, H=8
$ws.Cells.Item(6,5).Value2 = 2450000 # E6
$ws.Cells.Item(6,6).Value2 = 2380000 # F6
$ws.Cells.Item(6,7).Value2 = 2520000 # G6
$ws.Cells.Item(6,8).Value2 = 7350000 # H6
$wb.Save()
$wb.Close($false)
$excel.Quit()
- 向 Windows 传文件(MCP bridge)
MCP bridge: http://172.17.0.1:3001/mcp(仅 Linux VM 可访问)
小文件(<200 KB):
import base64, json
with open('/tmp/plik.xlsx', 'rb') as f:
b64 = base64.b64encode(f.read()).decode()
payload = {
"jsonrpc": "2.0", "id": 1, "method": "tools/call",
"params": {
"name": "shell_ps",
"arguments": {
"cmd": f'[IO.File]::WriteAllBytes("C:\\sciezka\\plik.xlsx", [Convert]::FromBase64String("{b64}"))'
}
}
}
大文件——分步:
fs_write → 将 base64 写入 TEMP
shell_ps → [IO.File]::WriteAllBytes 从 TEMP 读取
登录信息(用户填写)
Email: USER_EMAIL(例:pentom6@wp.pl)
应用密码: USER_APP_PASSWORD
IMAP: imap.wp.pl:993
PowerShell 模板
批量插入值:
$ws.Cells.Item(row, col).Value2 = value # 行 6-20 为数据行,列 E(5), F(6), G(7), H(8)
完整工作流(读取 → 修改 → 保存):
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
$wb = $excel.Workbooks.Open("C:\sciezka\wejsciowa.xlsx")
$ws = $wb.ActiveSheet
# 插入值
$ws.Cells.Item(6,5).Value2 = 2450000
# …更多单元格
$wb.SaveAs("C:\sciezka\wyjsciowa.xlsx", 51) # 51 = xlsx
$wb.Close($false)
$excel.Quit()