I have a simple MSSQL table with data in it like this:
IF OBJECT_ID('MY_BIG_TABLE') IS NOT NULL
DROP TABLE [dbo].[MY_BIG_TABLE]
CREATE TABLE [dbo].[MY_BIG_TABLE](
[ID] [int] NOT NULL,
[PERSON] [varchar](50) NOT NULL,
[STREET] [varchar](50) NOT NULL,
[PHONE] [varchar](50) NOT NULL,
[BATCH_ID] [int] NOT NULL
) ON [PRIMARY]
GO
INSERT INTO MY_BIG_TABLE (ID, PERSON, STREET, PHONE, BATCH_ID)
VALUES
(1, 'BOB', 'MAIN STREET', '555-555-5555', 100),
(2, 'SANDY', 'ELM', '666-555-5555', 100),
(3, 'FRED', 'PINE', '777-555-5555', 200),
(8, 'BOB', 'DIRT', '888-555-5555', 200),
(52, 'GEORGE', 'RIVER ROAD', '999-555-5555', 700)
I'm exporting data out of it into Excel
files using Python
and Pandas
like this:
import pypyodbc
import pandas
def main():
server_name = "SERVER_NAME"
database = "TEST_DATABASE"
connection = pypyodbc.connect(
"Driver={SQL Server};"
"Server=" + server_name + ";"
"Database=" + database + ";"
"Trusted_Connection=yes;"
)
batch_id_list = [100,200,700]
for batch_id in batch_id_list:
print ("--------------------------------------------")
print ("".join(["reading batch_id:", str(batch_id)]))
file_name = "".join(["EXPORT_", str(batch_id), ".xlsx"])
the_sql = """
SELECT * FROM
MY_BIG_TABLE
WHERE BATCH_ID = ?"""
data = pandas.read_sql(the_sql, connection, params=[batch_id])
print ("".join(["writing batch_id:", str(batch_id)]))
data.to_excel("".join(["c:/temp/", file_name]))
if __name__ == "__main__":
main()
And I get a nice little pile of Excel files. One file per BATCH_ID
with all of those results loaded in. Works great. What I need to do is have one of the columns be an Excel drop down like this:
I could certainly go into Excel and make it for each file, but as I'm sure you can tell this is just sample data. I'm going to be making thousands of Excel files.
How can I using what I have in Python make one of the columns a drop down like what I have in the picture? Is there some kind of template option I can take advantage of, I'm open to anything. I have control over the SQL data so I can add values if that helps make it easier. Thanks in advance!