![](/img/trans.png)
[英]Openpyxl: Code fails to write data from one Excel spreadsheet to another
[英]How to Write Data from one particular cell by storing it as a variable from one Excel to Another Excel
由於沒有提供有關源文件和目標文件以及選擇數據並將數據放入哪個單元格的特定信息,因此我提供了一個通用解決方案。 您可以使用它並識別源單元格和目標單元格來復制粘貼值。 除了被覆蓋的值外,目標文件不會發生任何變化。 樣式等將保持原樣。 希望這可以幫助。
import openpyxl
wbsource = load_workbook('inputfile.xlsx') #Open source excel from which you want to copy
wssource = wbsource['Sheet4'] #Pick sheet you want to get the data from
wbdest = load_workbook('output.xlsx') #Open destination excel into which you want to paste
wsdest = wbdest['Sheet1'] #Pick sheet you want to paste the data to
wsdest.cell(row=11, column=1).value = wssource.cell(row=3, column = 1).value #Take "value" from source cell A3 and paste to dextination cell A11
# Put your other source and destination cells here
# ...
wbdest.save('output.xlsx') #Finally save the file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.