一段excel导出信息到word的代码,为什么会信息替换不全?
from docx import Documentimport xlrd
def change_text(old_text, new_text):
all_paragraphs = document.paragraphs
for paragraph in all_paragraphs:
for run in paragraph.runs:
run_text = run.text.replace(old_text, new_text)
run.text = run_text
all_tables = document.tables
for table in all_tables:
for row in table.rows:
for cell in row.cells:
cell_text = cell.text.replace(old_text, new_text)
cell.text = cell_text
xlsx = xlrd.open_workbook('表格.xlsx')
sheet = xlsx.sheet_by_index(0)
for table_row in range(1, sheet.nrows):
document = Document("模版.docx")
for table_col in range(0, sheet.ncols):
change_text(str(sheet.cell_value(0, table_col)), str(sheet.cell_value(table_row, table_col)))
document.save("%s表.docx" % str(sheet.cell_value(table_row, 0)))
print("%s表完成" % str(sheet.cell_value(table_row, 0)))
程序能顺利运行,但是信息经常导出替换不全,excel和word换一下标记有时又可以了,求帮忙改进一下
页:
[1]