【发布时间】:2019-12-19 21:59:38
【问题描述】:
You can find my Excel data here :, and
My template is looking like this....
我在表格中有 Word 文档,我需要使用 Excel 文件中的数据填充表格。
我试过了,但是我没有得到任何与 python 代码相关的帮助。
[import xlrd
loc=('D:/SAIKUMAR/TEMPLATE/Dynamic_Reporting_Excel_Input.xlsx')
file=xlrd.open_workbook(loc,on_demand=True )
sheet=file.sheet_by_index(0)
print("First row index: ",sheet.cell_value(0,0))#index for first columns
print("no.of rows:",sheet.nrows)# count the number of rows
print("no of columns:",sheet.ncols)
for i in range(0,sheet.ncols):# print which row data you want
print(sheet.cell_value(0,i))
print('____________________')
for i in range(sheet.nrows): # print which column data you want
print(sheet.cell_value(i,6))
print(sheet.row_values(1))
#arrayofvalues = sheet.col_values(columnindex)
arrayofvalues = sheet.col_values(6)
print(arrayofvalues)
########################################################
from docx.api import Document
# Load the first table from your document. In your example file,
# there is only one table, so I just grab the first one.
document = Document('Dynamic_Reporting_Word_Template.docx')
table = document.tables\[1\]
#print(table)
# Data will be a list of rows represented as dictionaries
# containing each row's data.
data = \[\]
keys = None
for i, row in enumerate(table.rows):
text=(cell.text for cell in row.cells)
if i==0:
keys=tuple(text)
continue
row_data=dict(zip(keys, text))
data.append(row_data)][1]
这是我的模板,我用图片给出的,实际上它是存储在 word 文档中的。
【问题讨论】:
-
如果您在 xlsx 中有它,您可以将其转换为 .csv 之后,您可以轻松找到样板脚本来使用 .csv 值填充 excel 单元格。您可能会发现这很有帮助。 pbpython.com/python-word-template.html
-
但我的模板看起来不像,它看起来像表格
-
你是说 MS Word 表格?
-
是的,您可以在上面的附加链接中找到我的表格
-
你可以找到这个帮助 -- stackoverflow.com/questions/10366596/…
标签: python-3.x vba templates ms-word