簡體   English   中英

如何使用 python 將 df 更新到現有的 excel 文件

[英]How to Update df to existing excel file with python

I have existing excel file for updated finished e_mail.I need to update any detail to existing excel.This's my existing excel file.It have only header Like This: 在此處輸入圖像描述

這是我的 output。它分為兩個 excel 文件。像這樣。

在此處輸入圖像描述

在 Excel 文件中,它顯示如下細節:

在此處輸入圖像描述

我希望 output 更新現有 excel 文件的詳細信息。 像這樣:

在此處輸入圖像描述

這是我的 python 代碼:

import os
import pandas as pd
import win32com.client
import time
from datetime import datetime
import openpyxl
today = datetime.today().strftime('%d%m%Y')
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6).Folders.Item("3.1done_FPA")
finishbox = outlook.GetDefaultFolder(6).Folders.Item("4.finished")
outlook = inbox.Items
mail = outlook.GetLast()

filename = "R:/ReportAFS/PaymentOutlookForm/PaymentOutlookForm_"+str(today)+".xlsx"

for mail in reversed(outlook):
    subject = mail.Subject

    df = pd.read_excel("R:/veerachai/Invoice_form/copy_file/b'"+str(subject)+"'"+"/"+"b'"+str(subject)+"'.xlsm",sheet_name ='PaymentOutlookForm')

    directory = 'R:/ReportAFS/PaymentOutlookForm'
    file = 'PaymentOutlookForm_'+str(today)+'_'+str(subject)+'.xlsx'

    if not os.path.exists(directory):
        os.makedirs(directory)
    df.to_excel(os.path.join(directory, file),index = False)

    time.sleep(1)
    mail.Move(finishbox)
    print(str(mail.Subject)+ ' working success.')

請告訴我如何解決這個問題。

Excel 可以很容易地打開和使用.csv文件。 您可以將當前的 Excel 文件轉換為.csv ,然后使用pandas輕松使用它。 它提供了具有不同modesto_csv()方法,非常適合您的需要。 Given that you just want to append a row to an already existing .csv , you can just call the to_csv() method specifying the a mode parameter and setting the header to False (since we don't want to re-write the header every時間)。

df.to_csv('my_csv.csv', mode='a', header=False)

如果你想直接使用 Excel 文件(不轉換為.csv ),你可以看看這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM