SQL Server Reading/Writing /Executing .xlsm files in Python

unftdfkk  于 2023-05-16  发布在  Python
关注(0)|答案(1)|浏览(140)

I have been working on a project where I am supposed to use Python for data analysis. I am stuck on one part in the project. Actually, I want to copy all the data from a table in SQL server to an Excel file with a .xlsm extension. I have written most of the code using pandas and pyodbc libraries. However, I learned that pandas doesn't support .xlsm extension so switched to openpyxl, which supposedly does. However, my code doesn't work.

Here are the relevant lines of code:

sql_table = 'table1'

sql_query = f'SELECT * FROM {sql_table};'

        # Read the processed data in SQL Server
processed_data = pd.read_sql_query(sql_query, conn_sql)

data = pd.DataFrame(processed_data)

wb = load_workbook('new.xlsm')

ws = wb['Internal']

for r in dataframe_to_rows(data, index=False, header=True):
    ws.append(r)

wb.save('new.xlsm')

I am sure about the modules I have imported, the SQL connection and other stuff. This block of code is the only problem. Do note that I want to access an already existing Excel file with a .xlsm extension and not create a new one.

Expectation:

The script runs without an error and I see the changes in the relevant Excel file.

Reality:

I receive an error which states:
ValueError: Unable to read Workbook: could not read worksheets from new.xlsm. This is most probably because the workbook source files contain some invalid XML.

hzbexzde

hzbexzde1#

As pandas states in the pandas.read_excel documentation
Supports xls, xlsx, xlsm, xlsb, odf, ods and odt file extensions read from a local filesystem or URL.

So one way to do it is, opening the xlsm file with pandas read_excel, concatenating the two frames and then overwriting the file.

I have not tried it but something similar to this should work

sql_table = 'table1'

sql_query = f'SELECT * FROM {sql_table};'

        # Read the processed data in SQL Server
processed_data = pd.read_sql_query(sql_query, conn_sql)

data = pd.DataFrame(processed_data)

ws = pd.read_excel('new.xlsm', sheet_name='Internal')

ws = pd.concat([ws, data])

ws.to_excel('new.xlsm')

相关问题