I am using the xlwings
library, pandas
, and sqlalchemy
to work with and read from a SQL Server Database and write to Excel spreadsheets.
When I write to an existing spreadsheet that already has column names in the first row, it is including the column headers from the SQL query, so that I have 2 rows with column names.
Is there a way to prevent this from happening?
Here is my code:
engine = sa.create_engine(connection_url)
qry = "SELECT ID, GameTitle, GameType, GamePublishDate, GameStatus, TagType FROM game.tagNames"
with engine.connect() as con:
rs = con.execute(qry)
df = pd.read_sql_query(qry, engine)
app = xw.App(visible=False)
wb = xw.Book("tags.xlsx")
ws = wb.sheets['TagNames']
ws.range('A2').options(index=False).value = df
wb.save()
wb.close()
CodePudding user response:
You can prevent xlwings from writing the column names by adding header=False
to the options.
For example, if you change this line:
ws.range('A2').options(index=False).value = df
to
ws.range('A2').options(index=False, header=False).value = df
The header=False
part will stop it from writing column names.