I am looking to write a script that will export orders from a specific date and export them into a word document. I am using Python 2.7 to query the Woocommerce API.
I am only interested in one attribute from the order which is: the line_items meta data - message.
Here is my current Python code to extract the message from one specific order:
from woocommerce import API
import json
from docx import Document
wcapi = API(
url="xxxxxxxx",
consumer_key="xxxxxx",
consumer_secret="xxxxxxx",
wp_api=True,
version="wc/v2",
query_string_auth=True
)
json_data = (wcapi.get("orders/383").json())
json_parsed = json.dumps(json_data)
new = json_parsed.decode("utf-8")
new1 = json.loads(new)
new2 = (new1['line_items'][0]['meta_data'])
print new2
And this is the output:
[{u'key': u'Message', u'id': 475, u'value': u'Dear [TES TSTE STST EST\r\n\r\nIt has been very bu e. The elves have been working around the clock to make sure the toys are ready to deliver to al s. Rudolph and his fellow reindeer are getting ready for the long journey ahead, and I\u2019m si riting this letter and enjoying a lovely mince pie.\r\n\r\nI am writing to let you know that you aved this year and have made it onto my good child list. \r\n\r\nI am very proud of you, and as u2019d like to get you something extra-special for Christmas this year. \r\n\r\nI\u2019ll be vis n, keep up the good work. this is ss a gadfh\r\n\r\nMerry Christmas!\r\nFrom Santa Claus'}]
As you can see the output isn't correct.
How do I only pull in the contents of the message only - everything from 'Dear' onwards.
How do I format the output correctly? There looks to be lots of odd codes where there is meant to be a new paragraph or punctuation.
How would I export the message from Python and save in a word doc?
Thanks in advance Nick