Python Forum

Full Version: Saving as docx file from excel with new filename
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

so I have a running invoice program that functions ok. But I want to tweak it to run better for me.

But having a couple of problems

Problem 1. I want to save as a docx file from the excel. It does this, but when I open the newly created docx file it says:

Word found unreadable content in demo1.docx. Do you want to recover contents of this doc?

All attempts to recover contents fail.It only has the new invoice number in it per program below ( this is a stripped down version of my program to try and solve the problems I want fixing ). So something I am trying to do causing this.

Problem 2. I want to save the file with the new invoice number as part of the filename. When I try the bottom 2 lines of the program below ( both#'ed at the moment ), with copyfile it does save but the new file is called +str(new_invoice_number)+.docx ie it doesn't insert the actual number of new_invoice_number.

With os.renames it errors with:

SyntaxError: EOL while scanning string literal, indicating the last closing parenthesis.

Any pointers?

Thank you

from openpyxl import load_workbook
import docx
import os
from shutil import copyfile

wb = load_workbook(r"C:\Users\Ron McMillan\Desktop\demo1.xlsx")  #opens the existing spreadsheet

sh = wb['Sheet']


invoice_number = sh['A4']   #takes existing invoice number
print (invoice_number.value)

new_invoice_number = invoice_number.value + 1   #increments existing invoice number
print(new_invoice_number)
sh['A4'] = new_invoice_number    #writes new invoice number into sheet



wb.save(r'C:\Users\Ron McMillan\Desktop\demo1.xlsx')   #saves workbook

wb.save(r'C:\Users\Ron McMillan\Desktop\demo1.docx')   #saves as docx



#copyfile(r'C:\Users\Ron McMillan\Desktop\demo1.xlsx',r'C:\Users\Ron McMillan\Desktop\+str(new_invoice_number)+.docx')


#os.renames(r'C:\Users\Ron McMillan\Desktop\demo1.xlsx',r'C:\Users\Ron McMillan\Desktop\'+ str(new_invoice_number) +'.xlsx')
you save wb object (i.e. excel xlsx file) with docx extension. How you expect it to open without error?
the correct approach would be to work with docx file/template and eventually transfer data from excel to word file to create the invoice

Problem 2, however note that copy file with wrong extension is just problematic
import os
src_path = r'C:\Users\Ron McMillan\Desktop\demo1.xlsx'
src_file = os.path.join(src_path, 'demo1.xlsx')
dest_file = os.path.join(src_path, f'{new_invoice_number}.docx')
print(dest_file)
copyfile(src_file, dest_file)
Hi again,

so this didn't do what I was thinking.

If you print the src_file it gives this:
C:\Users\Ron McMillan\Desktop\demo1.xlsx\demo1.xlsx

And the print dest_file gives this:
C:\Users\Ron McMillan\Desktop\demo1.xlsx\2035.xlsx (which is almost there if the demo1.xlsx part is removed)

What I need is for the dest_file to be this:
C:\Users\Ron McMillan\Desktop\2035.xlsx

I have been playing around with os.path.splitext and os.path.split as it seems I need to break up the path, and replace the filename with the newly generated number.But no progress.

Slightly breaking my mind as lost as to what to try. Am I even on the right path(no pun intended)
sorry, it's clear mistake on my part - I just copied your full path and forgot to remove the file name
import os
src_path = r'C:\Users\Ron McMillan\Desktop'
src_file = os.path.join(src_path, 'demo1.xlsx')
dest_file = os.path.join(src_path, f'{new_invoice_number}.xlsx')
print(src_file)
print(dest_file)
Thanks. Will give it a whirl.