enter code here
첫째, 저는 Python을 모르지만 팀 프로젝트에 사용하고 있습니다. 격자 무늬 빠른 시작 자습서를 따랐습니다. 내가받은 enter code here
데이터로 데이터 프레임 을 만들고이를 사용하여 html을 만들고 그로부터 pdf를 생성 할 수있었습니다. 시간이 걸렸습니다! 이제 금액이 음수이면 빨간색으로 만들고 금액 필드를 오른쪽 정렬하는 등 데이터에 몇 가지 기본 서식을 적용하고 싶습니다. 코드를 작성했지만 Jupyter Lab을 사용하지 않고 atom을 사용하고 있습니다. 변경 사항을 가져 오기 위해 style.render ()가 필요하다는 참조를 보았지만 방법을 모르겠습니다.
이것은 pdf에서 색상의 변화를 보여주지 않는 내 코드입니다.
# Execute query selecting Transactions table in Plaid database
cur.execute("SELECT description, date, amount FROM transactions")
# Fetch rows
rows = cur.fetchall()
# Close cursor
cur.close()
# Close connection
con.close
# set negative amounts to red
def color_negative_red(amount):
color = 'red' if amount < 0 else 'black'
return 'color: %s' % color
# dataframe
df = pd.DataFrame (rows, columns = ["Transaction Description", "Date", "Amount"])
df.style.applymap(color_negative_red)
# convert data frame to <table> and display
html = df.to_html()
text_file = open("test.html", "w")
text_file.write(html)
text_file.close()
# output to a pdf
import pdfkit
pdfkit.from_file('test.html', 'out.pdf')
# open report and pdf in a browser
import webbrowser as wb
wb.open_new_tab('file://C:/Users/Carole/dev/Plaid/quickstart/python/out.pdf')
테스트 샌드 박스 환경에서 생성 된 pdf :
마지막 변경 사항 : # 음수를 빨간색으로 설정 def color_negative_red (s) : color = 'red'if s <0 else 'black'return 'color : % s'% color html = df.style.applymap (color_negative_red) .render ( )
pd.set_option('display.max_rows', 500)
# print DataFrame
print (df)
text_file = open("test.html", "w")
text_file.write(html)
text_file.close()
# output to a pdf
import pdfkit
pdfkit.from_file('test.html', 'out.pdf')
이것은 금액 문자열을 십진수로 변환하는 코드를 얻는 마지막 변경 사항이었습니다.
from re import sub
from decimal import Decimal
new = s.replace("$","").replace(",","")
value = Decimal(new)
color = 'red' if value < 0 else 'black'
return 'color: %s' % color
당신은 스타일을 평가하지만 그것으로 아무것도하지 않습니다. .render()
스타일러 의 메서드를 사용하여 HTML 출력을 얻을 수 있습니다 .
html = df.style.applymap(color_negative_red).render()
이것은 전체 HTML 페이지가 아닙니다. HTML 테이블 및 스타일 정보는 포함되지만 html, head 또는 body 태그는 포함되지 않습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다