How write to ms word by sample? - python

I have ms word sample.docx like this:
{{id1}} some text {{name1}} text again {{password1}}
{{id2}} some text {{name2}} text again {{password2}}
{{id3}} some text {{name3}} text again {{password3}}
dict from code
list_data = [
{"id": "1", "name": "cat", "password": "123"},
{"id": "2", "name": "john", "password": "321"},
{"id": "3", "name": "mike", "password": "555"},
{"id": "1", "name": "who is this", "password": "342"},
{"id": "2", "name": "some", "password": "67332"},
{"id": "3", "name": "horse", "password": "qwerty"},
{"id": "1", "name": "sone n", "password": "some pass n"},
{"id": "2", "name": "some n", "password": "some pass n"},
{"id": "3", "name": "some n", "password": "some pass n"},
]
code
from docxtpl import DocxTemplate
context = {}
doc = DocxTemplate("sample.docx")
for i in range(len(list_data)):
for data in list_data:
if i % 3 == 0:
context['id' + data['id']] = data['id']
context['name' + data['id']] = data['name']
context['password' + data['id']] = data['password']
doc.render(context)
doc.save(f"{i}_output.docx")
this code get next result:
0_output.docx:
1 some text who is this text again 342
2 some text some text again 67332
3 some text horse text again qwerty
and
3_output.docx have result 0_output.docx
--------------------------------------------------------------------------------------------------
How get result
0_output.docx:
1 some cat who is this text again 123
2 some john some text again 321
3 some mike some text again 555
3_output.docx:
1 some text who is this text again 342
2 some text some text again 67332
3 some text horse text again qwerty
etc ....

Try this (Python 3.x):
from docxtpl import DocxTemplate
list_data = [
{"id": "1", "name": "cat", "password": "123"},
{"id": "2", "name": "john", "password": "321"},
{"id": "3", "name": "mike", "password": "555"},
{"id": "1", "name": "who is this", "password": "342"},
{"id": "2", "name": "some", "password": "67332"},
{"id": "3", "name": "horse", "password": "qwerty"},
{"id": "1", "name": "sone n", "password": "some pass n"},
{"id": "2", "name": "some n", "password": "some pass n"},
{"id": "3", "name": "some n", "password": "some pass n"},
]
cols = ['id','name','password']
for i in range(len(list_data)):
if i % 3 ==0:
doc = DocxTemplate("sample.docx")
context = {}
for col in cols:
context[f'{col}1'] = list_data[i][col]
context[f'{col}2'] = list_data[i+1][col]
context[f'{col}3'] = list_data[i+2][col]
doc.render(context)
doc.save(f"{i}_output.docx")

Related

Parsing JSON in Python and Converting to Excel

I am attempting to parse this JSON and convert it to Excel and only print out certain columns using Python but I keep getting an TypeError: string indices must be integers. The JSON file continues to repeat what is inside "d" over and over again.
JSON:
{
"d": [
{
"__type": "New Cust",
"Description": "TV Purchase",
"End": "/Date(1624962600000)/",
"ID": 1,
"Resources": [
{
"Available": true,
"Key": "12345",
"Text": "John Doe",
"Type": "SalesProvider",
"CalendarID": "1234"
}
],
"Subject": "Meeting with Sam",
"Visible": true,
"AppStatus": "5",
"StartTime": "06/01/2021 10:30:00",
"AppID": "1",
"ServiceID": "7",
"NotesCount": "0",
"CustomerID": "1",
"AppType": "NNR",
"IsEditedThis": "False",
"BusinessPackageID": "0",
"PopupNotesCount": "0",
"EventType": "1",
"SPName": "John Doe",
"SPID": "12345",
"EventCapacity": "0",
"IsPaid": "False",
"IsWorkShop": "False",
"FormStatus": "0",
"PaidType": "1",
"AppComment": "",
"CustName": "Sam Will",
"ResourceID": "",
"CssClass": "rsCategoryBlue",
"ServiceName": "TV Sale",
"NoOfAttendees": null,
"StreamingNoOfAttendees": null,
"StreamingStatus": "0",
"StreamingEventCapacity": "",
"Photo": "",
"PersonalOffType": null,
"ResourceName": null,
"IsShowCheckIn": false,
"PaymentStatus": 0
},
{
"__type": "New Cust",
"Description": "Receiver Purchase",
"End": "/Date(1624962600000)/",
"ID": 1,
"Resources": [
{
"Available": true,
"Key": "12345",
"Text": "John Doe",
"Type": "SalesProvider",
"CalendarID": "1234"
}
],
"Subject": "Meeting with Bill",
"Visible": true,
"AppStatus": "5",
"StartTime": "07/02/2021 9:30:00",
"AppID": "1",
"ServiceID": "7",
"NotesCount": "0",
"CustomerID": "1",
"AppType": "NNR",
"IsEditedThis": "False",
"BusinessPackageID": "0",
"PopupNotesCount": "0",
"EventType": "1",
"SPName": "John Doe",
"SPID": "12345",
"EventCapacity": "0",
"IsPaid": "False",
"IsWorkShop": "False",
"FormStatus": "0",
"PaidType": "1",
"AppComment": "",
"CustName": "Bill Tom",
"ResourceID": "",
"CssClass": "rsCategoryBlue",
"ServiceName": "Audio Sale",
"NoOfAttendees": null,
"StreamingNoOfAttendees": null,
"StreamingStatus": "0",
"StreamingEventCapacity": "",
"Photo": "",
"PersonalOffType": null,
"ResourceName": null,
"IsShowCheckIn": false,
"PaymentStatus": 0
}
]
}
Python Code:
import json
import pandas as pd
f = open('JSON.txt', 'r')
data = json.loads(f.read())
l = []
for profile in data['d']:
l.append(profile["Subject"]["StartTime"]["IsPaid"]["CustName"]["ServiceName"])
df1 = pd.DataFrame(l)
print(df1)
df1.to_excel('df1.xlsx')
I do not need the "Resources": [] info I just need certain parameters outside it in the JSON object. I am having difficulty parsing the JSON any help would be appreciated.
you can use a combination of the json standard library and pd.json_normalize
import json
import pandas as pd
parsed_json = json.loads('JSON.txt')
df = pd.json_normalize(parsed_json['d'],record_path='Resources')
print(df)
Available Key Text Type CalendarID
0 True 12345 John Doe SalesProvider 1234
1 True 12345 John Doe SalesProvider 1234
then pass it to excel
df.to_excel(...,index=False)
Going back to your issues, it seems like your trying to grab a bunch of fields like a list, when in reality you're attempting to find an attribute from a single data type object.
print(data['d'][0]['Subject'])
'Meeting with Sam'
This has no other nested items so you'll naturally get an error.
the error TypeError: string indices must be integers is telling you you can only slice this object with integers i.e
#reversing the string.
print(data['d'][0]['Subject'][::-1])
maS htiw gniteeM
or
#first two characters of the string.
print(data['d'][0]['Subject'][:2])
Me
if you want to grab only a subset of columns from the top level you could do :
cols = ['Subject', 'StartTime', 'IsPaid', 'CustName', 'ServiceName']
df = pd.json_normalize(parsed_json['d'],)[cols]
print(df)
Subject StartTime IsPaid CustName ServiceName
0 Meeting with Sam 06/01/2021 10:30:00 False Sam Will TV Sale
1 Meeting with Bill 07/02/2021 9:30:00 False Bill Tom Audio Sale

Create JSON having a specific structure in Python by merging dictionaries in a blank master dictionary

So, I have JSON data that I am getting from an HTML form. I have to format this data to have a specific JSON structure as shown below.
[
{
"Header": "Stats",
"Line_01": "Line 01",
"Line_02": "Line 02",
"Line_03": "Line 03",
"Line_04": "Line 04",
"Line_05": "Line 05",
"Line_06": "Line 06",
"Line_07": "Line 07"
},
{
"Header": "JUV",
"Line_01": "89",
"Line_02": "34",
"Line_03": "765",
"Line_04": "123",
"Line_05": "1",
"Line_06": "4",
"Line_07": "455"
},
{
"Header": "SMP",
"Line_01": "12",
"Line_02": "89",
"Line_03": "124",
"Line_04": "678",
"Line_05": "92",
"Line_06": "120",
"Line_07": "5"
}
]
JSON I have from the HTML form is:
[
{
"name": "00",
"value": "JUV"
},
{
"name": "00",
"value": "STATS"
},
{
"name": "00",
"value": "SMP"
},
{
"name": "00",
"value": "89"
},
{
"name": "01",
"value": "LINE 01"
},
{
"name": "02",
"value": "12"
},
{
"name": "03",
"value": "34"
},
{
"name": "04",
"value": "LINE 02"
},
{
"name": "05",
"value": "89"
},
{
"name": "06",
"value": "765"
},
{
"name": "07",
"value": "LINE 03"
},
{
"name": "08",
"value": "124"
},
{
"name": "09",
"value": "123"
},
{
"name": "10",
"value": "LINE 04"
},
{
"name": "11",
"value": "678"
},
{
"name": "12",
"value": "1"
},
{
"name": "13",
"value": "LINE 05"
},
{
"name": "14",
"value": "92"
},
{
"name": "15",
"value": "4"
},
{
"name": "16",
"value": "LINE 06"
},
{
"name": "17",
"value": "120"
},
{
"name": "18",
"value": "455"
},
{
"name": "19",
"value": "LINE 07"
},
{
"name": "20",
"value": "5"
}
]
The form looks like this: HTML form - Image on Pasteboard
The Python code I am trying so far is:
import json
jarr = []
final_file = {}
json_template = {
"Header": "",
"Line_01": "",
"Line_02": "",
"Line_03": "",
"Line_04": "",
"Line_05": "",
"Line_06": "",
"Line_07": ""
}
with open("testo101.json",) as f:
jdata = json.load(f)
k = 0
for i in range(8):
a =[]
for j in range(3):
a.append(jdata[k]['value'])
k+=1
jarr.append(a)
for i, x in enumerate(json_template):
json_template[x]=jarr[i][1]
final_file.update(json_template)
for i, x in enumerate(json_template):
json_template[x]=jarr[i][0]
final_file.update(json_template)
for i, x in enumerate(json_template):
json_template[x]=jarr[i][2]
final_file.update(json_template)
print(final_file)
So what I have done so far is:
Import the JSON file I got from HTML form.
Covert it into a 3x8 matrix... so I can get values of each column
easily.
I can fill values from the JSON in the json_template dictionary with
the help of 2D array I just created.
The Problem:
I can't figure out how to merge the 3 dictionaries that I am generating from each column of the 2D array into a single dictionary final_file so I can dump it as a JSON file and that's what I want. How can I do this... or if there is some other better way to do this then please help.
Thanks.
These code should do the job:
import json
jarr = []
final_file = [] # CHANGE #1
json_template = {
"Header": "",
"Line_01": "",
"Line_02": "",
"Line_03": "",
"Line_04": "",
"Line_05": "",
"Line_06": "",
"Line_07": ""
}
with open("testo101.json",) as f:
jdata = json.load(f)
k = 0
for i in range(8):
a =[]
for j in range(3):
a.append(jdata[k]['value'])
k+=1
jarr.append(a)
for i, x in enumerate(json_template):
json_template[x]=jarr[i][1]
final_file.append(json_template.copy()) # CHANGE #2
for i, x in enumerate(json_template):
json_template[x]=jarr[i][0]
final_file.append(json_template.copy()) # CHANGE #2
for i, x in enumerate(json_template):
json_template[x]=jarr[i][2]
final_file.append(json_template.copy()) # CHANGE #2
with open('yourjson.json', 'w') as jsonfile:
json.dump(final_file, jsonfile, indent=4)
I made two changes to your code:
You need a list of dictionaries, not just one dictionary to dump three dictionaries, so I changed final_file to a list.
After you make each json_template, I attached a copy of the template to the list. (.copy() is important, otherwise later changes will be reflected in the previous entries and you end up getting three of the same item).
I wrote the dumping code and attached it to the end. You can open yourjson.json to see the result.

Getting the value in a field in the json object that comes in tabular form with django

I get the data from the postgresql table as json as follows. But for example in the data coming in json; I want to display the data in the "symbol" field in the "for" loop. How can I do that ?
So more than one data is coming and I want to print it on the screen one by one.
[
{"model": "cryptoinfo.cryptoinfo",
"pk": 4,
"fields":
{"createdDate": "2020-10-08T20:49:16.622Z", "user": 2, "created_userKey": "25301ba6-1ba9-4b46-801e-32fc51cb0bdc", "customerKey": "61754ecf-39d3-47e0-a089-7109a07aca63", "status": true, "side": "BUY", "type": "1", "symbol": "NEOUSDT", "quantity": "1", "reversePosition": "1", "stopMarketActive": "1", "shortStopPercentage": "1", "longStopPercentage": "1", "takeProfit": "1", "addPosition": "1", "takeProfitPercentage": "1", "longTakeProfitPercentage": "1", "shortTakeProfitPercentage": "1", "groupCode": "1453", "apiKey": "2200", "secretKey": "0022"}},
{"model": "cryptoinfo.cryptoinfo",
"pk": 7,
"fields": {"createdDate": "2020-10-08T20:51:16.860Z", "user": 1, "created_userKey": "2f35f875-7ef6-4f17-b41e-9c192ff8d5df", "customerKey": "b1c8cee3-c703-4d27-ae74-ad61854f3539", "status": true, "side": "BUY", "type": "1", "symbol": "NEOUSDT", "quantity": "1", "reversePosition": "1", "stopMarketActive": "1", "shortStopPercentage": "1", "longStopPercentage": "1", "takeProfit": "1", "addPosition": "1", "takeProfitPercentage": "1", "longTakeProfitPercentage": "1", "shortTakeProfitPercentage": "1", "groupCode": "1453", "apiKey": "0011", "secretKey": "1100"}}
]
def webhook(request):
json_body = json.loads(request.body)
queryset = CryptoInfo.objects.filter(status=True, symbol=json_body['symbol'], side=json_body['side'])
data = serializers.serialize('json', queryset)
print(data['symbol'])
return HttpResponse(data, content_type='application/json')
import json
def webhook(request):
...
data = serializers.serialize('json', queryset)
for crypto_info in json.loads(data):
print(crypto_info['fields']['symbol'])

Filter json response from http request to only specific value then select the largest 'id' value from all results

I have a Python script where I would like to filter Python with a http get and I would like to filter the response data for only specific values. The json response example is below:
{
"id": "38",
"name": "Report1",
"description": "",
"reportDefinitionID": "-1",
"jobID": "105600",
"type": "csv",
"status": "Completed",
"creator": {
"id": "1",
"username": "btest",
"firstname": "bob",
"lastname": "test"
},
{
"id": "39",
"name": "Report2",
"description": "",
"reportDefinitionID": "-1",
"jobID": "113218",
"type": "csv",
"status": "Completed"
"creator": {
"id": "1",
"username": "btest1",
"firstname": "Bob",
"lastname": "test1"
},
"id": "49",
"name": "Report1",
"description": "",
"reportDefinitionID": "-1",
"jobID": "113219",
"type": "csv",
"status": "Completed"
"creator": {
"id": "1",
"username": "btest1",
"firstname": "Bob",
"lastname": "test1"
}
I would like to filter the above json to only show a report by name. For example if there is a Python filter that would only allow me to filter for a report by the name of "Report1". If I filtered on name of "Report1". I would expect to following to be to be returned below:
{
"id": "38",
"name": "Report1",
"description": "",
"reportDefinitionID": "-1",
"jobID": "105600",
"type": "csv",
"status": "Completed",
"creator": {
"id": "1",
"username": "btest",
"firstname": "bob",
"lastname": "test"
},
"id": "49",
"name": "Report1",
"description": "",
"reportDefinitionID": "-1",
"jobID": "113219",
"type": "csv",
"status": "Completed"
"creator": {
"id": "1",
"username": "btest1",
"firstname": "Bob",
"lastname": "test1"
}
For the final part of the script I would like to compare the 'id' field to show the largest value for example id 38 vs id 49 and then output the json for the largest in this case id 49. I would like it output
},
"id": "49",
"name": "Report1",
"description": "",
"reportDefinitionID": "-1",
"jobID": "113219",
"type": "csv",
"status": "Completed"
"creator": {
"id": "1",
"username": "btest1",
"firstname": "Bob",
"lastname": "test1"
}
For the last part i would just like to save the id value '49' to a variable in Python.
So far what I have below is:
response_data = response.json()
input_dict = json.dumps(response_data)
input_transform = json.loads(input_dict)
# Filter python objects with list comprehensions
sort1 = sorted([r.get("id") for r in input_transform if r.get("name") == "Report1"], reverse=True)[0]
# Print sorted JSON
print(sort1)
I updated my code and now I'm getting the error below:
'str' object has no attribute 'get'
I researched it and can not figure out what I'm doing now and how to get past it.
You need to get the ID in the listcomp as bellow:
sorted([r.get("id") for r in sample if r.get("name") == "Report1"], reverse=True)[0]

Need help in Extracting information from an JSON which is storing an Abstract Syntax Tree

{
"id": "9",
"children": [{
"id": "8",
"children": [{
"id": "7",
"children": [{
"id": "6",
"children": [
{
"id": "0",
"type": "isPathForward"
},
{
"id": "2",
"children": [{
"id": "1",
"type": "maze_moveForward"
}],
"type": "DO"
},
{
"id": "5",
"children": [{
"id": "4",
"children": [{
"id": "3",
"type": "turnLeft"
}],
"type": "maze_turn"
}],
"type": "ELSE"
}
],
"type": "maze_ifElse"
}],
"type": "DO"
}],
"type": "maze_forever"
}],
"type": "program"
}
The above valid JSON is basically an AST (Abstract Syntax Tree) and I want to extract only the "type" in the order : 1) node followed by 2) Left child and then 3) Right child
Exactly like below for the above JSON :
Program
maze_forever
DO
maze_ifElse
isPathforward
Do
maze_moveForward
Else
maze_turn
turn_Left
I have not worked with json I tried using generators in python but the order is lost in the process as it converts to a dict.
Could you write a python implementation for this ?
UPDATE !!!!
So far i have tried :
import json
json_string= json.loads(above json)
when i type :
for i in json_string:
... print(i)
...
OUTPUT
type
id
children
I also tried
import pandas as pd
d=pd.read_json('{ "id": "9", "children": [{ "id": "8", "children": [{ "id": "7", "children": [{ "id": "6", "children": [ { "id": "0", "type": "isPathForward" }, { "id": "2", "children": [{ "id": "1", "type": "maze_moveForward" }], "type": "DO" }, { "id": "5", "children": [{ "id": "4", "children": [{ "id": "3", "type": "turnLeft" }], "type": "maze_turn" }], "type": "ELSE" } ], "type": "maze_ifElse" }], "type": "DO" }], "type": "maze_forever" }], "type": "program"}')
>>> d
output :
children id type
0 {'type': 'maze_forever', 'id': '8', 'children'... 9 program
Both cases above :
I do not know how to recursively get inside children as every child has one or more children inside. Most answers I searched donot explain for a JSON that is as nested as my JSON above.
The most obvious implementation is as a recursive function:
>>> def process(data):
... if 'type' in data: print data['type']
... if 'children' in data:
... for child in data['children']:
... process(child)
...
>>> j = json.load(open('test.json', 'r'))
>>> process(j)
program
maze_forever
DO
maze_ifElse
isPathForward
DO
maze_moveForward
ELSE
maze_turn
turnLeft
Note that we're printing the current structure's type before we recurse into children.

Categories

Resources