示例#1
0
    def MakeReport(self, sample = 'All', path = 'Default', docName = 'Default'):

        def doctable(document, data, tabletitle):
            document.add_heading(tabletitle)
            table = document.add_table(rows=(data.shape[0]+1), cols=data.shape[1]+1)  # First row are table headers!
            ind = data.index
            col = data.columns.values.tolist()
            if ind.name is None: ind.name = ' ' #incase there is no index header
            header = [ind.name] + col
            for i, column in enumerate(data) :
                table.cell(0,i).text = header[i] #column headers
                for r, row in enumerate(data.index.values) :
                    table.cell(r+1,0).text = str(ind[r]) #row names
                    if isinstance(data[column][row], np.float64):
                        dvalue = '%.2f' % data[column][row]
                    else:
                        dvalue = str(data[column][row])
                    table.cell(r+1, i+1).text = dvalue
                    
            table.cell(0,data.shape[1]).text = header[-1] #to get last column name
            table.style = 'Light Grid Accent 1'
        
        if sample == 'All':
            samples = self.samples
        else:
            samples = [sample]
        
        for sample in samples:
            if docName == 'Default':
                dName = sample + '_report.docx'
            if path == 'Default':
                path = os.getcwd() + '\\Report'
                print(path)
            self.PlotAll(sample = sample, path = path + '\\Chromatograms')
             
            document = doc()

            document.add_heading('Report',0)

            p = document.add_paragraph('Sample Name: ')
            p.add_run(sample).bold = True

            doctable(document, self.store['/' + sample + '/Method/MethodInfo'], 'Method Information')
            doctable(document, self.store['/' + sample + '/Method/GradientTable'], 'Gradient Table')
            doctable(document, self.store['/' + sample + '/Method/Wavelengths'], 'Signals')

            for signal in self.signals:
                document.add_heading('Signal: ' + signal ,level = 2)
                try: 
                    document.add_picture(path + '\\Chromatograms' + '\\' + sample + r'_' + signal + '.png')
                except KeyError:
                    document.add_paragraph('Signal does not exist in file')
                #print(path + '\\' + samp + r'_' + signal + '.png')
                try:
                    doctable(document, self.store['/' + sample + '/' + 'Signal' + signal + '/integration'], 'Integration Table')
                except:
                    document.add_paragraph('Integration table does not exist in file')
            print(path + '\\' + dName)
            document.save(path + '\\' + dName)
示例#2
0
import time
time.localtime()
now = time.strftime('%Y-%m-%d_%H-%M', time.localtime(time.time()))

engine = create_engine('mysql://root:@127.0.0.1/csrc?charset=utf8',
                       echo=False)  # 定义引擎
engine.execute('use csrc')
conn = engine.connect()

content = engine.execute('select content from bgczfkyj').fetchone()
url = engine.execute('select url from bgczfkyj').fetchall()
title = engine.execute('select title from bgczfkyj').fetchall()
# =============================================================================
# 下面这个方法可行,但是没有换行。估计还是得用 orm方式获取内容才行
# =============================================================================
document = doc()

for a in range(0, 496):
    i = 496 - a
    document.add_heading(title[i][0], level=1)
    document.add_paragraph(content[i])
    document.add_page_break()
    print(title[i][0])

file_name = '反馈意见' + now + '.docx'

document.save(file_name)

# =============================================================================
# 尝试用pd 可以用
# =============================================================================