python读取word文档中的表格,并生成网页
安装python-docx包
pip install python-docx
直接上代码
// An highlighted block
import docx
# 打开Word文档
doc = docx.Document('20231229.docx')
filename = "index.html"
# 遍历文档中的表格
print('begin')
file = open(filename, "w" , encoding='utf-8')
file.write("<table width='780' border='0' bgcolor='#cccccc' cellspacing='1' cellpadding='5' style='margin:10px 0 10px 10px;'> \n")
file.write("<tr bgcolor='#FFFFFF'> \n")
file.write("<td><b>No.</b></td> \n")
file.write("<td width='50'><b>Date</b></td> \n")
file.write("<td width='140'><b>Name</b></td> \n")
file.write("<td><b>Seminar Title</b></td> \n")
file.write("<td><b>Affiliation</b></td> \n")
file.write("</tr> \n")
for table in doc.tables:
# 遍历表格中的行
for row in table.rows:
# 遍历行中的单元格
# for cell in row.cells:
# # 获取单元格中的文本内容
# text = cell.text
# # 打印文本内容
# print(text)
# print(type(cell))
file.write("<tr bgcolor='#FFFFFF'> \n")
file.write("<td>" + row.cells[0].text +"</td>\n")
file.write("<td>" + row.cells[1].text +"</td>\n")
file.write("<td>" + row.cells[2].text +"</td>\n")
file.write("<td><a href='"+ row.cells[4].text +"'>" + row.cells[3].text +"</a></td>\n")
file.write("<td>" + row.cells[5].text +"</td>\n")
file.write("</tr> \n")
file.write("</table> \n")
file.close()