Exporting plain text header and image to Excel - python

I am fairly new to Python, but I'm getting stuck trying to pass an image file into a header during the DataFrame.to_excel() portion of my file.
Basically what I want is a picture in the first cell of the Excel table, followed by a couple of rows (5 to be exact) of text which will include a date (probably from datetime.date.today().ctime() if possible).
I already have the code to output the table portion as:
mydataframe.to_excel(my_path_name, sheet_name= my_sheet_name, index=False, startrow=7,startcol=0)
Is there a way to output the image and text portion directly from Python?
UPDATE:
For clarity, mydataframe is exporting the meat and potatoes of the worksheet (data rows and columns). I already have it starting on row 7 of the worksheet in Excel. The header portion is the trouble spot.

I found the solution and thanks for all of the help.
The simple answer is to use the xlsxwriter package as the engine. In other words assume that the image is saved at the path /image.png. Then the code to insert the data into the excel file with the image located at the top of the data would be:
# Importing packages and storing string for image file
import pandas as pd
import xlsxwriter
import numpy as np
image_file = '/image.png'
# Creating a fictitious data set since the actual data doesn't matter
dataframe = pd.DataFrame(np.random.rand(5,2),columns=['a','b'])
# Opening the xlsxwriter object to a path on the C:/ drive
writer = pd.ExcelWriter('C:/file.xlsx',engine='xlsxwriter')
dataframe.to_excel(writer,sheet_name = 'Arbitrary', startrow=3)
# Accessing the workbook / worksheet
workbook = writer.book
worksheet = writer.sheets['Arbitrary']
# Inserting the image into the workbook in cell A1
worksheet.insert_image('A1',image_file)
# Closing the workbook and saving the file to the specified path and filename
writer.save()
And now I have an image on the top of my excel file. Huzzah!

Related

How to insert images to specific excel file cells using python?

I need to create an excel file and add images to certain cells so in each row I add two images in A1 and B1. I need to do this for around 150 images by copying and pasting them?
Can someone tell me how to do that using python script automatically by just defining the images path and the excel file path and the number of the images in the folder?
Using the xlsxwriter library
import xlsxwriter
# creates an excel sheet
workbook = xlsxwriter.Workbook('images.xlsx')
worksheet = workbook.add_worksheet()
worksheet.insert_image('B2', 'your-image-name.png')
workbook.close()
See the xlsxwriter docs for more details

Is it possible to append data to an xls file in Python?

I am trying to add a large dataset to an existing xls spreadsheet.
I'm currently writing to it using a pandas dataframe and the .to_excel() function, however this erases the existing data in the (multi-sheet) workbook. The existing spreadsheet is very large and complex,it also interacts with several other files, so I can't convert it to xlsx or read and rewrite all of the data, as I've seen some suggestions on other questions. I want the data that I am adding to be pasted starting from a set row in an existing sheet.
Yes , you can use the library xlsxwriter , link= https://xlsxwriter.readthedocs.io
code example :
import xlsxwriter
Name="MyFile"+".xlsx"
workbook = xlsxwriter.Workbook(Name)
worksheet = workbook.add_worksheet()
worksheet.write("A1", "Incident category".decode("utf-8"))
worksheet.write("B1", "Longitude".decode("utf-8"))
worksheet.write("C1", "Latitude".decode("utf-8"))
workbook.close()

Python - save new Excel sheet with existing format

I want to save excel sheet with existing sheet formats and background color.
I could save successfully to a new file with out old file format.
How can I keep the existing excel sheet format when save in to a new file.
writer = pd.ExcelWriter('New.xlsx', engine='xlsxwriter')
dfDiff.to_excel(writer, sheet_name='DIFF', index=False)
writer.save()
I had no good experience with pandas and format styles... what works if you use win32com like this example:
from win32com.client import DispatchEx
excel = DispatchEx('Excel.Application')
wbP=excel.Workbooks.Open(r'C:\Temp\Junk\Temp.xlsx')
wbG=excel.Workbooks.Open(r'C:\Temp\Junk\Temp2.xlsx')
wbG.Worksheets('TAA2').Copy(Before=wbP.Worksheets("TAA"))
wbP.SaveAs(r'C:\Temp\Junk\Temp.xlsx')
excel.Quit()
del excel # ensure Excel process ends
this copies everything... (styles, formats, formulas etc.)
Another option is copy the whole workbook and delete the not needed sheets, if you create a new file and not editing an existing one...
Copy worksheet from one workbook to another one using Openpyxl

How can I use xlsxwriter and Excelwriter together to populate an excel file which contains some formatted text as well as python exported DataFrame?

I am writing a program in python that will work on a csv dataset. The aim of the code is to export the aggregated output in an excel, along with hardcoded header information. The excel file is a final report that is sent to the client; means full and final (as shown in below image).
This is how I begin:
I first created a typical header of the report using xlsxwriter and then tried to export the summary DataFrame (i.e. main table output) below the header using Excelwriter- DataFrame.to_excel
But as soon as I paste the DataFrame in the excel template I created in above step, the initially created header is wiped out, the cells appear blank. And only the DataFrame (table output) is displayed.
Alternatively, if I first export the dataFrame to the excel and then try to add a header to the excel report, header remains but the DataFrame is gone now.
What should I do in order to retain both; dataframe (table output) and header information? I used xlsxwritter, excelwriter of pandas.
Below are few lines of codes that might be important to explain the story.
writer = pd.ExcelWriter('SampleReport_1.xlsx', engine='xlsxwriter')
workbook = writer.book
WS_I = workbook.add_worksheet('I')
with ExcelWriter('SampleReport_1.xlsx') as writer:
dfI.to_excel(writer, sheet_name='I', header=True, index=True, startrow=9, startcol=0, engine=xlsxwriter, merge_cells=True)
writer.save()
cell_format1 = workbook.add_format()
cell_format1.set_bold()
for worksheet in workbook.worksheets():
worksheet.write('A1', 'Exhibit', cell_format1)
workbook.close()
I think I now get what the problem is. I tried the following approach on a sample Excel sheet with a preexiting header and it worked:
First - save your dataframe to csv
SampleReport_1_csv = df.to_csv (r'C:\Users\[your path]\SampleReport_1.csv', index = None, header=True)
Then open the Excel sheet with the preformatted header and (depending on your version of Excel) import the csv file by going to "data/get external data/from text". Importantly, in the last import step, Excel will ask you for the cell into which the csv is to be imported - make sure that that cell is under the preformatted block (I think it's cell A10, in your case).
Let me know if that works.
I found the below solution:
writer = pd.ExcelWriter('SampleReport_1.xlsx', engine='xlsxwriter')
workbook = writer.book
WS_I = workbook.add_worksheet('I')
cell_format1 = workbook.add_format()
cell_format1.set_bold()
for worksheet in workbook.worksheets():
worksheet.write('A1', 'Exhibit', cell_format1)
workbook.close()
WS_I.add_table('A3:G6', {'data': dfI_new.values.tolist(),'header_row': True})
Create a 2nd excel file with formatted template and link each cell to the 1st excel file. As long as you enable the link, changes to your python output should populate in the "template" excel file.

Append output to an existing Excel File using OpenPyXL

Step 1. Take input from an excel file.
Step 2. Ceate WB object.
Step 3. Take data from that WB object and create a list of dictionaries.
Step 4. Manipulate, Format, style the data.
Step 5. Now need to output the data and APPEND it into an Existing Excel Workbook.
Meaning the Existing Excel Workbook will be appended with the new data from
Step 1 constantly.
I have Steps 1 - 4 down and can output my data to a NEW workbook. I am coming up
empty on Step 5.
Any guidance or direction would be appreciated.
### Python 3.X ###
import sys
import time
import openpyxl
from openpyxl.styles import Alignment, Font, Style
from openpyxl.cell import get_column_letter
from pathlib import Path
###Double Click a Batch file, CMD opens, Client drags and drops excel File
###to be Formatted
try:
path = sys.argv[1]
except IndexError:
path = Path(input('Input file to read: ').strip("'").strip('"'))
output_string = str(Path(path.parent, path.stem + '.NewFormatted.xlsx'))
wb = openpyxl.load_workbook(str(path))
sheet1 = wb.worksheet[0]
###Do the Python Dance on Data###
###Style, Font, Alignment applied to sheets###
###Currently Saves output as a NEW excel File in the Directory the original
###file was drag and dropped from
wb.save(output_string)
###Need the output to be appended to an existing Excel file already present
###in a target directory
###Note Formatted Workbook (output) has multiple sheets###
openpyxl will let you edit existing workbooks including appending data to them. But the methods provided are limited to individual items of data such as the cells. There are no aggregate functions for copying things like worksheets from one workbook to another.

Categories

Resources