0
Follow
0
View

The json string is written to the word document

wangjz38257372 注册会员
2023-02-28 17:23

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.

About the Author

Question Info

Publish Time
2023-02-28 17:23
Update Time
2023-02-28 17:23

Related Question

Is it possible to traverse through broadcast object when building glass literals with generics?

SES.在Lambda中没有调用Promise

e.dispatchEvent不是Elementor Wordpress front .min.js的函数

mybatis一直报错

AceEditor:Uncaught Error:无法加载模块ace/theme/

vue的路由去掉#,哈希变成history

multisim怎么能出这个频谱图

在Azure数据工厂中平滑Json对象

为什么我的isEmpty()方法读取为空?

在Rust中遍历json时出现错误