简体   繁体   中英

How write text on an existing xlsx file with the help of openpyxl on python?

Most of the tutorials are outdated and I am unsuccessful at writing on an xlsx file with the help of python.

Here is some code I used:

import openpyxl 
wb = openpyxl.load_workbook('C:/Users/user/Desktop/pytranslate.xlsx')
path = 'C:/Users/user/Desktop/pytranslate.xlsx'
ws = wb['Sheet1']
ws.cell(row = 1,column = 1,value = 'need for meas')
val1 = ws['A1'].value
print(val1)
wb.save(path)

The program ends with no error but I cannot see any output on the xlsx file.

I used this code to test. I created an empty excel document first. It updated correctly.

import openpyxl 
path = 'pytranslate.xlsx'
wb = openpyxl.load_workbook(path)
ws = wb['Sheet1']
ws.cell(row = 1,column = 1,value = 'need for meas')
val1 = ws['A1'].value
print(val1)
wb.save(path)

Output (excel)

电子表格

If your document is not updating, confirm you have correct permissions to the file and folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM