Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Automated report writer

Hartkeye

Coder
Hello, i've come up with an idea for my very first project in python, and have some questions about formatting the data in an efficient way prior to writing the code for it. I want to be able to take data from tables i've created in an Excel file, and based on those data points, create a word document that implements the data in the correct place. Think of this as an interactive ad-lib book.

Example: one data table has subject information (Name, DOB, Address, etc.) The program would pull that information, insert it into a pre-written paragraph, and enter it into the word file. Another table has vehicle information, and would take data from the table and put it into a different pre-written paragraph. This would continue until the full report is completed.

My question: In writing the pre-written paragraphs, I've started to think about how annoying it would be to pre-write several different full report templates, and instead have the program be able to determine if a paragraph is put into the report based on whether or not a table exists for that section at all.
-would it be more efficient to have the program find the correct doc based on the data available and fill it in, or would it be more feasable to have the pre-written paragraph in the program itself to avoid searching several documents, and just print based on the data available within the program itself?
 
Hello, i've come up with an idea for my very first project in python, and have some questions about formatting the data in an efficient way prior to writing the code for it. I want to be able to take data from tables i've created in an Excel file, and based on those data points, create a word document that implements the data in the correct place. Think of this as an interactive ad-lib book.

Example: one data table has subject information (Name, DOB, Address, etc.) The program would pull that information, insert it into a pre-written paragraph, and enter it into the word file. Another table has vehicle information, and would take data from the table and put it into a different pre-written paragraph. This would continue until the full report is completed.

My question: In writing the pre-written paragraphs, I've started to think about how annoying it would be to pre-write several different full report templates, and instead have the program be able to determine if a paragraph is put into the report based on whether or not a table exists for that section at all.
-would it be more efficient to have the program find the correct doc based on the data available and fill it in, or would it be more feasable to have the pre-written paragraph in the program itself to avoid searching several documents, and just print based on the data available within the program itself?
Hi there,
First off, very interesting concept! As far as feasibility goes, I think it would make sense to have the paragraph in there to begin with, just so you can see how everything will play out. Once you have the files displaying the data that you need, then go back and play around with not having the paragraph and what you want to do in that scenario. I know it has always helped me out when I have the task done as it was intended to work, and then go back and include any scenario situations.
 
Hi there,
First off, very interesting concept! As far as feasibility goes, I think it would make sense to have the paragraph in there to begin with, just so you can see how everything will play out. Once you have the files displaying the data that you need, then go back and play around with not having the paragraph and what you want to do in that scenario. I know it has always helped me out when I have the task done as it was intended to work, and then go back and include any scenario situations.
I'm definitely leaning towards putting the paragraph in the code. Gonna start off super basic, and as it progresses hopefully i'll be able to make it more and more accurate based on each data point. My current plan for whether or not a paragraph gets included is mostly going to rely on 'if' statements to determine if there is a data entry made in the table or not, then get more detailed in future iterations. Thanks for the advice!
 
I'm definitely leaning towards putting the paragraph in the code. Gonna start off super basic, and as it progresses hopefully i'll be able to make it more and more accurate based on each data point. My current plan for whether or not a paragraph gets included is mostly going to rely on 'if' statements to determine if there is a data entry made in the table or not, then get more detailed in future iterations. Thanks for the advice!
No problem! :) Don't forget to ask away should you get stuck on any part of the process
 
So far i've made progress on parts of the project, and am stuck on others. docx is being used to start a word document and put the correct headers/paragraphs into the file. Now i'm working on using openpyxl to import data from an excel file i've created to simulate my final goal of getting data from a software I use at work. Baby steps. However, I'm stuck. I'm able to get the data into the code, but am unsure how to use it.

from openpyxl import load_workbook
wb = load_workbook('reportcode.xlsx')
ws = wb["OFFICER INFO"]
rows = ws.iter_rows(max_row=5, max_col=2)

# officer_info = dict((a.value, b.value) for a, b in rows) ???

officer_info_dic = {'First_Name': 'John', 'Last_Name': 'Smith',
'IBM': 5565, 'Call_Sign': '"3E3"', 'Veh_Num': 'N201'}
initial_info = (f'"""On DATE I was operating as "Call_Sign", I was operating in Veh_Num.\
At approximately TIME I responded to INCIDENT_LOCATION reference a CALL_TYPE."""')
for key in officer_info_dic.keys():
initial_info = initial_info().replace(key, officer_info_dic[key])
print(initial_info)

I want to be able to use the key: value in the info=dic and replace the keys in the actual paragraph. I'm totally lost at this point haha
 
So far i've made progress on parts of the project, and am stuck on others. docx is being used to start a word document and put the correct headers/paragraphs into the file. Now i'm working on using openpyxl to import data from an excel file i've created to simulate my final goal of getting data from a software I use at work. Baby steps. However, I'm stuck. I'm able to get the data into the code, but am unsure how to use it.

from openpyxl import load_workbook
wb = load_workbook('reportcode.xlsx')
ws = wb["OFFICER INFO"]
rows = ws.iter_rows(max_row=5, max_col=2)

# officer_info = dict((a.value, b.value) for a, b in rows) ???

officer_info_dic = {'First_Name': 'John', 'Last_Name': 'Smith',
'IBM': 5565, 'Call_Sign': '"3E3"', 'Veh_Num': 'N201'}
initial_info = (f'"""On DATE I was operating as "Call_Sign", I was operating in Veh_Num.\
At approximately TIME I responded to INCIDENT_LOCATION reference a CALL_TYPE."""')
for key in officer_info_dic.keys():
initial_info = initial_info().replace(key, officer_info_dic[key])
print(initial_info)

I want to be able to use the key: value in the info=dic and replace the keys in the actual paragraph. I'm totally lost at this point haha
ok so let's take a step back. You now have the data, great! What do you need from that data? What is the goal of the getting the data?
 
I want to take this data, and be able to have it automatically be placed into the correct position in my pre-written paragraph. So I changed the code a little to where I can print out the data in a decent looking dictionary.

rows = ws.iter_rows(max_row=5, max_col=2)
officer_info = dict((a.value, b.value) for a, b in rows)
print(officer_info)

the out
the output comes out as
{'First_Name': 'John', 'Last_Name': 'Smith', 'IBM': 5565, 'Call_Sign': '"3E3"', 'Squad_Num': 'N201'}

Now i'm trying to figure out how to have this dictionary find the "key" in my paragraph, and replace the key with the value.

This is the next step in understanding the way I can use the dictionary in the ways I want. At least I hope!
 
I GOT IT TO WORK
Python:
rows = ws.iter_rows(max_row=5, max_col=2)

officer_info = dict((a.value, b.value) for a, b in rows)

First_Name = officer_info.get('First_Name')
Last_Name = officer_info.get('Last_Name')
IBM = officer_info.get('IBM')
Call_Sign = officer_info.get('Call_Sign')
Squad_Num = officer_info.get('Squad_Num')

initial_info = f'hello, my name is {First_Name} {Last_Name}. My IBM is {IBM}, and I drive {Squad_Num}.'

print(initial_info)

It pulls from my excel sheet, creates a dictionary, and pulls the values into the correct places in the "initial info" paragraph.
 
I GOT IT TO WORK
Python:
rows = ws.iter_rows(max_row=5, max_col=2)

officer_info = dict((a.value, b.value) for a, b in rows)

First_Name = officer_info.get('First_Name')
Last_Name = officer_info.get('Last_Name')
IBM = officer_info.get('IBM')
Call_Sign = officer_info.get('Call_Sign')
Squad_Num = officer_info.get('Squad_Num')

initial_info = f'hello, my name is {First_Name} {Last_Name}. My IBM is {IBM}, and I drive {Squad_Num}.'

print(initial_info)

It pulls from my excel sheet, creates a dictionary, and pulls the values into the correct places in the "initial info" paragraph.
NIIIICE
 
Awesome work. I'd prewrite those report templates as Word files or something with var names wherever you want them and have Python open that file, do the value replacement, and spit out the new, completed file, rather than saving the text of the reports within the Python files themselves.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom