簡體   English   中英

如何將我使用 Pandas (python) 創建的 .xlsx 文件保存到不同的文件位置?

[英]How do I get my .xlsx file I created using Pandas (python) to save to a different file location?

我剛剛開始使用 Pandas,我已經使用 Pandas 將我的 xls 文件轉換為 xlsx 文件,但是我現在希望將該文件保存到其他位置,例如 OneDrive 我想知道您是否可以幫助我?

這是我為它編寫的代碼:

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
import os
 
#Deleting original file
path = (r"C:\Users\MQ\Downloads\Incident Report.xls")
os.remove(path)
print("Original file has been deleted :)")

#Identifying the xls file
excel_file_1 = 'Incident Report.xls'
df_first_shift = pd.read_excel(r'C:\Users\MQ\3D Objects\New Folder\Incident Report.xls')
print(df_first_shift)

#combining data
df_all = pd.concat([df_first_shift])
print(df_all)

#Creating the .xlsx file
df_all.to_excel("Incident_Report.xlsx")

通過傳入您的目標路徑來使用 pd.ExcelWriter!

destination_path = "path\\to\\your\\onedrive\\filename.xlsx"
writer = pd.ExcelWriter(destination_path , engine='xlsxwriter')
df.to_excel(writer, sheet_name='sheetname')
writer.save()

要寫入雲 OneDrive,建議使用以下代碼。 我沒有運行它,但提供它作為建議。

參考www.lieben.nu上傳文件到 onedrive 的例子

import requests 
import io
import pandas as pd

def cloudOneDrive(filename, bytesIO):
    '''
    Reference : https://www.lieben.nu/liebensraum/2019/04/uploading-a-file-to-onedrive-for-business-with-python/

    Write to cloud (bytesIO) 

    '''

    data = {'grant_type':"client_credentials", 
            'resource':"https://graph.microsoft.com", 
            'client_id':'XXXXX', 
            'client_secret':'XXXXX'} 
    URL = "https://login.windows.net/YOURTENANTDOMAINNAME/oauth2/token?api-version=1.0"

    # FIXME: put coder top open OneDrive file here as bytes stream
    r = requests.put(URL+"/"+filename+":/content", data=bytesIO, headers=headers)

    if r.status_code == 200 or r.status_code == 201:
        print("succeeded")
        return True
    else:
        print("Fail", r.status_code) 

fn = 'junk.xlsx'
with io.BytesIO() as bio:
    with pd.ExcelWriter(bio, mode='wb') as xio:  
        df.to_excel(bio, sheet_name='sh1')
    bio.seek(0)
    cloudOneDrive(fn, bio)

暫無
暫無

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

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