This answer quotes ChatGPT
Here is sample code for importing JSON data into a Word table using Python and the Python-docx library:
import json
from docx import Document
from docx.shared import Inches
# 打开 JSON 文件并读取数据
with open('data.json', 'r') as f:
data = json.load(f)
# 创建一个新的 Word 文档
document = Document()
# 创建一个表格,包含两行和三列
table = document.add_table(rows=2, cols=3)
# 添加表头
hdr_cells = table.rows[0].cells
hdr_cells[0].text = '名称'
hdr_cells[1].text = '数值'
hdr_cells[2].text = '类型'
# 将 JSON 数据插入到表格中
for item in data:
row_cells = table.add_row().cells
row_cells[0].text = item['name']
row_cells[1].text = str(item['value'])
row_cells[2].text = type(item['value']).__name__
# 将表格添加到 Word 文档中并保存
document.add_page_break()
document.save('output.docx')
In this example code, we first open a JSON file and read the data in it. We then create a new Word document using the Python-docx library and add a table containing table headers. Next, we traverse the JSON data and insert it into the table. Finally, we add the table to the Word document and save the document. Notice that we used the type() function to get the type of the JSON data and display it as a column in the table.