用python自动从EXCEL中提取数据填入word,并自动生成文件,但是填写的第一个数据无效是为什么?
from docx import Document
from openpyxl import load_workbook
import os
path=r'D:\函证编程\test'
if not os.path.exists(path + '/' + '全部银行询证函'):
os.mkdir(path + '/' + '全部银行询证函')
workbook=load_workbook(path + '/' + '银行函证内容.xlsx')
sheet = workbook.active
for table_row in range(2, sheet.max_row + 1):
wordfile = Document(path + '/' + '银行询证函.docx')
for table_col in range(1, sheet.max_column + 1):
old_text = str(sheet.cell(row=1, column=table_col).value)
new_text = str(sheet.cell(row=table_row, column=table_col).value)
if ' ' in new_text:
new_text = new_text.split()[0]
# 文档Document - 段落Paragraph - 文字块Run
all_paragraphs = wordfile.paragraphs
for paragraph in all_paragraphs:
for run in paragraph.runs:
run.text = run.text.replace(old_text, new_text)
# 文档Document - 表格Table - 行Row/列Column - 单元格Cell
all_tables = wordfile.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)
# 获取公司名用以生成函证的名称
company = str(sheet.cell(row=table_row, column=1).value)
wordfile.save(path + '/' + f'全部银行询证函/{company}银行询证函.docx')
我的excel第一列数据无法替换word文档相应的地方,但是其他地方又可以,这是为什么??
for table_col in range(1, sheet.max_column + 1):
你这是从第二列开始的啊