current position:Home>When the python docx template library circularly extracts Excel data, a file can be saved after each rendering, which cannot be directly generated into a word file after the cycle

When the python docx template library circularly extracts Excel data, a file can be saved after each rendering, which cannot be directly generated into a word file after the cycle

2022-02-02 20:11:53 CSDN Q & A

python It's going on excel To word Mail merge , During cyclic extraction , Cannot be generated directly in a word In the document .
 from docxtpl import DocxTemplate,InlineImagefrom docx.shared import Cmfrom mailmerge import MailMerge # Mail merge import jinja2import xlrd #excel Read def Covert_Excel_To_Word():    #  open Excel file     workbook = xlrd.open_workbook(r" Statistical table .xls")    sheet = workbook.sheet_by_index(0)    nrow = sheet.nrows # obtain excel Maximum number of rows     word_path = " Information card .docx"    contexts = []    #  Open the specified Word Templates     for i in range(2,nrow):        word = DocxTemplate(" Information card .docx")        picture = InlineImage(word,'E:/venv/Lib/a.jpg',width=Cm(3))        context = {
   
   
   
   ' Serial number ': sheet.cell_value(i, 0),                   ' Disease body number ': sheet.cell_value(i, 1),                   ' The name of the road ': sheet.cell_value(i, 2),                   ' Detailed description of the location ': sheet.cell_value(i, 3),                   ' Central coordinates _X': sheet.cell_value(i, 4),                   ' Central coordinates _Y': sheet.cell_value(i, 5),                   ' Type of hidden danger ': sheet.cell_value(i, 6),                   ' Hidden danger length ': sheet.cell_value(i, 7),                   ' Hidden danger width ': sheet.cell_value(i, 8),                   ' Hidden danger depth ': sheet.cell_value(i, 10),                   ' Hazard level ': sheet.cell_value(i, 12),                   ' Cause analysis ': sheet.cell_value(i, 13),                   ' The disposal of advice ': sheet.cell_value(i, 14),                   ' Map location ': picture        }        jinja_env = jinja2.Environment(autoescape=True)        word.render(context,jinja_env)       # word.save('ceshi'+str(sheet.cell(i, 0).value)+'.docx'        contexts.append(context)    with MailMerge(word_path) as doc:        doc.merge_templates(contexts, separator='page_break')        output = r'E:\venv\Lib\ test .docx'        doc.write(output)if __name__ == '__main__':    Covert_Excel_To_Word()###### Traceback (most recent call last):  File "E:\venv\Lib\word Illustration test 1.py", line 44, in <module>    Covert_Excel_To_Word()  File "E:\venv\Lib\word Illustration test 1.py", line 39, in Covert_Excel_To_Word    doc.merge_templates(contexts, separator='page_break')  File "D:\python\lib\site-packages\mailmerge.py", line 234, in merge_templates    self.merge(parts, **repl)  File "D:\python\lib\site-packages\mailmerge.py", line 254, in merge    self.__merge_field(part, field, replacement)  File "D:\python\lib\site-packages\mailmerge.py", line 266, in __merge_field    text_parts = text.replace('\r', '').split('\n')AttributeError: 'float' object has no attribute 'replace'
Traceback (most recent call last):  File "E:\venv\Lib\word Illustration test 1.py", line 44, in <module>    Covert_Excel_To_Word()  File "E:\venv\Lib\word Illustration test 1.py", line 39, in Covert_Excel_To_Word    doc.merge_templates(contexts, separator='page_break')  File "D:\python\lib\site-packages\mailmerge.py", line 234, in merge_templates    self.merge(parts, **repl)  File "D:\python\lib\site-packages\mailmerge.py", line 254, in merge    self.__merge_field(part, field, replacement)  File "D:\python\lib\site-packages\mailmerge.py", line 266, in __merge_field    text_parts = text.replace('\r', '').split('\n')AttributeError: 'float' object has no attribute 'replace'
It can be extracted directly and circularly , Save in a document



Refer to the answer 1:

Look at the error tips , It should be character type , But the argument is floating-point

img

Check it like this , See if there is such a situation in the generated data , If there is , At the corresponding position of the generated data , Add one str , Convert to character type .




Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022011511959.html

Random recommended