Trouble with Excel Autofit in Python (3.6) - python

I am trying to autofit the columns in my python generated xlsx file.
Found the code from here[https://stackoverflow.com/a/33665967/5518944], but getting an exception.
I am using Microsoft Office 2015.
Using this code:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
ends up in following error:
[...]Python36\lib\site-packages\win32com\client\gencache.py", line 236, in GetModuleForCLSID
__import__(sub_mod_name)
ModuleNotFoundError: No module named 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x8._Application'
Are you able to help me with this problem?

I can access and edit .xlsx files using:
from win32com.client import Dispatch
xl = Dispatch("Excel.Application")
wb = xl.Workbooks.Open(Filename="yourfile.xlsx")
ws = wb.Worksheets(1)
etc..
But i'm not sure if you really need EnsureDispatch, see this for mor about the differences.

Related

How to convert Excel SpreadsheetML to .xlsx using python

I'm new to python, so i have this XML file which i can open pretty easily using Excel,
this is XML file
andi want to convert it to .xlsx or any compatible format to be able to use openpyxl module to it so that i can read it easily, is there any way to do this on python? Any advice would be appreciated thankyou.
I had the same problem. This answer helped me
Attempting to Parse an XLS (XML) File Using Python
You may save your Excel styled XML as xlsx with Workbook.SaveAs method using win32com (only for Windows users) and read in with pandas.read_excel
import win32com.client
import pandas as pd
original_file = "Your_downloaded_file.xml"
output = "Your_converted_file.xlsx"
xlApp = win32com.client.Dispatch("Excel.Application")
xlWbk = xlApp.Workbooks.Open(original_file)
xlWbk.SaveAs(output, 51)
xlWbk.Close(True)
xlApp.Quit()
output_df = pd.read_excel(output)
print(output_df.columns.ravel())

Error (little-endian) reading a XLS file with python

I download a XLS file from the web using selenium.
I tried many options I found in stack-overflow and other websites to read the XLS file :
import pandas as pd
df = pd.read_excel('test.xls') # Read XLS file
Expected "little-endian" marker, found b'\xff\xfe'
And
df = pd.ExcelFile('test.xls').parse('Sheet1') # Read XLSX file
Expected "little-endian" marker, found b'\xff\xfe'
And again
from xlrd import open_workbook
book = open_workbook('test.xls')
CompDocError: Expected "little-endian" marker, found b'\xff\xfe'
I have tried different encoding: utf-8, ANSII, utf_16_be, utf16
I have even tried to get the encoding of the file from notepad or other applications.
Type of file : Microsoft Excel 97-2003 Worksheet (.xls)
I can open the file with Excel without any issue.
What's frustrating is that if I open the file with excel and just press save I then can read the file with of the previous python command.
I would be really grateful if someone could provide me other ideas I could try. I need to open this file with a python script only.
Thanks,
Max
Solution(Somewhat messy but simple) that could potentially work for any type of Excel file :
Called VBA from python to Open and save the file in Excel. Excel "clean-up" the file and then Python is able to read it with any read Excel type function
Solution inspired by #Serge Ballesta and #John Y comments.
## Open a file in Excel and save it to correct the encoding error
import win32com.client
import pandas
downloadpath="c:\\firefox_downloads\\"
filename="myfile.xls"
xl=win32com.client.Dispatch("Excel.Application")
xl.Application.DisplayAlerts = False # disables Excel pop up message (for saving the file)
wb = xl.Workbooks.Open(Filename=downloadpath+filename)
wb.SaveAs(downloadpath+filename)
wb.Close
xl.Application.DisplayAlerts = True # enables Excel pop up message for saving the file
df = pandas.ExcelFile(downloadpath+filename).parse('Sheet1') # Read XLSX file
Thank you all!
What does pd mean?? What
pandas is made for data science. In my opinion, you have to use openpyxl (read and write only xlsx) or xlwt/xlrd (read xls... and write only xls).
from xlrd import open_workbook
book = open_workbook(<math file>)
sheet =....
It has several examples with this on Internet...

Copy Image from Excel and Save it using python

I am trying to copy an image from an excel named Inputs_v3 and sheet named Inputs and save. The code is as follows`
import win32com.client as win32
from PIL import ImageGrab
from xlrd import open_workbook
import os
excel = win32.gencache.EnsureDispatch("Excel.Application")
wb = open_workbook('Inputs_v3.xlsm')
r = wb.sheet_by_name('Inputs')
r.CopyPicture()
im = ImageGrab.grabclipboard()
im.save('somefile.png','PNG')
` The error is as follows
'Attribute error: 'Sheet' object has no attribute 'CopyPicture''
Please suggest where I am doing wrong.Thanks in advance
Use a python library called excel2img. In one line you can take a screenshot from any excel file
import excel2img
excel2img.export_img("Excel File Full Path", "Target Image full Path", "Excel SheetName", None)
and you can identify a specific cells range as well.
import excel2img
excel2img.export_img("test.xlsx", "test.bmp", "", "Sheet2!B2:C15")
I hope this will help.
The following code will get you the win32com reference that you actually need to access the Excel worksheet's objects and methods:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open('myworkbook.xlsx')
ws = wb.Worksheets('worksheet_name') # alternatively Worksheets(1) etc
Now you can do, for example:
ws.Shapes(1).CopyPicture()
I've tested this with Python 3.4, pywin32 219 and Excel 2010 on Windows 7.
Note that this doesn't involve xlrd at all - that's a package that can read Excel files without having Excel installed on the computer, but I don't know if it supports getting images of or from Excel workbooks.

Saving an already open Excel window with python

I have been having issues finding an answer for this one. I am currently utilizing win32com, but while it is very powerful, I cannot figure out how to tap into an Excel file that's already open.
There is no alternative ; an external script opens Excel and writes to it the data, it is not located on the hard disk
You can access all the opened workbooks by looping through Excel.Application.Workbooks:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
print("Active WB:", excel.ActiveWorkbook.Name)
for wb in excel.Workbooks:
print("WB:",wb.Name)
wb.Save()
I haven't got enough reputation to upvote Maxime Biette's answer but it's the right one.
If you just want one specific sheet to be saved:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
print("Active WB:", excel.ActiveWorkbook.Name)
for wb in excel.Workbooks:
if wb.Name == 'thenameofyourfile.xlsx' :
print("WB:",wb.Name)
wb.Save()

Python: Modifying Excel files while maintaing formatting

Is there any way to modify a few cells in an existing Excel file without losing formatting, and without using xlutils?
I have access to xlwt, and xlrd, but not xlutils.
Thank you!
Although its use is deprecated nowadays, you can use from win32com.client module which contains "dispatch". It preserves formatting.
Example :
from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible=1
book = xlApp.Workbooks.Open("FileName")
sheet1 = book.Worksheets(1)
sheet1.Cells(1,1).Value="ABC"

Categories