How to copy from Excel and paste as image in Word using Python? - python

How do I copy a set of cells from Excel and paste them as image in Word using Python ?
I am trying to automate some reports (in docx format). The excels are auto generated and then I have to manually copy cells from Excel and paste as image in Word.
I have done some research and found there are some libraries like python-docx (for word) and openpyxl (for excel). What I am not able to figure out is the copy and paste as image from excel to word.

I don't think this is an easy task. Because conversion Excel to image probably means the Python package/codes need to know how to render the excel content, which is a big step beyond read/write excel format. I don't know any Python package can do that.
Assume you're running Python codes in Windows, you may try to call COM to copy/paste directly from Excel to Word. I guess that would behave just like you manually Ctrl+C/V. Reference
If you prefer to convert to an image, may try a VBA script. Reference

Related

How to convert docx to pdf in python dash

I am trying to deploy a python dash app to Heroku server. I perform some calculations based on user input and I create a docx report (I have a specific .docx template and I fill it with data each time). My problem is that before sending that report to the user, I want to convert it to pdf. I was already using for offline use, docx2pdf however I do not know if heroku has word embedded. Apart from that, I do not want to save the .docx and then convert it to pdf and then delete that .docx. I want to do all the conversions when the files are in memory, not stored.
Can you suggest me any other way to do that?
I tried docx2pdf without success, and I do not want this approach either because I have to save the file.
I tried another solution that I found, conver the docx to html and then to pdf with pdfkit, but the conversion from docx to html really messed up the document.
I also tried several other minor libraries without success.

Write data with Python into existing excel file keeping it intact as much as possible

We have a rather complicated Excel based VBA Tool that shall be replaced by a proper Database and Python based application step by step.
There will be time of the transition between were the not yet completely ready Python tool and the already existing VBA solution will coexist.
To allow interoperability the Python tool must be able to export the database values into the Excel VBA Tool keeping it intact. Meaning that not only all VBA codes have to work as expected but also Shapes, Special Formats etc, Checkboxes etc. have to work after the export.
Currently a simple:
from openpyxl import load_workbook
wb = load_workbook(r'Tool.xlsm', keep_vba=True)
# Write some data i.e. (not required to destroy the file)
wb["SomeSheet!SomeCell"] = "SomeValue"
wb.save(r"Tool_filled.xlsm")
will destroy the file, i.e. shapes won't work, checkboxes neither. (The resulting file is only 5 MB from originally 8 MB, showing that something went quite wrong).
Is there a way to only modify only the data of an ExcelSheet keeping everything else intact/untouched?
As far I know an Excel Sheet are only zipped .xml files. So it should be possible to edit only the related sheets? Correct?
Is there a more comfortable way as writing everything from scratch to only modify the data of an existing Excel file?
Note: The solution has to work in Linux, so simple remote Excel calls are not an option.

Automatic input from text file in excel

My problem is rather simple : I have an Excel Sheet that does calculations and creates a graph based on the values of two cells in the sheet. I also have two lists of inputs in text files. I would like to loop through those text files, add the values to the excel sheet, refresh the sheet, and print the resulting graph to a pdf file or an excel file named something like 'input1 - input2.xlsx'.
My programming knowledge is limited, I am decent with Python and have looked into python libraries that work with excel such as openpyxl, however most of those don't seem to work for me for various reasons. Openpyxl deletes the graphs when opening an excel file; XlsxWriter can only write files, not read from them; and xlwings won't work for me.
Should I use python, which I'm familiar with, or would VBA work for this kind of problem? Have any of you ever done something of the sort?
Thanks in advance
As a more transitional approach to what m. wasowski wrote above, I'd suggest you do the following.
Install the pandas package, and see how easy it is to load a file using read_excel. Then, read 10 Minutes to Pandas, and manipulate the data.
You state that the Excel sheet is complex. In general, the more complex it is, this approach will eventually make it simpler. But you don't have to switch everything immediately. You can still do parts in Excel and parts in pandas.
I think you should consider win32Com for excel operation in python instead of Openpyxl,XlsxWriter.
you can read/write excel, create chart and format excel file using win32com without any limitation.
And creating chart you can consider matplotlib, in that after creating chart you can save it in pdf file also.

How to convert .xls file to pdf through python scripting

I have created .xls using xlwt module.
Now i want to convert this newly created xls file to pdf. Can anyone please tell me how can i do this through python scripting. Am using python 2.6 version.
There are at least two possibilities here: either you read the .xls back in as cells and generate a PDF, or maybe you want to print the .xls to .pdf from the program that just created the .xls.
In the first case you might want to consider writing the .pdf directly (you have the data to make the .xls in the program at some point). I have a grid class that can be written out in HTML format, or to .xls' (usingxlwt),.xlsm(usingopenpyxl) and to.pdf(usingreportlab`).
You will need to use the second option if you write formula and that kind of stuff in your .xls and want to have the calculated results in your .pdf. In that case use subprocess to call Microsoft Excel or OpenOffice/LibreOffice Calc with the right commandline parameters for printing/converting the file to PDF.
E.g. for LibreOffice 4.0 the commandline would be:
scalc --convert-to pdf yourfile.xls
which will result in a yourfile.pdf

Module that can help control excel from within python application anything more needed than pywin32?

I have a viewer I built using WXPython. The viewer is basically a browser (built on IE wx.lib.iewin) that loads the txt or htm files I have in a directory and then lets me move through the files sequentially. Instead of having to go to the directory to select the next file to view the viewer/browser has a next button that loads the next file in the queue.
I want to be able to add a new feature that allows me to highlight some text that is visible in the browser and then push a button and have that text passed into a cell in excel.
Lots of things are going to have to happen like I need to be able to find and start a new instance of excel. I need to be able to add a new worksheet and pass some values to populate cells on the worksheet based on the file I am looking at and then if I want to collect some data from the file I want to be able to highlight the data in the viewer and then press a button on the viewer and have the data passed to excel.
I think I am going to start with PyWin32 but I am wondering if there is something else I need but I don't know enough to look for it.
If someone knows of an example where text was piped from a Python application to excel under the users control I would appreciate a pointer in that direction. It is easy enough I think to do this going from the application to a file that gets created (but not displayed) but I am hoping to go from the browser to the excel file so that the user can evaluate their work in progress.
I'd recomend using one of the python excel modules like python-excel.
It works on any OS, and without any other Excel application installed.
http://www.python-excel.org/
Code would look somehting liek this to write to a new xls document, slightly different to open an existing.
import xlwt
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet 1')
# indexing is zero based, row then column
sheet.write(0,1,'test text')
wbk.save('test.xls')
Hopefully this can get you on the right path, then you'll be able to post more specific questions if you run into problems.
Note: Another option is openpyxl:
http://packages.python.org/openpyxl/tutorial.html
If you're using wxPython (which I assume you are due to the tag), you should look at XLSGrid: http://www.blog.pythonlibrary.org/2011/08/20/wxpython-new-widget-announced-xlsgrid/
If you just want to work with Excel, I would recommend xlwt or xlrd, although you can use PyWin32 to work with it too via COM: http://www.blog.pythonlibrary.org/2010/07/16/python-and-microsoft-office-using-pywin32/

Categories