简体   繁体   中英

Openpyxl not writing to cells properly

Attemping to write a Python script that will open an existing invoice template I have created and prompt for the info to fill in the cells. The file is created with the intended name, the print statement at the end successfully prints the value intended for cell C5, and the script finishes without errors. Upon opening the newly created Excel file, no data appears in cells C3 and C5. Initially I was missing the save statement and assumed that was my problem, however after adding it I'm still running into the same issue. Please help!

import openpyxl


#prompt for invoice number

invNumber = raw_input('What is this invoice number? ')
#convert string to integer
invNumberInt = int(invNumber)

file ='test.xlsx'

wb = openpyxl.load_workbook(file)

ws = wb.get_sheet_by_name('Sheet1')


ws['C3'] = invNumberInt
#invoice due date

dueDate = raw_input('When is this invoice due? mm/dd/yyyy: ')
ws['C5'] = dueDate


wb.save(filename = "invoice%d.xlsx" % invNumberInt)

print ws['C5'].value
  1. What happens if you open the file in Excel and save it (that corrects some openpyxl problems for me, where it has not fully updated an excel 2016 xlsx worksheet).
  2. What does a file compare show between test.xlsx and invoiceN.xlsx?

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