I am trying to align text in a cell inside of an excel file that was created with xlwings. I am aware that there isn't a top level function in xlwings to achieve that. However, in the documentation, it states that .api can be used to access the pywin32 (on windows) to achieve that goal and in the documentation for pywin32 there is a method called SetTextAlign.
My problem is that I cannot figure out how to call it or what arguments to pass into it. A good bit of info can be found here: http://timgolden.me.uk/pywin32-docs/PyCDC__SetTextAlign_meth.html
but that only helps me a tiny bit.
So, for example, my code could be:
self.sheet['A1'].value = 'test'
self.sheet['A1'].font.api ... # code here to set the alignment to center
Would be super appreciated if someone could help!
self.sheet['A1'].api.HorizontalAlignment = xlwings.constants.HAlign.xlHAlignCenter
Alignment is not a font property, but a Range property. A cell ('A1') is a Range.
You can find the Excel API here.
The PyCDC documentation you found is some internal Windows API, it is irrelevant for Excel features.
Related
I am trying to use GTK and libchamplain to display a map from local map data. The application is to be run on computers with no internet connection.
After taking a look at the mapbox.py example it seems like one should replace the NetworkTileSource source with FileTileSource. So I used the minimal.py example to work with FileTileSource:
widget = GtkChamplain.Embed()
widget.set_size_request(640, 480)
tile_source = Champlain.FileTileSource.new_full(
ID,
NAME,
LICENSE_TEXT,
LICENSE_URL,
MIN_ZOOM,
MAX_ZOOM,
TILE_SIZE,
Champlain.MapProjection.MERCATOR,
Champlain.ImageRenderer())
tile_source.load_map_data("map.osm")
widget.get_view().set_map_source(tile_source)
Unfortunately, when running the application, the map is not being displayed, and I receive the following error message:
(minimal.py:26308): libchamplain-WARNING **: NULL pixbuf
Based on some C examples (one, two), I assume that there is a so-called renderer missing, called Memphis, which is C only. It seems like Champlain's ImageRenderer, despite its similar name, is not the right tool for this.
So my question is how I would continue at this point. There are quite a few OSM renderers besides Memphis, probably some written in Python as well, but which one(s) would integrate well with Champlain?
It doesn't have to be be OSM, either. A collection of pre-rendered PNG would be fine with me, too. It's just that the FileTileSource documentation specifically mentions "Loads the OpenStreetMap XML file at the given path", which is why I assume better support for OSM.
Any help is much appreciated.
I managed to do it in a very simplistic way: just organize your tiles respecting the #z#/#x/#y# logic as described in the docs, then don't change anything in that mapbox.py example except the URI. your case, for a file structure on the /tmp directory could look like:
MAX_ZOOM,
TILE_SIZE,
Champlain.MapProjection.MERCATOR,
"file:///tmp/tiles-#Z#/#X#/#Y#.png",
#"https://a.tiles.mapbox.com/v4/mapbox.streets/#Z#/#X#/#Y#.png?access_token=" + ACCESS_TOKEN,
Champlain.ImageRenderer())
still according to the docs, the FileTileSource is meant for using a single osm file. I'm not so sure how that works, I didn't yet try it.
I have an excel workbook that is created using an excellent "xlsxwriter" module. In this workbook, there about about 200 embedded charts. I am now trying to export all those charts into several power point presentations. Ideally, I want to preserve the original format and embedded data without linking to external excel work book.
I am sure there is a way to do this using VBA. But, I was wondering if there is a way to do this using Python. Is there a way to put xlsxwriter chart objects into powerpoints ?
I have looked at python-pptx and can't find anything about getting charts or data series from excel work book.
Any help is appreciated !
After spending hours of trying different things, I have found the solution to this problem. Hopefully,it will help someone save some time.The following code will copy all the charts from "workbook_with_charts.xlsx" to "Final_PowerPoint.pptx."
For some reason, that I am yet to understand, it works better when running this Python program from CMD terminal. It sometimes breaks down if you tried to run this several times, even though the first run is usually OK.
Another issue is that in the fifth line, if you make False using "presentation=PowerPoint.Presentations.Add(False)," it does not work with Microsoft Office 2013, even though both "True" and "False" will still work with Microsoft Office 2010.
It would be great if someone can clarify these about two issues.
# importing the necessary libraries
import win32com.client
from win32com.client import constants
PowerPoint=win32com.client.Dispatch("PowerPoint.Application")
Excel=win32com.client.Dispatch("Excel.Application")
presentation=PowerPoint.Presentations.Add(True)
workbook=Excel.Workbooks.Open(Filename="C:\\.........\\workbook_with_charts.xlsx",ReadOnly=1,UpdateLinks=False)
for ws in workbook.Worksheets:
for chart in ws.ChartObjects():
# Copying all the charts from excel
chart.Activate()
chart.Copy()
Slide=presentation.Slides.Add(presentation.Slides.Count+1,constants.ppLayoutBlank)
Slide.Shapes.PasteSpecial(constants.ppPasteShape)
# WE are going to make the title of slide the same chart title
# This is optional
textbox=Slide.Shapes.AddTextbox(1,100,100,200,300)
textbox.TextFrame.TextRange.Text=str(chart.Chart.ChartTitle.Text)
presentation.SaveAs("C:\\...........\\Final_PowerPoint.pptx")
presentation.Close()
workbook.Close()
print 'Charts Finished Copying to Powerpoint Presentation'
Excel.Quit()
PowerPoint.Quit()
The approach I'd be inclined toward with the current python-pptx version is to read the Excel sheets for their data and recreate the charts in python-pptx. That of course would require knowing what the chart formatting is, etc., so I could see why you might not want to do that.
Importing charts directly from Excel has been done in the past, see the pull request here on GitHub: https://github.com/scanny/python-pptx/pull/65
But it involved a large amount of surgery on python-pptx, and many versions back now, so at most it might be a good guide to what strategies might work. You'd need to want it pretty bad I suppose to go that route :)
I don't have enough reputation to comment but if you get the same issue as #R__raki__ then you can use the integer value defined by the VBA reference. For this case it would be 12.
So replace
Slide=presentation.Slides.Add(presentation.Slides.Count+1,constants.ppLayoutBlank)
with
Slide=presentation.Slides.Add(presentation.Slides.Count+1,12)
See here for more.
Currently I'm working on Python to automate a powerpoint file. I'm using win32com.client to access the various objects needed. However I haven't been able to find any documentation on chart objects. can someone point me in the right direction.
import win32com.client
PPTApplication = win32com.client.Dispatch("PowerPoint.Application")
PPTApplication.Visible = True
PPTFile = PPTApplication.Presentations.Open("D:\test.pptx")
x=PPTFile.Slides(1).Shapes("Chart1").Chart.ChartData.Workbook.Sheets(1).Range("A1","C13").Value
The code above allows me to access the data for chart1.
How do I access the format axis options????
Or where can I find the documentation for the same.
The PowerPoint Chart object API is documented here:
https://msdn.microsoft.com/EN-US/library/office/ff746468.aspx
The direction you'll want to head is Chart.Axes(), perhaps Chart.Axes(xlValue).
Once you have the axis you want, you have the properties and methods listed here available to you:
https://msdn.microsoft.com/EN-US/library/office/ff745187.aspx
In my experience, it sometimes takes some digging to come up with the path to the right attribute, but that's the general procedure.
Usually when I don't find what I'm looking for I search on 'vba powerpoint chart font color' or whatever specific call I'm looking for and the example VBA code shows the "path" through the object hierarchy to the property I need.
This may be a long shot, but I figured it's worth asking. I need a way to programmatically insert externally linked images in excel, meaning that every time you open the file, the spreadsheet will contact the url at which the image is located. It's easy to do this manually in excel, but I want to do it programmatically, preferably with python. I've tried using the openpyxl and XlsxWriter libraries, but neither have this specific functionality. My only other option is to look for the excel source code so I can see how an externally linked image is represented by excel. I don't suppose Microsoft makes that source code public, do they?
Thanks for any suggestions
I'm using appscript to create an InDesign document from Database data, but I can't seem to figure out how to set the contents of the objects in my page. In older versions of ID (CS4 and earlier, if i'm correct) I could get the object with script label foo by calling spread.page_items['foo']. However, this does not seem to work anymore. spread.page_items.ID(<foo_id>).label.get() does show foo, so the script label is set correctly.
The Adobe documentation speaks of a label property, however I haven't yet figured out how to use that property to correctly select the right object.
If there is a way to obtain an object's ID easily, that might also do the trick.
I managed to solve this after finding this article about references in appscript. I had to use a filter, like so: spread.page_items[its.label == 'foo'].
Also, as suggested by Youngware below, the script labels support is indeed replaced by layer names.