从 .msg 附件转换 .xls 文件,而不将 .xls 作为临时文件存储在磁盘上

发布于 2025-01-11 13:16:33 字数 1209 浏览 0 评论 0原文

我正在尝试从 .msg 文件中提取所有 .xls 附件文件,然后将它们转换为 .xlsx 所以我当前的代码流程如下所示:

msg = outlook.OpenSharedItem(os.path.join(mesg_path, file))
att=msg.Attachments
for i in att:
    if i.FileName.endswith('.xls'):
        i.SaveAsFile(os.path.join('C:/some_path_goes_here', i.FileName))

然后我使用一些其他代码打开这些 .xls 文件并转换为 .xlsx 文件:

for file in dir_list:
if file.endswith('.xls'):
    file_with_destination = os.path.join(destination_path, file)
    file_with_path = os.path.join(source_path, file)
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    wb = excel.Workbooks.Open(file_with_path)
    wb.SaveAs(file_with_destination + "x", FileFormat = 51)    
    wb.Close(True)                                        
    excel.Application.Quit()

它完成了这项工作。但我很好奇,在从 .msg 附件中提取 .xls 文件时是否可以直接转换它们,这样我就不必创建临时 .xls 文件并将其存储在磁盘上。有什么办法可以做到这样的事情:?

    msg = outlook.OpenSharedItem(os.path.join(mesg_path, file))
    att=msg.Attachments
    for i in att:
       if i.FileName.endswith('.xls'):
       #i.SaveAsFile(os.path.join('C:/some_path_goes_here', i.FileName))
       #instead of saving convert to .xlsx and save to destination folder

I am trying to extract all .xls attachment files from .msg files and then convert them to .xlsx
So my current code flow looks like this:

msg = outlook.OpenSharedItem(os.path.join(mesg_path, file))
att=msg.Attachments
for i in att:
    if i.FileName.endswith('.xls'):
        i.SaveAsFile(os.path.join('C:/some_path_goes_here', i.FileName))

and then I use some other code to open these .xls files and convert to .xlsx files:

for file in dir_list:
if file.endswith('.xls'):
    file_with_destination = os.path.join(destination_path, file)
    file_with_path = os.path.join(source_path, file)
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    wb = excel.Workbooks.Open(file_with_path)
    wb.SaveAs(file_with_destination + "x", FileFormat = 51)    
    wb.Close(True)                                        
    excel.Application.Quit()

It does the job. But I am curious if I could convert .xls files strait when extracting them from .msg attachments so I don't have to create temp .xls file and store it on disk. Is there any way to do it something like that: ?

    msg = outlook.OpenSharedItem(os.path.join(mesg_path, file))
    att=msg.Attachments
    for i in att:
       if i.FileName.endswith('.xls'):
       #i.SaveAsFile(os.path.join('C:/some_path_goes_here', i.FileName))
       #instead of saving convert to .xlsx and save to destination folder

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏大泽ㄣ 2025-01-18 13:16:33

您可以直接读取文件并将其保存为xlsx格式到所需位置,如下面的代码所示。只需将 file_destination 变量更新为要保存文件的目标路径即可。

import io
import pandas as pd

msg = outlook.OpenSharedItem(os.path.join(mesg_path, file))
att=msg.Attachments
for i in att:
    if i.FileName.endswith('.xls'):
        file_content_bytes = i.content
        my_file_io = io.BytesIO(file_content_bytes)
        # Below goes your file name or the destination path
        file_destination = "filename.xlsx"
        pd.read_excel(io=my_file_io).to_excel(file_destination, index=False)

You can directly read and save the file into xlsx format at desired location as mentioned in below code. Just update the file_destination variable as to the desitnation path where you want to save the file.

import io
import pandas as pd

msg = outlook.OpenSharedItem(os.path.join(mesg_path, file))
att=msg.Attachments
for i in att:
    if i.FileName.endswith('.xls'):
        file_content_bytes = i.content
        my_file_io = io.BytesIO(file_content_bytes)
        # Below goes your file name or the destination path
        file_destination = "filename.xlsx"
        pd.read_excel(io=my_file_io).to_excel(file_destination, index=False)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文