AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-10470463

Pedroski's questions

Martin Hope
Pedroski
Asked: 2025-04-23 13:26:23 +0800 CST

tkinter 按钮是否应始终以变量作为名称?

  • 5

我可以制作一个这样的 tkinter 按钮:

button1 = ttk.Button(root, text = "Button 1", command=lambda: button_click(button1))

或者像这样循环:

tk.Button(root, text=f"Button {i}", command=lambda x=i: button_click(x)).pack()

但是第二个按钮没有像 button1 那样的变量名。因此,我无法通过名称访问它:

def button_click(button):
        button_text = button.cget('text')
        ttk.Label(root, text = button_text).pack()

或者在这样的循环中创建时,第二个按钮实际上是否具有 tkinter 默认名称?

tkinter
  • 1 个回答
  • 22 Views
Martin Hope
Pedroski
Asked: 2024-02-24 21:31:00 +0800 CST

如何获取邮件正文的正文?

  • 3

我有这个代码,但我实际上没有收到电子邮件文本。

我必须解码电子邮件文本吗?

import sys
import imaplib
import getpass
import email
import email.header
from email.header import decode_header
import base64

def read(username, password, sender_of_interest):
    # Login to INBOX
    imap = imaplib.IMAP4_SSL("imap.mail.com", 993)
    imap.login(username, password)
    imap.select('INBOX')
    # Use search(), not status()
    # Print all unread messages from a certain sender of interest
    if sender_of_interest:
        status, response = imap.uid('search', None, 'UNSEEN', 'FROM {0}'.format(sender_of_interest))
    else:
        status, response = imap.uid('search', None, 'UNSEEN')
    if status == 'OK':
        unread_msg_nums = response[0].split()
    else:
        unread_msg_nums = []
    data_list = []
    for e_id in unread_msg_nums:
        data_dict = {}
        e_id = e_id.decode('utf-8')
        _, response = imap.uid('fetch', e_id, '(RFC822)')
        html = response[0][1].decode('utf-8')
        email_message = email.message_from_string(html)
        data_dict['mail_to'] = email_message['To']
        data_dict['mail_subject'] = email_message['Subject']
        data_dict['mail_from'] = email.utils.parseaddr(email_message['From'])
        #data_dict['body'] = email_message.get_payload()[0].get_payload()
        data_dict['body'] = email_message.get_payload()
        data_list.append(data_dict)
    print(data_list)
    # Mark them as seen
    #for e_id in unread_msg_nums:
        #imap.store(e_id, '+FLAGS', '\Seen')
    imap.logout()    
    return data_dict

所以我这样做:

print('Getting the email text bodiies ... ')
emailData = read(usermail, pw, sender_of_interest)
print('Got the data!')
for key in emailData.keys():
    print(key, emailData[key])

输出是:

mail_to [email protected]
mail_subject 获取 json 文件
mail_from ('Pedro Rodriguez', ' [email protected] ')
body [<email.message.Message object at 0x7f7d9f928df0>, <email.message.Message object at 0x7f7d9f928f70>]

如何实际获取电子邮件文本?

尝试了该建议,但似乎失败了,因为它是多部分的,并带有附加的文本文件:

def read(username, password, sender_of_interest):
    # Login to INBOX
    imap = imaplib.IMAP4_SSL("imap.qq.com", 993)
    imap.login(username, password)
    imap.select('INBOX')
    # Use search(), not status()
    # Print all unread messages from a certain sender of interest
    if sender_of_interest:
        status, response = imap.uid('search', None, 'UNSEEN', 'FROM {0}'.format(sender_of_interest))
    else:
        status, response = imap.uid('search', None, 'UNSEEN')
    if status == 'OK':
        unread_msg_nums = response[0].split()
    else:
        unread_msg_nums = []
    data_list = []
    for e_id in unread_msg_nums:
        data_dict = {}
        e_id = e_id.decode('utf-8')
        _, response = imap.uid('fetch', e_id, '(RFC822)')
        email_message = email.message_from_bytes(response[0][1], policy=default)
        #html = response[0][1].decode('utf-8')
        #email_message = email.message_from_string(html)
        data_dict['mail_to'] = email_message['To']
        data_dict['mail_subject'] = email_message['Subject']
        data_dict['mail_from'] = email.utils.parseaddr(email_message['From'])
        #data_dict['body'] = email_message.get_payload()[0].get_payload()
        #data_dict['body'] = email_message.get_payload()[0]
        data_dict['body'] = email_message.get_body('html', 'text').get_payload(decode=True)
        data_list.append(data_dict)
    print(data_list)
    # Mark them as seen
    #for e_id in unread_msg_nums:
        #imap.store(e_id, '+FLAGS', '\Seen')
    imap.logout()    
    return data_dict

得到这个错误:

emailData = read(usermail, pw, sender_of_interest) 回溯(最近一次调用):文件“/usr/lib/python3.10/idlelib/run.py”,第 578 行,在 runco​​de exec(code, self.locals) 文件中“<pyshell#126>”,第 1 行,文件“<pyshell#125>”,第 29 行,读取 TypeError: MIMEPart.get_body() 采用 1 到 2 个位置参数,但给出了 3 个

我还导入了 BeautifulSoup 以从 html 中获取文本:

from bs4 import BeautifulSoup 

# this seems to work
def read(username, password, sender_of_interest):
    # Login to INBOX
    imap = imaplib.IMAP4_SSL("imap.qq.com", 993)
    imap.login(username, password)
    imap.select('INBOX')
    # Use search(), not status()
    # Print all unread messages from a certain sender of interest
    if sender_of_interest:
        status, response = imap.uid('search', None, 'UNSEEN', 'FROM {0}'.format(sender_of_interest))
    else:
        status, response = imap.uid('search', None, 'UNSEEN')
    if status == 'OK':
        unread_msg_nums = response[0].split()
    else:
        unread_msg_nums = []
    data_list = []
    for e_id in unread_msg_nums:
        data_dict = {}
        e_id = e_id.decode('utf-8')
        _, response = imap.uid('fetch', e_id, '(RFC822)')
        email_message = email.message_from_bytes(response[0][1], policy=default)
        html = response[0][1].decode('utf-8')
        data_dict['mail_to'] = email_message['To']
        data_dict['mail_subject'] = email_message['Subject']
        data_dict['mail_from'] = email.utils.parseaddr(email_message['From'])
        body = email_message.get_body(('html', 'text')).get_payload(decode=True)
        soup = BeautifulSoup(body, 'html.parser')
        div_bs4 = soup.find('div')
        text = div_bs4.string
        data_dict['body'] = text
        data_list.append(data_dict)
    print(data_list)
    # Mark them as seen
    #for e_id in unread_msg_nums:
        #imap.store(e_id, '+FLAGS', '\Seen')
    imap.logout()    
    return data_dict

body 的输出现在是:

'body': '你能得到附件吗?

现在我需要做的就是获取附件!

python
  • 1 个回答
  • 39 Views
Martin Hope
Pedroski
Asked: 2023-12-02 18:15:57 +0800 CST

为什么这不设置宽度?

  • 5

尝试设置表格列宽度,但我不能。

根据我在这里查找的线程,类似的东西应该设置单元格宽度。不确定 EMU 是什么,但我相信 Mm(10) 将 10mm 转换为 EMU。

table.cell(0,0).width = 1097280

我做了一个非常简单的表,只有一行。我认为这应该设置列宽,但我得到的只是 3 个等宽列!尝试了很多不同的方法。

编辑:忘记输入数据

from docx import Document
from docx.shared import Pt, Mm, Cm, Inches
from docx.enum.table import WD_ALIGN_VERTICAL, WD_TABLE_ALIGNMENT


filename = "/home/pedro/myPython/docxFiles/example_table.docx"
doc = Document()
heading = 'Shiny new but awkward table" \n\n'
doc.add_heading(heading, 4)
table = doc.add_table(rows=1, cols=3)
table.alignment = WD_TABLE_ALIGNMENT.CENTER
table.autofit = False
table.allow_autofit = False
table.style = 'Table Grid'
headers = ["Name", "Age", "Occupation"]
widths = [15.9, 11.8, 26.7]
for col, col_data in enumerate(headers):
    print(col, col_data)
    table.cell(0,col).text = col_data
   
for col, width in enumerate(widths):
   print(col, width)
   table.cell(0, col).width = Mm(widths[col])
doc.save(filename)

当然,我想获取列中的最大字符串宽度并将列宽度设置为该宽度或最大值,但目前即使设置绝对值对我来说也不起作用。

我究竟做错了什么?也许是因为我用 Libre Office 打开它,而不是 MS Word??

有什么建议吗?

python-docx
  • 1 个回答
  • 32 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve