我有一个JSON需要转换成Excel。 我正在使用Python3.8和xlsxwriter库。 下面是示例JSON
{
"companyId": "123456",
"companyName": "Test",
"companyStatus": "ACTIVE",
"document": {
"employee": {
"employeeId": "EM1567",
"employeeLastName": "Test Last",
"employeeFirstName": "Test Fist"
},
"expenseEntry": [
{
"allocation": [
{
"allocationId": "03B249B3598",
"journal": [
{
"journalAccountCode": "888",
"journalPayee": "EMPL",
"journalPayer": "COMP",
"taxGuid": [
"51645A638114E"
]
},
{
"journalAccountCode": "999",
"journalPayee": "EMPL",
"journalPayer": "EMPL",
"taxGuid": [
"8114E51645A63"
]
},
],
"tax": [
{
"taxCode": "TAX123",
"taxSource": "SYST"
},
{
"taxCode": "TAX456",
"taxSource": "SYST"
}
]
}
],
"approvedAmount": 200.0,
"entryDate": "2020-12-10",
"entryId": "ENTRY9988"
}
],
"report": {
"currencyCode": "USD",
"reportCreationDate": "2020-12-10",
"reportId": "ACA849BBB",
"reportName": "Test Report",
"totalApprovedAmount": 200.0
}
},
"id": "c71b7d756f549"
}
我现在的代码是: https://repl.it/@tonyiscoming/jsontoexcel
我试过用熊猫
import pandas as pd
df = pd.json_normalize(data, max_level=5)
df.to_excel('test.xlsx', index=False)
我尝试使用json\u excel\u转换器
from json_excel_converter import Converter
from json_excel_converter.xlsx import Writer
conv = Converter()
conv.convert(data, Writer(file='test.xlsx'))
有人能帮我处理这个案子吗?非常感谢你
这是你要找的代码。我是用XlsxWriter包完成的。首先,我用一些单元格格式的东西制作了模板。之后,我根据您的JSON输入值
有一件事,不用在脚本中创建模板,您可以创建自己的模板并将其保存到其他地方。然后获取该模板的副本,并使用脚本添加数据。这将使您有机会创建自己的基础模板,否则,您必须使用脚本格式化excel,例如边框格式化、合并单元格等
我使用了^{} python内置函数从itertools到
journal
和tax
对象。只需按照Python – Itertools.zip_longest()或Python's zip_longest Function文章进行示例。如果您不理解我的代码中的任何内容,请在下面进行评论Excel网格中有空单元格并不是真正的“propper”,这就是为什么json_Excel_转换器会有这样的功能。 所以,如果你想实现这一点,恐怕你必须自己开发
相关问题 更多 >
编程相关推荐