Home > Back-end >  Change excel page headers with python
Change excel page headers with python

Time:02-01

Forgive me if this is an idiotic question. Im new to coding and wanted to automate part of my workflow.

Im enjoying the puzzle so i won't ask too many questions. But im stuck on this

Every time an order comes in, I have to copy data from raw excel files to a templates.

I want to replace the three headers at the top of this page with variables ive already extracted from the raw excel data.

enter image description here

so that it would look like this on every page

enter image description here

In every tutorial I see, their "header" is just row 1.

I think xlsxwriter has the ability to change those headers looks like that only on new worksheets.

`df1.to_clipboard(index=False, header=False) #Copies df1 to clipboard (BOM Data) ws.Range("A2").Select() ws.PasteSpecial(Format='Unicode Text') # Paste as text in template

#So at this point i guess im using pywin32 to copy and paste but have to use switch back to xlsxwriter to change the header?

wb = xlsxwriter.Workbook(r'C:\Users\jfras\Desktop\Auto BOM\PARKER BOM TEMPLATE.xlsx') ws = wb.Worksheets(1)

header1 = '&CTest Entry'#So at this point i guess im using pywin32 to copy and paste but have to use switch back to xlsxwriter to change the header?

wb = xlsxwriter.Workbook(r'C:\Users\jfras\Desktop\Auto BOM\PARKER BOM TEMPLATE.xlsx') ws = wb.Worksheets(1)

header1 = '&CTest Entry'`

CodePudding user response:

Your question is a little unclear, the screenshots you attached look to be inside of word. It seems like you are trying to automate moving data from excel into a word document template, is that correct?

If I understand correctly, you will need to use a python package to read your excel document, then use a python package to insert that data into a parameterized template in word. Here is an article explaining doing exactly that.

In a nutshell, using Openpyxl (or presumably any python excel reader of your choosing) you would read the excel sheet, then "plug-in" your data into a word template using something like Python-docx. The article linked above contains code snippets explaining this process in more detail.

CodePudding user response:

I hope I understood your question right. If so, something like this code below may work:

import xlsxwriter

workbook = xlsxwriter.Workbook('teste.xlsx')
worksheet = workbook.add_worksheet()

worksheet.set_header('&L P10853'    '&CTEST OBJECT'    '&RUN_28583')

workbook.close()

Of course, if you just run this code you gonna end up having an empty sheet that prints nothing until you fill at least one cell. But, anyway, you can understand the code like, the command set_header it's the mandatory here and it's doing what we want. When you put a string with &L you setting the left header &C for the center header and &R for the right header. You can see more in https://xlsxwriter.readthedocs.io/example_headers_footers.html

  • Related