![](/img/trans.png)
[英]How to copy data from One Excel sheet to another sheet for same Workbook Using Python?
[英]Python code not writing output in excel sheet but is able to take input from another sheet in same workbook
背景:我正在從 Web 獲取符號的選項鏈,然后將其寫入 Excel 工作表。 我在同一個工作簿中還有另一個 excel 表,我從中獲取輸入以運行程序。 所有這些都是我用 excel 2016 做的。
程序中的代碼示例作為整個程序很長:
import xlwings as xw
excel_file = 'test.xlsx'
wb = xw.Book(excel_file)
wb.save()
# Fetching User input for Script/Ticker else it will be set to NIFTY as default
try:
Script_Input = pd.read_excel(excel_file, sheet_name = 'Input_Options', usecols = 'C')
script = Script_Input.iloc[0,0]
except:
script = 'NIFTY'
# Writing data in the sheet
sht_name = script + '_OC'
try:
wb.sheets.add(sht_name)
print('new sheet added')
wb.save()
except:
pass
# print('sheet already present')
# directing pointer towards current sheet to be written
sheet = wb.sheets(sht_name)
sheet.range('A4').options(index = False, header = False).value = df
sheet.range('B1').value = underlying
sheet.range('C1').value = underlying_Value
# sheet.range('A3').options(index = False, header = False).value = ce_data_final
# sheet.range('J3').options(index = False, header = False).value = pe_data_final
wb.save()
問題:從昨天開始,我可以使用 excel 2016 打開我的 excel 工作簿並更改我的程序的輸入,但是,我沒有在從程序中獲取輸出的工作表中寫入任何數據。 該程序運行完美,因為我可以在終端上測試輸出。 此外,一旦我刪除了工作表,就不會創建新的工作表。 我的嘗試:我已經卸載了所有其他版本的 excel,所以現在只有 excel 2016 存在。 我已確保所有相應的文件格式都使用 excel 2016 作為默認應用程序。
另請注意,2 天前我能夠在相應的工作表中完美地寫入數據,但現在我無法這樣做。
任何幫助表示贊賞...
對所有試圖解決這個問題的人感到抱歉。 在@buran詢問'df'之后,我查看了我的代碼,發現在將'df'寫入工作表之前我有一個return
語句(我創建了一個單獨的函數來在excel中寫入數據)。 現在我已將該語句移至適當的位置,代碼運行正常。 我非常抱歉,因為我沒有意識到問題在第一位,並認為它與 excel 和 python 有關。 現在程序運行完美,我得到了我想要的輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.