Python Forum
Saving as docx file from excel with new filename
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving as docx file from excel with new filename
#1
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')
Reply
#2
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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)
Reply
#4
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks. Will give it a whirl.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 337 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 442 Feb-07-2024, 12:24 PM
Last Post: Viento
  no module named 'docx' when importing docx MaartenRo 1 885 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  Search Excel File with a list of values huzzug 4 1,249 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 847 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Replace a text/word in docx file using Python Devan 4 3,432 Oct-17-2023, 06:03 PM
Last Post: Devan
  output provide the filename along with the input file processed. arjunaram 1 943 Apr-13-2023, 08:15 PM
Last Post: menator01
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,112 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,070 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 818 Feb-16-2023, 08:11 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020