简体   繁体   中英

Using python to autofill a word.docx from excel file

I'm about halfway through Automate the Boring Stuff with Python textbook and video tutorials, however I have a big project at work where I need to autopopulate 60 Chemical Purchase Review documents that we can't seem to find. Rather than fill them out individually, I'd like to use what I've learned so far. I've had to jump ahead in chapters, but I can't seem to figure out how to get past the last line of code. Basically, I have an excel spreadsheet with four columns of information I need to be input into certain areas on the word document form template. I have "AAAA, BBBB..." in the word doc as a something to be found and replaced.

import openpyxl,os,docx,re

os.chdir(r'C:\Users\MYUSERNAME\OneDrive\Documents\Programming\ChemInv')

wb = openpyxl.load_workbook('cheminv.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
doc = docx.Document('ChemPurchaseForm_.docx')
fillObj = ('AAAA','BBBB','CCCC','DDDD')

for a in range(1,61):
    for b in range(1,5):
        fill = sheet.cell(row=a,column=b).value
        for x in range(len(fillObj)):
            inputRegex = re.compile(fillObj[x])
            inputRegex.sub(fill,doc)

        doc.save('ChemPurcaseForm_' + fill + '.docx')   

I'm getting this error:

Traceback (most recent call last):
    File "C:/Users/MYUSERNAME/OneDrive/Documents/Programming/ChemInv/autofill.py", line 
15, in <module>
    inputRegex.sub(fill,doc)
TypeError: expected string or bytes-like object

I'm assuming that either the "fill" variable or "doc" variable are not binary or string values?

Thank you in advance for help!

To debug this, you'll need to figure out which of the values are not binary or string values. A convenient way is to begin adding print statements for each value. For instance, you might try

print(fill)
print(doc)
print(type(fill))
print(type(doc))

I don't know exactly how the docx module works, but two hypotheses occur to me:

  1. doc is not the appropriate type for the sub function; you'll have to cast the object to something different, or access it a different way if that's the case.
  2. fill is None . That's easier to fix, it means you're not reading the Excel document properly.

Reading the docx documentation, I lean towards 1, since it doesn't look like it's a byte or string object, or a byte or string-compatible object, and so the sub method won't be able to properly operate on it; if that's correct, read the python-docx docs for more details that might help you figure out what you need to do. I'd explore what properties exist on your document, it seems there are some for directly accessing the text.

Good luck!

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