Openpyxl - Apply Borders to Cells - python

I am hoping someone can help me as I have been unable to figure this one out for myself unfortunately.
I am trying to put a thin border around a cell using openpyxl 2.3.3 and python 3.4. I have the following code:
from openpyxl.styles import Border
ws.cell('A1').border = Border(top = Side(border_style='thin', color='FF000000'),
right = Side(border_style='thin', color='FF000000'),
bottom = Side(border_style='thin', color='FF000000'),
left = Side(border_style='thin', color='FF000000'))
Oddly this is throwing the following error:
NameError: name 'Side' is not defined
I have looked at the official documentation here:
http://openpyxl.readthedocs.org/en/2.4/styles.html
http://openpyxl.readthedocs.org/en/2.4/_modules/openpyxl/styles/borders.html
I have also looked at the following articles with no luck:
https://bitbucket.org/openpyxl/openpyxl/issues/365/styling-merged-cells-isnt-working
Applying borders to a cell in OpenPyxl
Apply borders to all cells in a range with openpyxl
Apply Border To Range Of Cells Using Openpyxl
Is anyone able to help me out?
Thanks in advance,
Eamon

You haven't imported the Side object.
from openpyxl.styles import Border, Side

Related

has anyone tried to add polylines on a folium map displayed on streamlit?

I tried to display bus lines on a map (on streamlit) using folium.Polylines,
but I keep getting the same error message :
ValueError: could not convert string to float
here's my code:
trajet_bus_fields = data
mapish= folium.Map(location = [48.083328, -1.68333])
for index, row in trajet_bus_fields.iterrows():
popup = row['idligne']
my_PolyLine=folium.PolyLine(locations=row["coordinates_reversed"],weight=3, popup = popup, color= "red")
mapish.add_child(my_PolyLine)
folium_static(mapish)
there is a step before this code that i used to invert the coordinates :
trajet_bus_fields["coordinates_reversed"]=np.empty((len(trajet_bus_fields), 0)).tolist()
here is a link to the .JSON File I used, that, of course I converted to .csv beforehand on google colab. (then i Dowloaded it to visual code where i'm launching streamlit)
[1]:https://github.com/HOUDZZZ/project3/blob/7c2336a1537cbfe4d1d96852871b79ee388feba9/parcours-des-lignes-de-bus-du-reseau-star.json
As I said i Keep getting the same message Error although it works on Google colab.
Here is also a link to my Colab if it helps:
[2]: https://colab.research.google.com/drive/1EWgKH-2S5HobWB-_Cex8aRjOC2PLR6dM?usp=sharing
Your Help will be very much appreciated, I looked for hours before posting my question

Can't get map to appear in Python Data Visualization

I am trying to visualize US COVID-19 data geographically within Python. Currently I have a CSV with all my data imported which contains case numbers, longitudes, latitudes etc. Currently my code is as follows:
df=pd.read_csv
fig=px.scatter_mapbox(df,lat='Lat', lon='Long', hover_name='Province_State', size='Confirmed',mapbox_style='open-streetmap',template='plotly_dark')
fig.write_html("Time_series_county_JH.html")
fig.show()
However, when I run the code I just get a black box with the legend on the right
Would be great if someone can help on how I can get the actual map to appear rather than just a black output. I am very new to Python so any help would be greatly appreciated.
I think you are facing a problem with rendering the image using plotly.
You could set the renderers for the plotly image as below:
import plotly.io as pio
pio.renderers.default = "colab"
And change the following line in your code as shown below:
pio.show(fig)
If the figure still shows black, then it is the problem with mapbox_style. Change it to the relevant requirement.
mapbox_style='carto-darkmatter'
As a whole:
df=pd.read_csv("/content/COVID-19_Cases_US.csv")
fig=px.scatter_mapbox(df, lat='Lat', lon='Long_', hover_name='Province_State', size='Confirmed',color='Confirmed',mapbox_style='carto-darkmatter',template='plotly_dark',zoom=0, size_max=70)
fig.write_html("Time_series_county_JH.html")
pio.show()
Result:
Update:
for mapbox_style = 'open-street-map' and code:
df=pd.read_csv("/content/COVID-19_Cases_US.csv")
fig=px.scatter_mapbox(df, lat='Lat', lon='Long_', hover_name='Province_State', size='Confirmed',color='Confirmed',mapbox_style='open-street-map',template='plotly_dark',zoom=4, size_max=70)
fig.write_html("Time_series_county_JH.html")
fig.show()
Here is the result:

xlwings: set cell formatting from python on a Mac specifically

I'm trying to format font size, font color, horizontal alignment, border style/weight etc on a worksheet using python xlwings on a Mac. I know you'd have to use the missing features to do this type of editing on python but i don't know what API member functions to use in order to do these formatting on Mac. The ones for windows I can find everywhere; for example:
import xlwings
wb = xlwings.Book('checker_output.xlsx')
wb.sheets.add('sheet1')
sht = wb.sheets['sheet1']
sht.range('A3:A26').api.Font.Size = 15
would work on the windows API, but not on a mac. Any work around for this?
I found you can use .api to center / left / right align but this is on a PC, perhaps it can work on Mac as well.
Set the value of the HorizontalAlignment property of the cell to:
Center-aligned, we’d set HorizontalValue to -4108;
Right-aligned, we’d set HorizontalValue to -4152;
Left-aligned, we’d set HorizontalValue to -4131;
What it would look like:
import xlwings as xw
sht = xw.sheets.active
sht.range(f'$A1:$C5').api.HorizontalAlignment = -4131

Python-docx find and replace image in table

How do I find an existing image in MS document table and replace it with a new one? With this code am able to find table cell/location of an image and add new image, but it doesn't replace the old image.
from docx import Document
from docx.shared import Inches
doc = docx.Document('myWordDoc.docx')
tables = doc.tables
# Find existing image and remove it then add new image
img = tables[0].rows[0].cells[0].add_paragraph()
r = img.add_run()
r.add_picture('/tmp/foo.jpg', width=Inches(2.5))
Is this possible with python-docx module or any other module?
I replaced your 3 last lines of code:
img = tables[0].rows[0].cells[0].add_paragraph()
r = img.add_run()
r.add_picture('/tmp/foo.jpg', width=Inches(2.5))
With something like this
#This seams to clear the content in my cell
tables[0].rows[0].cells[0]._element.clear_content()
#Then when the image is inserted to the cell it is not placed one linefeed down.
img = tables[0].rows[0].cells[0].add_paragraph().add_run().add_picture('Image.png', width=Inches(0.4))
It seams to work for me, but a big thanks for your question it put me on the right track, It was not only hard to remove the image, when I added the new image it was a line feed down. But my code, that is a bit compact fixed both of the problems.
Thanks!
Perhaps try using https://github.com/khanfarhan10/PythonCertificateGenerator/blob/main/ImageReplacer.py and suit the code to yourself!
All file manipulations are performed using python ZipFile.

How can I find out why PIL isn't drawing the font correctly?

Here's the code I'm using:
from PIL import Image
import ImageFont, ImageDraw
import sys
import pdb
img = Image.new("RGBA",(300,300))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(sys.argv[1],30)
draw.text((0,100),"world",font=font,fill="red")
del draw
img.save(sys.argv[2],"PNG")
and here's the image that results:
img http://www.freeimagehosting.net/image.php?976a0d3eaa.png ( for some reason, I can't make it show on SO, so the link is http://www.freeimagehosting.net/image.php?976a0d3eaa.png )
The thing is, I don't understand why it isn't drawing the font correctly? I should be able to read the word "world" off of it. It's like the picture's been cut in half or something. Does anyone have any clue?
EDIT: after balpha's comment, I decided to try another font. I'm only interested in ttf fonts, so I tried with another one, and it worked. This is kind of strange. The original font I tried to run this with is Beautiful ES. I'm curious if you guys can reproduce the same image on your computers, and if you happen to know the reason for why that is.
PIL uses the freetype2 library, so most possibly it is an issue with the font file; for example, it could have bad metrics defined (e.g see the OS/2 related ones opening the font with FontForge).

Categories